I have a WinForms application with a DataGridView, which DataSource is a DataTable (filled from SQL Server) which has a column of xxx. The following code raises the exception of
ArgumentException was unhandled. Column named xxx cannot be found.
foreach (DataGridViewRow row in Rows)
{
if (object.Equals(row.Cells["xxx"].Value, 123))
}
Is it possible to get the cell values by column name?
DataGridViewColumnobjects have aName(shown only in the forms designer) and aHeaderText(shown in the GUI at the top of the column) property. The indexer in your example uses the column’sNameproperty, so since you say that isn’t working I assume you’re really trying to use the column’s header.There isn’t anything built in that does what you want, but it’s easy enough to add. I’d use an extension method to make it easy to use:
And then in your code: