如何从DataGridView获取Textbox值?

时间:2013-05-28 03:34:17

标签: asp.net vb.net

所以这里我试图从DataGridView中填充所选数据的文本框,所以当我单击DataGridView中的按钮时,所选结果将被放入文本框中,我尝试了其他解决方案资源,但仍然没有运气。有人可以帮忙吗?这是DataGridView代码:

的DataGridView:

<asp:Panel ID="PanelDGV" runat="server" ScrollBars="None" Height="250" Width="515">
     <asp:GridView ID="DGV" runat="server" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" PageSize="8" CssClass="mGrid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt">
              <Columns>
     <asp:BoundField DataField="ProjectCode" HeaderText="Project Code" />
     <asp:BoundField DataField="ProjectName" HeaderText="Project Name" />
     <asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">
              <ItemStyle HorizontalAlign="Center"></ItemStyle>
                    </asp:ButtonField>
              </Columns>
                    <PagerStyle CssClass="pgr"></PagerStyle>
                    AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
      </asp:GridView>
</asp:Panel>

注意按钮是:

 <asp:ButtonField ButtonType="Image" ImageUrl="../Support/Image/Edit.png" ItemStyle-HorizontalAlign="Center" CommandName="CmdSearch" HeaderText="Edit">

哦,文本框的ID是“TbProjectCode”,两者都在单独的页面中,比如1.aspx包含文本框和一个用于打开datagridview的按钮,2.aspx包含datagridview和用于选择项目的按钮代码。

谢谢

1 个答案:

答案 0 :(得分:3)

在gridview上添加OnRowCommand事件:RowCommand =“DGV RowCommand”。

然后在您的代码后面添加:

    protected void DGV_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "CmdSearch")
        {
            int index = (int)e.CommandArgument;
            GridViewRow row = DGV.Rows[index];
            // Get the text on first cell of the row which is the project code.
            TbProjectCode.Text = row.Cells[0].Text;
        }
    }