Method to Find GridView Column Index by Name

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

    I prefer collection iteration but why bother with the overhead of foreach and grid.Columns.IndexOf call in this case? Just iterate through array with an index.

    private int GetColumnIndexByName(GridView grid, string name)
    {
        for(int i = 0; i < grid.Columns.Count; i++)
        {
            if (grid.Columns[i].HeaderText.ToLower().Trim() == name.ToLower().Trim())
            {
                return i;
            }
        }
    
        return -1;
    }
    
    0 讨论(0)
  • 2020-12-09 04:18

    In case if you need a column itself and not just its index you can use some Linq magic:

    DataControlField col=GridView1.Columns.Cast<DataControlField>().First(c => c.HeaderText == "Column_header")
    
    0 讨论(0)
  • 2020-12-09 04:21

    Better solution which works for Datafield, SortExpression and headerText.

    public static int GetBoundFieldIndexByName(this GridView gv,string name)
        {
            int index = 0;
            bool found = false;
            foreach (DataControlField c in gv.Columns)
            {
                if (c is BoundField)
                {
                    BoundField field = (BoundField)c;
                    if (name == field.DataField ||
                        name == field.SortExpression ||
                        name == field.HeaderText)
                    {
                        found = true;
                        break;
                    }
                }
                index++;
            }
            return found ? index : -1;
        }
    
    0 讨论(0)
  • 2020-12-09 04:36

    Here's a VB version

    Protected Function GetColumnIndexByHeaderText(grid As GridView, findHeader As String) As Integer
        Dim i As Integer = 0
        For i = 0 To grid.Columns.Count - 1
            If grid.Columns(i).HeaderText.ToLower().Trim() = findHeader.ToLower().Trim() Then
                Return i
            End If
        Next
    
        Return -1
    End Function
    
    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
  • 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;
        }
    
    0 讨论(0)
提交回复
热议问题