我需要获取DataGridViewRow
的列索引,其标题文本与字符串匹配"删除"。
如何通过以下表达式达到:
DataGridViewColumn c = new DataGridViewColumn();
int index = Specialization_DataGridView.Columns.IndexOf(c.HeaderText.Where(/* dont know how to write expression where header text matches to "delete" */));
答案 0 :(得分:1)
如果您将Name
的{{1}}和HeaderText
属性设置为相同,那么获取索引就是:
DataGridViewColumn
如果它们不相同,则循环遍历列,直到找到它为止:
int index = Specialization_DataGridView.Columns["delete"].Index;
答案 1 :(得分:0)
尝试使用扩展:
<Extension>
Public Sub DgvColumnNameByHeaderText(Dgv As DataGridView)
Dim c As DataGridViewColumn
With Dgv
For Each c In Dgv.Columns
c.Name = c.HeaderText
Next
End With
End Sub
' Use: DataGridView1.DgvColumnNameByHeaderText()