How to get text boxes for Gridview Row values?

我是研究僧i 提交于 2019-12-23 10:27:01

问题


Using C# & Mysql

In my webpage am using gridview, if i click the column in the girdview the value should display in the textbox.

For Example

Griview

Column1 column2 Column3

    1 Raja 9876
    2 Ravi 7890
    3 Ramu 9879
    ...

If i click the 2 rows all the values should display in the textbox

Textbox1.text = 2
textbox2.text = Ravi
textbox3.text = 9879
...,

How to write a code for this condition.

Need C# code Help


回答1:


I'm assuming that by stating "[...]click the 2 rows[...]" you actually mean "click the 2nd row"; at least, this is what your last snippet suggests, since it shows only the values of the 2nd row (on a little side note: the ID's wrong there; it should be 7890).

The following code snippet shows a GridView which allows the selection of a single row, and uses an event handler in the code-behind to set the text of each TextBox to the according value in the selected row:

Page.aspx:

<asp:GridView runat="server" ID="gridView" OnSelectedIndexChanged="gridview_SelectedIndexChanged" AutoGenerateSelectButton="true"></asp:GridView>

Event handler in the code-behind file 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 */;
}



回答2:


You can use an EditItemTemplate for this.

See the CodeProject article, Editable Gridview with Textbox, CheckBox, Radio Button and DropDown List.



来源:https://stackoverflow.com/questions/3413830/how-to-get-text-boxes-for-gridview-row-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!