Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I iterate through all the rows and columns in a DataGrid?

You use the row index and column index as indexers on the DataGrid object.

private void IterateOverDataTable()
{
  CurrencyManager cm =
      (CurrencyManager) BindingContext[ dataGrid1.DataSource ];
  int rowCount = cm.Count;
  // assumes data source is a DataTable
  int colCount = ( (DataTable) dataGrid1.DataSource ).Columns.Count;

  for ( int row = 0; row < rowCount; ++row )
    for ( int col = 0; col < colCount; ++col )
    {
      object cellValue = dataGrid1[ row, col ];
      // process cellValue here...
    }
}

Contributed from George Shepherd's Windows Forms FAQ



Page view counter