Home » Asp.net

ASP.NET,C#: retrieve data from gridview row selected

Hi dude,

Does any body know how to retrieve data from gridview on row selectindexchanged??




12 Answers Found


Answer 1

In the SelectedIndexChanged event you can access the SelectedRow. On the SelectedRow you can get the values you're looking for. If they are BoundFields then you could do SelectedRow.Cells[n].Text and IF they're TemplateFields you can use FindControl, something like

TextBox tbox = (TextBox)SelectedRow.FindControl("TextBox1");


Answer 2

The SelectedIndex can be used to look for the Row in the Rows collection. You can then use FindControl to find Controls in it.
If you have DataKeyNames set, you can also get the DataKeys for SelectedIndex.
If you want to extract values like an Update would, you will have to iterate over the Cells of the Row. Find the ones that are DataControlFieldCell. These will have a ContainingField, that will be a DataControlField. You can use that to ExtractValuesFromCell on the cell, into an OrderedDictionary.

Answer 3

If you want to retrieve the selected row data cells, try using the following way:

        if (GridView1.SelectedIndex != -1)
            GridViewRow row = GridView1.SelectedRow;
            string cell1 = row.Cells[1].Text; //I use '1' because the '0' index is the SELECT command text of my GridView
            string cell2 = row.Cells[2].Text; // second cell and so on...

This code should be added inside GridView SelectedIndexChanged.



Answer 4

Thanks guys for your respond.

I already try with this code below but it doesn't work.

I want to click any row of gridview to show data in the textbox .

protected void grdProProcess_SelectedIndexChanged(object sender, EventArgs e)
            txtProcessOrder.text= grdProProcess.SelectedRow.Cells[4].Text;
            txtProProcess.text = grdProProcess.SelectedRow.Cells[6].Text;
            txtAuthorise.text = grdProProcess.SelectedRow.Cells[5].Text;



Answer 5

Normally, the SelectedIndexChanged will only "work" when you have defined a button with CommandName="Select" - with that you can click any "Select" (on any row) and you will get that code to work.

Now, let me think ahead, you're thinking that you can click anywhere on the row and get that code to work?

IF yes, that will require more of code to wire up. Follow the code:

//in the RowCreated event do this:

protected void CustomersGridView_RowCreated(object sender, GridViewRowEventArgs e)
            if (e.Row.RowType == DataControlRowType.DataRow)
                e.Row.Attributes.Add("onclick",ClientScript.GetPostBackEventReference(CustomersGridView,"Select$" + e.Row.RowIndex.ToString()));

and override the render (keep the code as is by itself on the codebehind):

protected override void Render(HtmlTextWriter writer)
            for (int i = 0; i < CustomersGridView.Rows.Count; i++)
                Page.ClientScript.RegisterForEventValidation(CustomersGridView.UniqueID, "Select$" + i);

change the name of the gridview to your gridviewid


Answer 6

Dear Petenet,

Your solution was work,but if i use ClientScript.GetPostBackEventReference my gridview will refresh.

so my ItemTemplate button in gridview will not work.

for example as Figure 1. I want to select Name Colum to show Name in the textbox and if click Button PopUp,it will show popup. If I use ClientScript.GetPostBackEventReference my popup will be popup and disapear immediatly.

Figure 1.

Button PopUP
Button PopUPMr.B
Button PopUPMr.C
Button PopUPMr.D


Answer 7

I think you should use a linkbutton for the Name column and set its CommandName="Select" and Text='<%#  Eval("Name") %>'  and then the SelectedIndexChanged will fire and you should be able to get the values as explained earlier. ...and show you popup etc.


Answer 8

Dear PeteNet,

Yes,if use link button it's will work but can we have other way not to use it??

Coz the user will feel not comfort if the colum name is link button.




Answer 9

so you're saying it is OK to have "Button PopUP" as linkbutton? that should be fine. The important fact here is the CommandName, which has to be "Select" for the event to fire.

can we have other way not to use it??

I'm not quite sure what you mean: like without a button, using the second method (using GetPostBackEventReference)? that should have worked too but we can't see your markup and don't know exactly why it didn't work.


Answer 10

Dear PeterNet,

I've got it.Thanks you so much for your help.




Answer 11



Would you have that code in VB instead of C#, getting lots a wavy lines



Answer 12

Would you have that code in VB instead of C#, getting lots a wavy lines

there's quite a few code converters online, here's one: http://converter.telerik.com/



<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure