How do I get the DataGrid row data while selecting a DataGrid. For example, while selecting a row in a DataGrid , I want to show that field data in concern TextBoxes. Please see the code snippet below
<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>
updated code
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>
Use GridViewSelection Event for that
Oh yeah this code is for grid.So if you are using datagrid You need to Set the Select CommandField in the Grid so that the SelectedIdexChanged event will fire up
Here is code sample that will definitely help you