如何在文本框中获取DataGrid行数据
本文关键字:获取 DataGrid 数据 文本 | 更新日期: 2023-09-27 18:00:19
选择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>
对该使用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事件将启动
这是一个代码示例,它肯定会帮助您
您可以使用java脚本来实现这一点。。将复选框放在每一行的前面,然后在选中的索引上选中哪一行,获取数据并将其显示在所需的文本框中。。