How to read acontent of a cell in datagrid view
Hi Every Body
I'm a new in VC#.net and want to know How to read the content of a cell in datagridview control.
for exampel i wrote the code below to add an items to the datagridview cells
DataTable table = new DataTable();
DataColumn nameCol;
DataColumn emailCol;
DataColumn telNumCol;
DataRow row = table.NewRow();
row[nameCol] = telRecordObj.GSName;
row[emailCol] = telRecordObj.GSEmail;
row[telNumCol] = telRecordObj.GSTeleNumber;
table.Rows.Add(row);
dataGridView1.DataSource = table.DefaultView;
now what's the code for reading the content of a row of cells
Re: How to read acontent of a cell in datagrid view
I havn't tested yet as I dont know the names of your DataColumns but it should look something like
Code:
string name = (string)dataGridView1.Rows[row].Cells["do columnName as string here"].Value;
// like in your examplestring name =(string)dataGridView1.Rows[0].Cells["First Name"].Value;
// or you also can do
string name =(string)dataGridView1.Rows[0].Cells[0].Value;
// or also this way
string name =(string)dataGridView1.Rows[0].Cells[nameCol.ColumnName ].Value;
This should work. BTW I cannot see your DataColumn working as they are not initialized, the dataType isn't set and all that.
Why instead of this, you are not using an existing database and filling a DataTable using a DataAdapter ?
If you want to do it as you have typed it here it needs to be
Code:
DataTable table = new DataTable();
// you need to initialize the Column objects and at least set a headertext
// you also can format the columns in the DataColumns like setting datatypes ... DataColumn nameCol = new DataColumn("First Name");
DataColumn emailCol = new DataColumn("Email");
DataColumn telNumCol = new DataColumn("Phone");
// You need to add the DataColumns to the table table.Columns.AddRange(new DataColumn[]{ nameCol,emailCol,telNumCol });
DataRow row = table.NewRow();
row[nameCol] = "Brown Joe"; // as I cannot test your objects I used imple strings for testing example row[emailCol] = "[email protected]";
row[telNumCol] = "0666 66 69 66";
table.Rows.Add(row);
dataGridView1.DataSource = table.DefaultView;
BTW Please read forumrules and use Code Tags as you can see how to do in the signature lines of my post
Re: How to read acontent of a cell in datagrid view
Thanks JonnyPoet
It works fine,with respect to my code i want tell that the code i wrote is a part of my project which is a bout telephone index which take my data(Name,Tel Number,Email address) from text box and store the data in telRecord object and store that object in a sequential file,the datagrid are filled from that file at the beginning of the application
Re: How to read acontent of a cell in datagrid view
Quote:
Originally Posted by
Falcon Eyes
...
It works fine,with respect to my code i want tell that the code i wrote is a part of my project which is a bout telephone index ...
I thought something like that and that this isn't your problem. But I didn't want to create this class as I think you have done that yourself. So I simple used strings in my example :D
Re: How to read acontent of a cell in datagrid view
BTW
I find it easier to write (and for others to understand):
Code:
string abc = (string)dataGridView1[0,1].Value;
than
Code:
string abc = (string)datagridview.row[1].cell[0].Value;
Re: How to read acontent of a cell in datagrid view
you all right k_nemelka it's more easier
any way thanks to JonnyPoet & k_nemelka
Re: How to read acontent of a cell in datagrid view
Quote:
Originally Posted by
k_nemelka
BTW
I find it easier to write (and for others to understand):
Code:
string abc = (string)dataGridView1[0,1].Value;
than
Code:
string abc = (string)datagridview.row[1].cell[0].Value;
You are right that I havn't shown all ways to solve his problem. But I really disagree about whats easier to understand. :mad: For a newbie you can easily see which index is for row and which for column if you write it in an full expressive way. You are right, if you are talking about what is shorter to type. Because having both indexes in one bracket, who will tell you reading that, which value the row and which is column index is. Then you only can see it in the itellisence help but not here in forum. So in which way you think its easier to understand ??? :rolleyes:
Re: How to read acontent of a cell in datagrid view
Don't be angry JonnyPoet booth of u r right
Re: How to read acontent of a cell in datagrid view
Quote:
Originally Posted by
Falcon Eyes
Don't be angry JonnyPoet booth of u r right
hehe - little diplomat , arn't you :D -
But if no one should not use this bad 'Smilies', why they are there ;) :lol:
BTW Nothing is eaten as hot as it comes to the table.:D