Method to Find GridView Column Index by Name

前端 未结 7 2043
萌比男神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:36

    This way, works for me (.NET Gridview):

        private int GetColumnIndexByName(GridView grid, string name)
        {
            for (int i = 0; i < grid.HeaderRow.Cells.Count; i++)
            {
                if (grid.HeaderRow.Cells[i].Text.ToLower().Trim() == name.ToLower().Trim())
                {
                    return i;
                }
            }
            return -1;
        }
    

提交回复
热议问题