Method to Find GridView Column Index by Name

前端 未结 7 2045
萌比男神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
    //Get index of column by header text.
        int GetColumnIndexByName(GridViewRow row, string headerText)
        {
            int columnIndex = 0;
            foreach (DataControlFieldCell cell in row.Cells)
            {
                if(cell.ContainingField is TemplateField){
                    if(((TemplateField)cell.ContainingField).HeaderText.Equals(headerText))
                    {
                        break;
                    } 
                }
              if(cell.ContainingField is BoundField){
                        if (((BoundField)cell.ContainingField).HeaderText.Equals(headerText))
                    {
                        break;
                    }
              }
                columnIndex++; 
            }
    
    
            return columnIndex;
        }
    
    0 讨论(0)
提交回复
热议问题