如何在Textbox中获取DataGrid行数据

时间:2011-06-26 05:11:38

标签: c# asp.net datagrid

如何在选择DataGrid时获取DataGrid行数据。例如,在DataGrid中选择一行时,我想显示与TextBoxes相关的字段数据。请参阅下面的代码段

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

2 个答案:

答案 0 :(得分: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;

    }

哦,是的,这个代码是针对grid的。如果你正在使用datagrid你需要在Grid中设置Select CommandField,以便启动SelectedIdexChanged事件

Here是代码示例,肯定会帮助您

答案 1 :(得分:0)

你可以使用java脚本在每行前面的那个..put复选框,然后在所选的索引上更改,检查选择哪一行并获取数据并将其显示在你想要的文本框中。