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

前端 未结 12 1678
陌清茗
陌清茗 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

    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);

提交回复
热议问题