我在pageload中代码如下
- C# code
-
if (Page.IsPostBack)
{
string sqlstring = "select * from users";
db GVset = new db();
DataSet ds = GVset.GetDataSet(sqlstring);
this.GVinfo.DataSource = ds;
this.GVinfo.DataBind();
}
在网上我查到了两种解决办法 一种是: 在RowCreated事件中书写如下代码
- C# code
-
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = true; //如果想使第1列不可见,则将它的可见性设为false
//可以根据需要设置更多的列
}
另外一种是
- C# code
-
Public void myTestFunction()
{
string conString="....";//省略
string sqlquery="...";//省略
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter da = new SqlDataAdapter(sqlquery, con);
DataSet ds = new DataSet();
da.Fill(ds);
ds.Tables[0].Columns[0].ColumnMapping = MappingType.Hidden;
GridView1.DataSouce = ds.Tables[0];
GridView1.DataBind() ;
}
使用方法一的办法,我测试的时候加入断点,发现pageload执行完毕没有执行rowcreaded事件。 我使用方法二,观察执行顺序发现执行了,但是没有效果。 请帮忙分析下错误在哪里,在线等。
|
|
|
|
1:确认你有没有进行数据绑定?没有触发rowcreaded事件,或者你绑定的数据是否有值 2:直接删除table里面不需要显示的列 3:把自动绑定关闭掉手动绑定需要绑定的列 | |
|