在网格视图中隐藏列... [英] Hidding A Collumn in Grid View.......

查看:79
本文介绍了在网格视图中隐藏列...的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string qry = "select * from Users where UserName=''Admin''";
       da = new SqlDataAdapter(qry, connection);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView1.DataSource = ds.Tables[0];
       GridView1.DataBind();


       GridView1.Columns[0].Visible = false;
      // GridView1.Columns[1].Visible = false;// error//



现在要隐藏第一列(Columns [0]),它可以工作,但是当我想隐藏第二列(Columns [1])时,它显示的错误是:

索引超出范围.必须为非负数并且小于集合的大小.
参数名称:index

虽然我的表格中有5列


在此先感谢.......



Now to hide 1st column (Columns[0]) it works but when I want to Hide column 2nd (Columns[1]) it is showing error that :

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

While i have 5 column in my Table


Thanks in Advance.......

推荐答案

首先,不要从表(*)中选择所有内容.只是您需要的列.虽然这样做会更好,但是当向表中添加新列时,它也会更好地工作.

关于列隐藏.我不想让gridview填充列.而是手动(以代码形式)创建列,然后将数据表绑定到gridview.
First, don''t select everything from a table (*). Just the columns you need. While this is going to perform better, it also is going to work better when new columns are added to your table.

About the column hiding. I''d prefer not to let the gridview to populate the columns. Instead create the columns by hand (in code) and bind the datatable to the gridview after that.


如果它给您索引超出范围"错误,则必须减少集合中的2列以上.
如果您确定您的表包含5列,则SELECT查询必须不返回任何行,否则您的表还没有任何列.进行检查:在其中添加一些跟踪内容或添加断点,然后查看集合本身.
If it is giving you an "Index out of range" error, then there must be less than 2 columns in the collection.
If you are sure that your table contains 5 columns, then the SELECT query must be returning no rows, or your table has not got any columns yet. Check it: put some tracing stuff in or add a breakpoint and look at the collection itself.


您可以在设计时隐藏列.

如果您的应用程序仅需要4列,则在选择查询中仅写入4列名称.
you can hide columns at design time.

if your application needs only 4 columns then write only 4 column name in select query.


这篇关于在网格视图中隐藏列...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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