Referencing a cell in a DataGridView

sfx

Well-known member
Joined
Jan 31, 2006
Messages
46
Programming Experience
Beginner
Hello All,

How do I reference an exact cell's text or value property in a DataGridView control on a form?

Cheers,

sfx
 
VB.NET:
myDataGridView.Rows(rowIndex).Cells(columnIndexOrName).Value
Note that this will access the value to which the cell is bound if the it is in a bound column. This will not necessarily be what's displayed. For instance, if the cell is in a ComboBox column and you've set the column's DataSource, DisplayMember and ValueMember, the Value property of the cell will come from the ValueMember while the actual displayed value comes from the DisplayMember and would be accessed via the cell's FormattedValue property.
 
Hi jmcilhinney,

Thank you very much for your input! It is greatly appreciated.

May I ask, do you know how to add a preset value to a cell in a DataGridView every time a new row is added to it via a BindingNavigator 'Add' button?

Thanks again,

sfx
 
What's the data source for your BindingSource? If it's a DataTable then you should set the DefaultValue property of the DataColumn that maps to that column in the grid. Otherwise you could handle the UserAddedRow event of the grid and set the Value of the desired cell. That's untested mind you, but it seems like it would be sound.
 
Thanks again for your advice. I have since found that the DefaultValue property of the DataColumn did the trick.

Take care,

sfx
 
Back
Top