Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating through Datagridview column headers

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

How can this be done?

like image 828
l46kok Avatar asked Oct 26 '25 04:10

l46kok


2 Answers

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'];
}
like image 66
Sepster Avatar answered Oct 27 '25 17:10

Sepster


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.

like image 42
Erre Efe Avatar answered Oct 27 '25 19:10

Erre Efe