C# 如何隐藏数据表中的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18085271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to hide column in data table
提问by daidai
I have DataTable
which I filled using an SQL Query. Then I filled the GridView
with the DataTable
.
我有DataTable
使用 SQL 查询填充的内容。然后我GridView
用DataTable
.
DataTable table = new DataTable();
table.Load(reader);
gvAktivne.DataSource = table;
gvAktivne.DataBind();
This works fine but now I want to hide the first column. When I add this:
这工作正常,但现在我想隐藏第一列。当我添加这个时:
gvAktivne.Columns[0].Visible = false;
I get an IndexOutOfRange
exception.
Does anybody have an idea how to fix this?
我得到一个IndexOutOfRange
例外。有人知道如何解决这个问题吗?
回答by Mike Perrenoud
Based on the problem statement it appears you have AutoGenerateColumns
set to true
. This is going to be problematic when you want to hide columns. You need to make sure that you issue that line of code afterthe DataBind()
and I would do it in OnPreRender
.
基于问题陈述看来你已经AutoGenerateColumns
设置true
。当您想隐藏列时,这将是有问题的。你需要确保你发出该行代码后的DataBind()
,我会做它OnPreRender
。
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
gvAktivne.Columns[0].Visible = false;
}
回答by bala
Try this:
尝试这个:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
}
回答by Linh Vu
This may work for you:
这可能对您有用:
DataTable data;
data.Columns[0].ColumnMapping = MappingType.Hidden;