简体   繁体   中英

How to get text boxes for Gridview Row values?

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

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 */;
}

You can use an EditItemTemplate for this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM