Gop*_*pal 5 c# asp.net gridview
使用C#和Mysql
在我的网页上使用gridview,如果我点击girdview中的列,该值应显示在文本框中.
例如
Griview
Column1 column2 Column3
1 Raja 9876
2 Ravi 7890
3 Ramu 9879
...
Run Code Online (Sandbox Code Playgroud)
如果我单击2行,则所有值都应显示在文本框中
Textbox1.text = 2
textbox2.text = Ravi
textbox3.text = 9879
...,
Run Code Online (Sandbox Code Playgroud)
如何为这种情况编写代码.
需要C#代码帮助
我假设通过声明“ [...]单击 2 行[...] ”,您实际上的意思是“单击第二行”;至少,这是你的最后一个代码片段所建议的,因为它只显示第二行的值(顺便说一句:ID在那里是错误的;它应该是7890
)。
以下代码片段显示了GridView
允许选择单行,并使用代码隐藏中的事件处理程序将每个文本设置TextBox
为所选行中的相应值:
页面.aspx:
<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>
Run Code Online (Sandbox Code Playgroud)
代码隐藏文件Page.aspx.cs中的事件处理程序:
void gridview_SelectedIndexChanged(object sender, EventArgs e)
{
var grid = sender as GridView;
if (grid == null) return;
//Cell[0] will be the cell with the select button; we don't need that one
Textbox1.Text = grid.SelectedRow.Cell[1].Text /* 2 */;
Textbox2.Text = grid.SelectedRow.Cell[2].Text /* Ravi */;
Textbox3.Text = grid.SelectedRow.Cell[3].Text /* 7890 */;
}
Run Code Online (Sandbox Code Playgroud)