vs2005 请问如何将GRIDVIEW选定行的内容显示在textbox上? 代码如下点击没发应
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SqlConnection con = new SqlConnection( "Server=infor_oracle;DataBase=db_blk;uid=xie;pwd=xie ");
con.Open();
SqlCommand cmd1 = new SqlCommand( "SELECT name as 姓名,ks as 科室名称,phone as 电话号码,address as 住址 from t_phone order by name ", con); //
SqlDataReader sdr1 = cmd1.ExecuteReader();
this.GridView1.DataSource = sdr1;
this.GridView1.DataBind();
sdr1.Close();
con.Close();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.GridView1.SelectedDataKey[ "姓名 "].ToString();
this.TextBox2.Text = this.GridView1.SelectedDataKey[ "科室名称 "].ToString();
this.TextBox3.Text = this.GridView1.SelectedDataKey[ "电话号码 "].ToString();
this.TextBox4.Text = this.GridView1.SelectedDataKey[ "住址 "].ToString();
}
设置DataKeyNames属性如: DataKeyNames= "姓名,科室名称,电话号码,住址 "
[解决办法]
try:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.GridView1.SelectedRow.Cells[0].Text;// "姓名 "所在的列号,假设为0,以下类推
this.TextBox2.Text = this.GridView1.SelectedRow.Cells[1].Text;
this.TextBox3.Text = this.GridView1.SelectedRow.Cells[2].Text;
this.TextBox4.Text = this.GridView1.SelectedRow.Cells[3].Text;
}
[解决办法]
TextBox1.Text = this.GridView1.SelectedRow.Cells[1].Text;
TextBox2.Text = this.GridView1.SelectedRow.Cells[2].Text;
[解决办法]
代码好像没有什么问题,
我自己的测试代码也是可以的
<%@ Page Language= "C# " %>
<%@ Import Namespace= "System.Data " %>
<%--http://community.csdn.net/Expert/TopicView3.asp?id=5657247--%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<script runat= "server ">
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Write(GridView1.SelectedDataKey[ "ProductID "].ToString());
Response.Write( " <br/> ");
Response.Write(GridView1.SelectedDataKey[ "ProductName "].ToString());
Response.Write( " <br/> ");
Response.Write(GridView1.SelectedDataKey[ "CategoryID "].ToString());
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
LoadProductData();
}
}
void LoadProductData()
{
DataTable dt = CreateSampleProductData();
GridView1.DataSource = dt;
GridView1.DataBind();
}
#region sample data
static DataTable CreateSampleProductData()
{
DataTable tbl = new DataTable( "Products ");
tbl.Columns.Add( "ProductID ", typeof(int));
tbl.Columns.Add( "ProductName ", typeof(string));
tbl.Columns.Add( "UnitPrice ", typeof(decimal));
tbl.Columns.Add( "CategoryID ", typeof(int));
tbl.Rows.Add(1, "Chai ", 18, 1);
tbl.Rows.Add(2, "Chang ", 19, 1);
tbl.Rows.Add(3, "Aniseed Syrup ", 10, 2);
tbl.Rows.Add(4, "Chef Anton 's Cajun Seasoning ", 22, 2);
tbl.Rows.Add(5, "Chef Anton 's Gumbo Mix ", 21.35, 2);
tbl.Rows.Add(47, "Zaanse koeken ", 9.5, 3);
tbl.Rows.Add(48, "Chocolade ", 12.75, 3);
tbl.Rows.Add(49, "Maxilaku ", 20, 3);
return tbl;
}
#endregion
</script>
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> Untitled Page </title>
<script>
</script>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:GridView ID= "GridView1 " DataKeyNames= "ProductID, ProductName, CategoryID " runat= "server " OnSelectedIndexChanged= "GridView1_SelectedIndexChanged " AutoGenerateSelectButton= "True ">
</asp:GridView>
</div>
</form>
</body>
</html>