2011-06-26 33 views
0

如何在选择DataGrid时获取DataGrid行数据。例如,在DataGrid中选择一行时,我想显示关注TextBoxes的字段数据。请参考下面如何在文本框中获取DataGrid行数据

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

回答

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.So如果您使用DataGrid的需要设置选择CommandField中的网格,以便SelectedIdexChanged事件会火起来

Here是代码示例,一定会帮助你

+0

显示类似'System.Web.UI.WebControls.DataGrid'的错误消息不包含'SelectedRow'的定义,也没有 扩展方法'SelectedRow'接受类型为'System.Web.UI.WebControls.DataGrid'的第一个参数 '可以找到(你是否缺少使用指令或程序集引用?)' –

+0

在此事件定义中使用保护无效并请让我知道 – Syeda

+0

我是usin g protected void,参见下面的代码,'protected void Grid_SelectedIndexChanged(object sender,EventArgs e) { //使用SelectedRow属性获取当前选中的行。 GridViewRow row = Grid.SelectedRow; //在这里填写文本框 tbCode.Text = row.Cells [0] .Text; }' –

0

您可以在每一行的前面用java脚本that..put复选框,然后在选择指数的代码片段改变该检查哪些行选择和获取数据,并在您需要的文本框显示它..

相关问题