Method to Find GridView Column Index by Name

前端 未结 7 2102
萌比男神i
萌比男神i 2020-12-09 04:00

I\'m trying to write a small method to loop through and find a GridView Column by its Index, since it can change position based on what might be visible.

<
7条回答
  •  情话喂你
    2020-12-09 04:38

    I figured it out, I needed to be using DataControlField and slightly different syntax.

    The working version:

    private int GetColumnIndexByName(GridView grid, string name)
        {
            foreach (DataControlField col in grid.Columns)
            {
                if (col.HeaderText.ToLower().Trim() == name.ToLower().Trim())
                {
                    return grid.Columns.IndexOf(col);
                }
            }
    
            return -1;
        }
    

提交回复
热议问题