如何在.cs文件中,把不想在gridview中显示的列隐藏掉?
来源:百度知道 编辑:UC知道 时间:2024/06/18 06:14:22
如题目所说,我也不知再怎么细说了.
ASPX文件主要代码为:
<asp:GridView ID="dg" runat="server" >
</asp:GridView>
.CS文件主要代码为:
protected void Page_Load(object sender, EventArgs e)
{
string b = Request.QueryString["bianhao2"];
string dbpath = System.Configuration.ConfigurationSettings.AppSettings
["dbpath"];
OleDbConnection conn = new OleDbConnection
("Provider=Microsoft.jet.OLEDB.4.0;Data source=" + Server.MapPath(dbpath));
conn.Open();
string str = "select * from kehu where tid='" + b + "'";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
现在的情况是
ASPX文件主要代码为:
<asp:GridView ID="dg" runat="server" >
</asp:GridView>
.CS文件主要代码为:
protected void Page_Load(object sender, EventArgs e)
{
string b = Request.QueryString["bianhao2"];
string dbpath = System.Configuration.ConfigurationSettings.AppSettings
["dbpath"];
OleDbConnection conn = new OleDbConnection
("Provider=Microsoft.jet.OLEDB.4.0;Data source=" + Server.MapPath(dbpath));
conn.Open();
string str = "select * from kehu where tid='" + b + "'";
OleDbDataAdapter da = new OleDbDataAdapter(str, conn);
DataSet ds = new DataSet();
da.Fill(ds);
dg.DataSource = ds;
dg.DataBind();
}
现在的情况是
GridView默认会自动生成数据源的列,一般来说,不要让GridView自动生成列,那样就不好控制显示哪些列,也不好控制显示列的数据格式,
在aspx文件中,把GridView的【AutoGenerateColumns】属性设置成【false】:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"></asp:GridView>
然后手动添加绑定列或模板列,模板列更灵活,以下代码是添加模板列(假定你的数据源有UserName和TrueName两个字段):
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="用户名">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("UserName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="真实姓名">
<ItemTemplate>