日期:2014-05-18  浏览次数:20446 次

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 "-//W3