从代码后面隐藏GridView中的列 [英] Hide Column in GridView from Code Behind

查看:186
本文介绍了从代码后面隐藏GridView中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中有一个gridview,我必须从后面的代码中设置(某些列有时不包括),但是一列包含超链接。我现在在.aspx页面中设置gridview和hyperlink列,如下所示:

 < asp:GridView runat =server ID =GridView1> 
<列>
< asp:BoundField DataField =EditHtmlEncode =falseHeaderText =HeaderStyle-Wrap =falseSortExpression =Edit/>
< /列>
< / asp:GridView>

我在后面的代码中绑定了其余的数据:

  GridView1.DataSource = dt; 
GridView1.DataBind();
Page.DataBind();

链接栏显示正常,链接按预期工作。但是,所创建的GridView在左边有链接列,我想要显示的所有列,然后是右侧包含链接字符串的另一列,如下所示:

 < a href ='ForecastComments.aspx?zip = 49905& date = 1day'>编辑

有没有办法摆脱这一列,但仍然有我的链接列?我无法从表中删除它,因为我需要访问链接列。我尝试将后面的代码更改为:

  GridView1.DataSource = dt; 
GridView1.DataBind();
this.GridView1.Columns [12] .Visible = false;
Page.DataBind();

因为我不想显示的列是第12列,但gridview中的列数显然只有1(不知道这是为什么),所以无论如何,它不工作。奖励:我宁愿在表格右侧的链接列,而不是左侧 - 如果有人知道如何做到这一点,那就太棒了。

解决方案

可以在 RowDataBound 中移动和/ GridView事件。您可以在标记中设置事件处理程序:

 < asp:GridView ID =GridView1runat =serverOnRowDataBound = GridView1_RowDataBound > 

在代码隐藏中,您可以定义一个变量来保存您想要的链接列索引删除:

  private int LinkColIndex; 

并且在将数据绑定到GridView之前从DataTable获取它的值:

  LinkColIndex = dt.Columns [Edit]。Ordinal; 
GridView1.DataSource = dt;
GridView1.DataBind();

最后,处理 RowDataBound 事件处理函数:

pre $ protected void GridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
TableCell cell = e.Row.Cells [0];
e.Row.Cells.RemoveAt(0);
e.Row.Cells.RemoveAt(LinkColIndex);
e.Row.Cells.Add(cell);
}


I have a gridview in my page that I have to set from the code behind (certain columns are not included sometimes), but one column contains hyperlinks. I am currently setting the gridview and hyperlink column in the .aspx page like this:

 <asp:GridView runat="server"  ID="GridView1" >
    <Columns>
        <asp:BoundField DataField="Edit" HtmlEncode="false" HeaderText="" HeaderStyle-Wrap="false" SortExpression="Edit" />
    </Columns>
 </asp:GridView>

I'm binding the rest of the data in the code behind:

GridView1.DataSource = dt;           
GridView1.DataBind();         
Page.DataBind();

The link column is showing up fine, and the links work as expected. However, the GridView that is created has the link column on the left, all of the columns that I want to show up, and then another column on the right that contains a string of the link, like this:

<a href='ForecastComments.aspx?zip=49905&date=1day'>Edit

Is there a way I can get rid of this column but still have my link column? I can't delete it from the table because I need access to it for the link column. I tried changing the code behind to this:

GridView1.DataSource = dt;           
GridView1.DataBind();            
this.GridView1.Columns[12].Visible = false;
Page.DataBind();

because the column I don't want showing is column 12, but the number of columns in the gridview is apparently only 1 (not sure why that is), so anyway, it didn't work. Bonus: I would rather have the link column on the right of the table, not the left--if anyone knows how to do that also, that'd be awesome.

解决方案

The cells can be moved and/or deleted in the RowDataBound event of the GridView. You can set the event handler in the markup:

<asp:GridView ID="GridView1" runat="server" OnRowDataBound="GridView1_RowDataBound">

In code-behind, you define a variable to hold the index of the link column that you want to remove:

private int LinkColIndex;

and get its value from the DataTable before binding the data to the GridView:

LinkColIndex = dt.Columns["Edit"].Ordinal;
GridView1.DataSource = dt;           
GridView1.DataBind();

Finally, you process the cells for each row in the RowDataBound event handler:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    TableCell cell = e.Row.Cells[0];
    e.Row.Cells.RemoveAt(0);
    e.Row.Cells.RemoveAt(LinkColIndex);
    e.Row.Cells.Add(cell);
}

这篇关于从代码后面隐藏GridView中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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