How to get the cell value by column name not by index in GridView in asp.net

后端 未结 10 1611
轻奢々
轻奢々 2020-12-01 06:21

I am having a gridview in asp.net and now I want the cell value by the column name but not by the cell index.

How would be it possible

10条回答
  •  甜味超标
    2020-12-01 06:52

    Header Row cells sometimes will not work. This will just return the column Index. It will help in a lot of different ways. I know this is not the answer he is requesting. But this will help for a lot people.

    public static int GetColumnIndexByName(GridView gridView, string columnName)
        {      
            for (int i = 0; i < gridView.Columns.Count ; i++)
            {
                if (gridView.Columns[i].HeaderText.ToUpper() == columnName.ToUpper() )
                {
                    return i;
                }
            }     
            return -1;
        }
    

提交回复
热议问题