但是这在ASP.NET 2.0时无效的.当一个列的可见性设置为false,控件不会再将数据绑定到该列中,所以你尝试得到隐藏列的值时,只能得到一个空的字符串.
However, this does not work in ASP.Net 2.0. When a column's visibility is set to False, then the Grid does not bind data to the column, and thus when you try to retrieve data from the hidden column, it either blows up or returns an empty string.
对于开发人员来说这个正是可大麻烦,现在提供一个解决方法,希望对你您会有帮助
条件
必须拥有Visual Studio 2005 或者 Visual Web Developer Express并对ASP.NET 2.0有一定了解
方法一:
在RowCreated事件中书写如下代码
void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow ||
e.Row.RowType == DataControlRowType.Header)
{
e.Row.Cells[0].Visible = false; //如果想使第1列不可见,则将它的可见性设为false
}
//可以根据需要设置更多的列
}
因为在RowCreated事件(隐藏)在绑定时候发生,所以这样就即能将数据绑定到列上,又隐藏了该列.所
以可以访问到隐藏列的值
方法二:
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() ;
}
方法三:
private void BindGrid()
{
String cmdText = "SELECT KMS_AccountInfo.AccountId, KMS_ServerInfo.ServerId, KMS_ServerInfo.ServerCountryCn, KMS_AccountInfo.StockNum,";
cmdText += " KMS_ServerInfo.ServerName,";
cmdText += " KMS_ServerInfo.GameTypeName,dbo.getmneed(KMS_ServerInfo.ServerId) AS needNum, (dbo.getmkc(KMS_ServerInfo.ServerId)-KMS_AccountInfo.StockNum) AS kcnum,dbo.getmkc(KMS_ServerInfo.ServerId) - dbo.getmneed(KMS_ServerInfo.ServerId) AS CanConvertNum FROM KMS_AccountInfo INNER JOIN";
cmdText += " KMS_ServerInfo ON KMS_AccountInfo.ServerId = KMS_ServerInfo.ServerId inner join kms_gameinfo on KMS_ServerInfo.gameid=kms_gameinfo.gameid Where KMS_ServerInfo.ServerName like '" + txt_ServerName.Text.Trim().ToString() + "%' and KMS_AccountInfo.IsRealStock = 0 and kms_gameinfo.gameid=";
cmdText += Convert.ToInt32(Session["OperatorGame"]);
cmdText += " ORDER BY KMS_ServerInfo.ServerCountryCn DESC, KMS_ServerInfo.ServerName";
SqlDataAdapter myadapter = new SqlDataAdapter(cmdText, myConnection);
DataSet DsOrderList = new DataSet();
myadapter.Fill(DsOrderList);
StockList.DataSource = DsOrderList;
StockList.DataBind();
StockList.Columns[0].Visible = false;
if (myConnection.State.ToString() == "Open") myConnection.Close();
}
在.aspx文件中设置隐藏列的Visible 属性为true 绑定后设定其属性为false即可