如何隐藏ASP.Net Gridview列 [英] How to hide ASP.Net Gridview column

查看:83
本文介绍了如何隐藏ASP.Net Gridview列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,下面的代码假设要隐藏一列,但它给了我这个错误:

Hi guys, code below suppose to hide a column but its giving me this error:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

Source Error:


Line 28:         if (grdAvailableStuff.Columns.Count > 0)
Line 29:         {
Line 30:             grdAvailableStuff.Columns[2].Visible = false;
Line 31:
Line 32:



检查下面的代码,我不了解发生了什么



Check the code below, I dont knw what hapend

if (grdAvailableStuff.Columns.Count > 0)
       {
           grdAvailableStuff.Columns[2].Visible = false;


       }
       else
       {
           grdAvailableStuff.HeaderRow.Cells[2].Visible = false;


           foreach (GridViewRow grd in grdAvailableStuff.Rows)
           {
               grd.Cells[2].Visible = false;


           }
       }

推荐答案

此处

grdAvailableStuff.Columns [2] .Visible = false;

表示您正在隐藏网格视图的第三列.

确保网格视图呈现3列或3列以上.
here

grdAvailableStuff.Columns[2].Visible = false;

Means your are hiding 3rd column of grid view.

Make sure that grid view renders with 3 or more than 3 columns.


尝试一下

Try this

protected void grdAvailableStuff_RowCreated(object sender, GridViewRowEventArgs e)
       {
           if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
           {
               e.Row.Cells[1].Visible = false;
              
           }
       }


这篇关于如何隐藏ASP.Net Gridview列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆