For Each Loop through DataGridViewColumn Headers

2019-07-20 02:16发布

问题:

I have a DataGridView called DataGridViewHistorical that has columns for 2012 to 1900. I am trying to loop through the column headers and use the year to find a specific date in a string. So in the example below I would be looking for 2012-01-03, 2011-01-03, 2010-01-03 ... 1900-01-03.

For Each columnYear As DataGridViewColumn In DataGridViewHistorical.Columns
  MessageBox.Show(strBuffer.IndexOf(columnYear & "-01-03"))
Next

When I run this code I get error "Operator '&' is not defined for types 'System.Windows.Forms.DataGridViewColumn' and 'String'."

How can I get this to work?

回答1:

I think you are looking for the HeaderText property of the DataGridViewColumn:

MessageBox.Show(strBuffer.IndexOf(columnYear.HeaderText & "-01-03"))