如何得到数据库中没有在GridView1前台显示的值?
protected void Page_Load(object sender, EventArgs e)
{
string sqlstr;
sqlstr = "select * from record";
string connectionstring = "没写";
SqlConnection conn1 = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sqlstr, conn1);
conn1.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
dd.Close();
}
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow gvr in GridView1.Rows)
{
string text = gvr.Cells[3].Text;
switch (text)
{
case "1":
gvr.BackColor = System.Drawing.Color.FromName("#FFFFFF");
break;
case "2":
gvr.BackColor = System.Drawing.Color.FromName("#000000");
break;
}
}
}
//string text = gvr.Cells[3].Text;可以得到在GridView1中显示的第三列值,但在GridView1中没有显示的如何得到????比如表record中一个字段名 number,如何取的值?
------解决方案--------------------
有很多方法可以实现.比如:
1.设置GridView的某些列不显示,比如 GridView1.Columns[1].Visible = false,这样后台仍能取值.
2.在GridView的某个模板列"偷偷"塞进几个HiddenField.
3.把可能用到的列关键字设定在 DataKeyNames中.
等等.