How to hide columns in an ASP.NET GridView with auto-generated columns?

前端 未结 12 1665
陌清茗
陌清茗 2020-11-30 08:28

GridView1.Columns.Count is always zero even SqlDataSource1.DataBind();

But Grid is ok

I can do

for (int i = 0; i < GridView1.HeaderRow.Cel         


        
相关标签:
12条回答
  • 2020-11-30 08:55

    I was having the same problem - need my GridView control's AutogenerateColumns to be 'true', due to it being bound by a SQL datasource, and thus I needed to hide some columns which must not be displayed in the GridView control.

    The way to accomplish this is to add some code to your GridView's '_RowDataBound' event, such as this (let's assume your GridView's ID is = 'MyGridView'):

    protected void MyGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[<index_of_cell>].Visible = false;
        }
    }
    

    That'll do the trick just fine ;-)

    0 讨论(0)
  • 2020-11-30 08:55

    Iterate through the GridView rows and make the cells of your target columns invisible. In this example I want to keeps columns 4-6 visible as is, so we skip those:

    foreach (GridViewRow row in yourGridView.Rows)
       {
         for (int i = 0; i < rows.Cells.Count; i++)
         {
            switch (i)
            {
               case 4:
               case 5:
               case 6:
                  continue;
            }
            row.Cells[i].Visible = false;
         };
       };
    

    Then you will need to remove the column headers separately (keep in mind that removing header cells changes the length of the GridView after each removal):

    grdReportRole.HeaderRow.Cells.RemoveAt(0);

    0 讨论(0)
  • 2020-11-30 08:56

    The Columns collection is only populated when AutoGenerateColumns=false, and you manually generate the columns yourself.

    A nice work-around for this is to dynamically populate the Columns collection yourself, before setting the DataSource property and calling DataBind().

    I have a function that manually adds the columns based on the contents of the DataTable that I want to display. Once I have done that (and then set the DataSource and called DataBind(), I can use the Columns collection and the Count value is correct, and I can turn the column visibility on and off as I initially wanted to.

    static void AddColumnsToGridView(GridView gv, DataTable table)
    {
        foreach (DataColumn column in table.Columns)
        {
            BoundField field = new BoundField();
            field.DataField = column.ColumnName;
            field.HeaderText = column.ColumnName;
            gv.Columns.Add(field);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:58

    You have to perform the GridView1.Columns[i].Visible = false; after the grid has been databound.

    0 讨论(0)
  • 2020-11-30 08:59

    As said by others, RowDataBound or RowCreated event should work but if you want to avoid events declaration and put the whole code just below DataBind function call, you can do the following:

    GridView1.DataBind()
    If GridView1.Rows.Count > 0 Then
        GridView1.HeaderRow.Cells(0).Visible = False
        For i As Integer = 0 To GridView1.Rows.Count - 1
            GridView1.Rows(i).Cells(0).Visible = False
        Next
    End If
    
    0 讨论(0)
  • 2020-11-30 08:59

    Similar to accepted answer but allows use of ColumnNames and binds to RowDataBound().

    Dictionary<string, int> _headerIndiciesForAbcGridView = null;
    
    protected void abcGridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (_headerIndiciesForAbcGridView == null) // builds once per http request
        {
            int index = 0;
            _headerIndiciesForAbcGridView = ((Table)((GridView)sender).Controls[0]).Rows[0].Cells
                .Cast<TableCell>()
                .ToDictionary(c => c.Text, c => index++);
        }
    
        e.Row.Cells[_headerIndiciesForAbcGridView["theColumnName"]].Visible = false;
    }
    

    Not sure if it works with RowCreated().

    0 讨论(0)
提交回复
热议问题