【问题标题】:How to get the Textbox value from DataGridView?如何从 DataGridView 获取文本框值?
【发布时间】:2013-05-28 13:29:41
【问题描述】:

所以在这里我试图让文本框填充来自 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和选择项目的按钮代码。

谢谢

【问题讨论】:

  • 这里有一些文档可以告诉你如何去做! msdn.microsoft.com/en-us/library/…。 (看底部的例子)
  • 有点帮助,但是DataGrid和Textbox位于不同的页面,有什么建议吗?
  • 文本框完全在不同的页面上?
  • 是的,假设数据网格在 data.aspx 中,当我单击文本框旁边的按钮(假设它是 textbox.aspx)时,它会弹出,当我单击数据网格,数据网格将关闭,值将放在上一页文本框中

标签: asp.net vb.net


【解决方案1】:

在您的网格视图上添加 OnRowCommand 事件:OnRowCommand="DGV_OnRowCommand"。

然后在你的代码后面添加这个:

    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;
        }
    }

【讨论】:

  • 我希望项目代码结果显示在文本框中,顺便说一句,我使用的是 VB :) 而且数据网格和文本框在不同的页面中
  • 谢谢你的回答,只有当DataGrid和Textbox在同一个页面时才能正常工作,但在我的情况下,DataGrid和Textbox所在的页面不同,有什么建议吗?
  • 嗯..你能告诉我你在弹出窗口中使用什么或者你是怎么做的吗?
猜你喜欢
  • 2012-06-04
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-28
  • 2011-04-14
  • 2021-12-12
相关资源
最近更新 更多