在选择gridview行时将gridview行数据传递到文本框

时间:2016-02-24 17:20:15

标签: c# asp.net gridview textbox gridviewrow

我似乎无法将我的gridview数据传递到我选择的gridview之外的文本框。该事件似乎正在解雇,但文本框总是充满了& NBSP ;.我无法弄清楚为什么会发生这种情况,因为gridview中的所有单元格都不是空白。我已经在这个网站和其他网站上研究了这个问题几天,没有运气。这是我的aspx.cs:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    txtBLName.Text = GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
}

这是我的gridview代码:

<asp:GridView ID="GridView1"
    runat="server"
    AutoGenerateColumns="False"
    onselectedindexchanged="GridView1_SelectedIndexChanged" >
    <Columns>
        <asp:CommandField ShowSelectButton="true" SelectText="Edit" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName"/>
     </Columns>
</asp:GridView>

有人可以帮忙吗?谢谢!

1 个答案:

答案 0 :(得分:0)

正在工作

ASP代码

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 <asp:GridView ID="GridView2"
runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="GridView2_SelectedIndexChanged" >
<Columns>
    <asp:CommandField ShowSelectButton="true" SelectText="Edit" />
    <asp:BoundField DataField="LastName" HeaderText="Last Name" SortExpression="LastName"/>
 </Columns>

C#CODE

protected void Page_Load(object sender, EventArgs e)
{

    DataTable oDT = new DataTable();

    oDT.Columns.Add("edit",typeof(string));
    oDT.Columns.Add("LastName", typeof(string));

    oDT.Rows.Add("ad", "1212121");
    oDT.Rows.Add("aad", "1asdasd212121");

    GridView2.DataSource = oDT;
    GridView2.DataBind();      

}

protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView2.SelectedRow;
    TextBox1.Text = row.Cells[1].Text;
}

<强> RESULT

enter image description here