【问题标题】:How to get the DataGrid row data in Textboxs如何在文本框中获取 DataGrid 行数据
【发布时间】:2011-06-26 05:11:37
【问题描述】:

如何在选择 DataGrid 时获取DataGrid 行数据。例如,在 DataGrid 中选择一行时,我想在关注的 TextBoxes 中显示该字段数据。请看下面的代码sn-p

    <asp:TextBox ID="tbCode" runat="server" TabIndex="0"                        
                    style="width: 82px; position: absolute; top: 16px; left: 63px; width: 88px; height: 24px;" 
                    MaxLength="5"></asp:TextBox>
                <asp:Label ID="lblCode" runat="server" Text="Code"                          
                    style="width: 45px; position: absolute; top: 18px; left: 18px; height: 22px;">
 </asp:Label>
            <asp:TextBox ID="tbCode" runat="server" TabIndex="0"                        
                    style="width: 82px; position: absolute; top: 16px; left: 63px; width: 88px; height: 24px;" 
                    MaxLength="5"></asp:TextBox>
                <asp:Label ID="lblCode" runat="server" Text="Code"                          
                    style="width: 45px; position: absolute; top: 18px; left: 18px; height: 22px;">
</asp:Label>

   <asp:DataGrid ID="Grid" runat="server" AllowPaging="True" DataKeyField="Team_Id"
        AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" 
        OnPageIndexChanged="Grid_PageIndexChanged" OnCancelCommand="Grid_CancelCommand"
        style="position:absolute; top: 122px; left: 13px; width: 545px; bottom: 238px;"
        OnDeleteCommand="Grid_DeleteCommand" OnEditCommand="Grid_EditCommand"             

        OnUpdateCommand="Grid_UpdateCommand" TabIndex="3" BorderStyle="Solid" 
        BorderWidth="1">

    <Columns>
        <asp:BoundColumn HeaderText="Code" DataField="Team_Code" ReadOnly="true"> </asp:BoundColumn>
        <asp:BoundColumn HeaderText="Team Name" DataField="Team_Name"> </asp:BoundColumn>
        <asp:EditCommandColumn  EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"> </asp:EditCommandColumn>
        <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"> </asp:ButtonColumn>            
    </Columns>          
    </asp:DataGrid>

更新代码

    protected void GetSelectedData(Object src, EventArgs e)
{
    String TeamId = Grid.DataKeys[Grid.SelectedIndex].ToString();
    using (OdbcConnection DbConnection = new OdbcConnection(ConfigurationManager.AppSettings["ConnectionStr"]))
    {
        DbConnection.Close();
        string cmdText = "SELECT Team_Code,Team_Name FROM team_details WHERE Team_Id=?";
        OdbcCommand cmd = new OdbcCommand(cmdText, DbConnection);
        cmd.Parameters.Add("?Id", OdbcType.Int).Value = Convert.ToInt32(TeamId);
        DbConnection.Open();
        OdbcDataReader DR = cmd.ExecuteReader();
        while (DR.Read())
        {
            tbCode.Text = DR.GetValue(0).ToString();
            tbName.Text = DR.GetValue(1).ToString();
        }

    }
}

  <asp:DataGrid ID="Grid" runat="server" AllowPaging="True" DataKeyField="Team_Id"
        AutoGenerateColumns="False" CellPadding="2" ForeColor="#333333" GridLines="None" 
        OnSelectedChanged="GetSelectedData" OnCancelCommand="Grid_CancelCommand"
        style="position:absolute; top: 122px; left: 13px; width: 545px; bottom: 238px;"
        OnDeleteCommand="Grid_DeleteCommand" OnEditCommand="Grid_EditCommand"               
        OnUpdateCommand="Grid_UpdateCommand" TabIndex="3" BorderStyle="Solid" 
        BorderWidth="1">
    <Columns>
        <asp:BoundColumn HeaderText="Code" DataField="Team_Code" ReadOnly="true"> </asp:BoundColumn>
        <asp:BoundColumn HeaderText="Team Name" DataField="Team_Name"> </asp:BoundColumn>
        <asp:EditCommandColumn  EditText="Edit" CancelText="Cancel" UpdateText="Update" HeaderText="Edit"> </asp:EditCommandColumn>
        <asp:ButtonColumn CommandName="Delete" HeaderText="Delete" Text="Delete"> </asp:ButtonColumn>            
        <asp:TemplateColumn>
            <ItemTemplate>
                <asp:LinkButton runat="server" CommandName="select" text="select team" />                    
            </ItemTemplate>
        </asp:TemplateColumn> 
    </Columns>
  </asp:DataGrid>

【问题讨论】:

    标签: c# asp.net datagrid


    【解决方案1】:

    为此使用 GridViewSelection 事件

        OnSelectedIndexChanged="Grid_SelectedIndexChanged"
    
    
       void Grid_SelectedIndexChanged(Object sender, EventArgs e)
       {
    
         // Get the currently selected row using the SelectedRow property.
          GridViewRow row = Grid.SelectedRow;
    
         //Fill textboxes here
         Code.Text =  row.Cells[0].Text;
    
        }
    

    哦,是的,这段代码是用于网格的。因此,如果您使用的是数据网格,则需要在网格中设置 Select CommandField,以便触发 SelectedIdexChanged 事件

    Here 是绝对可以帮助您的代码示例

    【讨论】:

    • 显示类似System.Web.UI.WebControls.DataGrid' does not contain a definition for 'SelectedRow' and no extension method 'SelectedRow' accepting a first argument of type 'System.Web.UI.WebControls.DataGrid' could be found (are you missing a using directive or an assembly reference?)的错误消息
    • 在此事件定义中使用受保护的 void,请告诉我
    • 我正在使用受保护的 void,请参阅下面的代码,protected void Grid_SelectedIndexChanged(object sender, EventArgs e) { // Get the currently selected row using the SelectedRow property. GridViewRow row = Grid.SelectedRow; //Fill textboxes here tbCode.Text = row.Cells[0].Text; }
    • 我使用的是 DataGrid 而不是 GridView 组件。
    • 是的,我的错误我很抱歉。我给了你一个链接,读出来我发现它对数据网格很有帮助。在数据网格中,我们必须应用 CommandField 来获取行......如果你的问题得到解决,请告诉我......
    【解决方案2】:

    您可以为此使用 java 脚本..将复选框放在每行前面,然后在更改的选定索引上检查选择了哪一行并获取数据并将其显示在您想要的文本框中..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-17
      • 1970-01-01
      • 2015-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-22
      相关资源
      最近更新 更多