DataGridView in c# - Getting cell value

D

dushkin

Hello.

I am trying to work with DataGridView object.

I put it on a form, launcehd it and added a new row with 3 cells.
Then I edited the three cells.

So it looks like this:

+-------------+-------------+-------------+-------------+
| | HD1 | HD2 | HD3 |
+-------------+-------------+-------------+-------------+
| | Val1 | Val2 | Val3 |
+-------------+-------------+-------------+-------------+

Now I am strugging to get the values from the grid. I tried this code
(among many others...)

foreach (DataGridViewRow row in dataGridView1.Rows)
{
DataGridViewCellCollection cells = row.Cells;
Console.WriteLine("Cells Count = {0}", cells.Count);
Console.WriteLine("Cells[0] = {0}", cells[0].ToString());
Console.WriteLine("Cells[0].value = {0}", cells[0].Value);
}


But the value I get is null !

The other output is

Cells Count = 3
Cells[0] = DataGridViewTextBoxCell { ColumnIndex=0, RowIndex=0 }

What is wrong here?

I am editing the grid directly in the cell (inplace editing). Do I
need to do an extra operation before I can try to retrieve back the
text?

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top