如何获取标题匹配某些字符串的列索引?

时间:2016-02-22 16:47:50

标签: c# datagridview

我需要获取DataGridViewRow的列索引,其标题文本与字符串匹配"删除"。

  1. 你能给我一个提示吗?
  2. 如何通过以下表达式达到:

    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" */));
    

2 个答案:

答案 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()