Iterating through Datagridview column headers

11,850

Solution 1

foreach (DataGridViewColumn column in myDataGridView.Columns)
{
   DataGridViewColumnHeaderCell headerCell = column.HeaderCell;
   string headerCaptionText = column.HeaderText;
   string columnName = column.Name; // Used as a key to myDataGridView.Columns['key_name'];
}

Solution 2

You can iterate over the DataGridView.Columns property retrieving its header via the Name property as in this example. Now, if you have set an associated header cell then you need to use the HeaderText property instead.

Share:
11,850
TtT23
Author by

TtT23

Updated on June 23, 2022

Comments

  • TtT23
    TtT23 almost 2 years

    Given a DataGridView, I'd like to iterate through datagridivew column headers.

    How can this be done?