Monday, October 22, 2007

How to set the value of a DataGridView cell programmatically

The new DataGridView control doesn't have a Cells collection, but it has Rows,Columns and a default indexer.

To set the value of a certain cell in the DataGridView, it is easy to use the indexer like this:

dataGridView1[1,1].Value = "New Value";

Or, we can do that using the Rows collection:

Rows[1].Cells[1].Value = "New Value";

For more information about the DataGridView see those links:

A New Grid Control in Windows Forms


Anonymous said...

Thanks for info. Very, very useful information. Simple one, but I spent quite a lot of time figuring out how to set the value for any cell in the datagridview. Greetings, FrenkyB.

Anonymous said...

But doing just this won't give you the same result as you get when the user enters a value e.g. you won't get a new row for new records.

Anonymous said...

Keep it as stupid as possible...

dgview.CurrentCell = dgview.Item(0, RowIndex)

SendKeys.Send("New Value")

Now you can get the same result like when user gives input