我如何在运行时删除或隐藏GridView的列自动生成的字段. [英] How i Remove or Hide the column Auto Generated fields of GridView at Runtime.

查看:175
本文介绍了我如何在运行时删除或隐藏GridView的列自动生成的字段.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

如何在运行时删除或隐藏Gridview的自动生成的字段"列?
现在,在当前代码中,输出(运行时)Gridview显示的列字段与数据库中存储的列字段相同.我要显示选定的字段.

代码:

Hello

How can I Remove or hide the column Auto Genrated Fields of Gridview at Run Time?
now in current code the output (Runtime) Gridview shows the same column fields as stored in database. I want to display selected fields.

Code:

protected void Button1_Click(object sender, EventArgs e)
  {
      GridView1.Visible = true;
      SqlCommand cmd = new SqlCommand();
      cmd.Parameters.Add("@ten", SqlDbType.Float).Value = TextBox1.Text;
      cmd.Parameters.Add("@twel", SqlDbType.Float).Value = TextBox2.Text;
      cmd.CommandText = "select * from table1 where (ten >=  @ten) and (twel >= @twel)";
      cmd.Connection = con;
      SqlDataReader dr;
      dr = cmd.ExecuteReader();
      if (dr.HasRows)
      {
          dr.Read();
          GridView1.DataSource = dr;
          GridView1.DataBind();
                         // To edit Header Text of gridview at run time
          GridViewRow header = GridView1.HeaderRow;
          header.Cells[0].Text = "10th %age";
          header.Cells[1].Text = "12th %age";
      }
      GridView1.Columns.RemoveAt(1);
  }



当我使用GridView1.Columns.RemoveAt(1);
用于删除第二列,则错误为:
索引超出范围.必须为非负数并且小于集合的大小.
参数名称:index

在此代码中,显示了这两个字段.并且仅是第一个字段.
如果还有其他方法,请建议我...

[edit]已添加代码块,忽略HTML ..."选项已禁用-OriginalGriff [/edit]



When I use GridView1.Columns.RemoveAt(1);
for removing second column then ERROR is:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

in this code out put is shows the both 2 fields. and required is only first field.
If there is an another method then please suggest me...

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]

推荐答案

首先,您确定是否有任何表行符合您的条件?如果没有,那么
Firstly, are you sure there are any table rows which meet your conditions? If there aren''t, then
if (dr.HasRows)

将失败,并且您不会将任何数据绑定到GridView.
这会导致您描述的错误!

尝试将

will fail, and you will not bind any data to the GridView.
This would cause the error you describe!

Try moving

GridView1.Columns.RemoveAt(1);

上移到绑定数据的块中...

但是,如果您不想要它,为什么还要从数据库中获取数据呢?更改SELECT语句效率更高:

up into the block where you bind the data...

But why are you getting data from the database if you don''t want it? It is a lot more efficient to change your SELECT statement:

cmd.CommandText = "select * from table1 where (ten >=  @ten) and (twel >= @twel)";


cmd.CommandText = "select ten from table1 where (ten >=  @ten) and (twel >= @twel)";

,并且根本不返回第一列的内容.

当您知道您感兴趣的字段是...时,通常认为使用"SELECT * FROM"是不正确的做法.

and not to return the content of column one at all.

It is generally considered bad practice to use "SELECT * FROM" when you know what the fields you are interested in are...


最佳方法是选择所需的列.为什么要选择所有列并在那之后隐藏,尽管有隐藏的方法.
The optimal method would be selecting required columns. Why select all columns and hide after that, though ways are there to hide.


假设您在gridview中有3列.
"ID",名称",年龄"
这些列的索引值为0、1、2

因此,如果您想删除名称"列,则可以使用.
GridView1.Columns.RemoveAt(1);
完成此操作后,网格将重新排列其列.它将具有2列.
"ID",年龄".
因此从技术上讲,此代码将无效.

GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(2);
索引将超出范围.在第一行代码之后,原因是它已重新排列了带有索引的列.

Suppose you have 3 columns in a gridview.
"ID", "Name", "Age"
Index value of these columns would be 0, 1, 2

so if you wanna remove the column "Name", you''d use.
GridView1.Columns.RemoveAt(1);
After you do that The Grid will re-arrange its columns. It will have 2 columns then.
"ID", "Age".
So technically this code won''t work.

GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(2);
Index will be out of range. Cause after first line of code it has re-arranged the columns with the indexes.

GridView1.Columns.RemoveAt(1);
GridView1.Columns.RemoveAt(1);


但这会同时删除名称"和年龄"列.

在使用循环时,请记住在满足RemoveAt()条件时执行i.


But this will remove both "Name" and "Age" columns.

While you''re using loop, remember to do i-- when the RemoveAt() condition meets.


这篇关于我如何在运行时删除或隐藏GridView的列自动生成的字段.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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