Given a DataGridView, I'd like to iterate through datagridivew column headers.
How can this be done?
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'];
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With