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/




New to asp.net and struggling with a problem.

I have two gridviews on same page - If a record is selected in Gridview1, I need to unselect a record (if one is selected) in Gridview2 - and vice-versa.

Can this be done?



hi all

i want to use rowcommand in asp gridview

when i click on link button on any row , i want to retrieve value of the first column

for example i click on UserName = sam , then i want to get the ID of that user from the first column


can any one help me please :)


Is it possible to select a row without using the SELECT hyperlink/button besides I don't want to slect the entire row instead, i can select the row of the checked radio button?

Based on this example image, I just want to retrieve the data from column FROM, TO, FLIGHT and the selected radio button which is PHP 2899.00.


I am looking a way to set row attribut of the gridview at runtime. I want to display some rows in bold based on its view status (if it is not viewed) of that record from database.


Hi ,

    i am having gridview with two columns.one column contains checkboxes and another column contains Name.i have given UserId to DataKeyNames in gridview.Now i will check some checkboxes in gridview and click on button GetData. whenver i click on button GetData i need to get datakey values of all the gridview rows whatever checked in gridview using JQUERY

Please help me i am new to JQUery



I have a gird view,which has only one field displayed,and a text box. (Grid view has 2 data rows.)

On the gridview i have a button field named "Detailed View" which has command name "Detailed".

When i click on the row 1 i wanted to display the value on the text box

How will I execute it?

I have given the following code but its not giving me the expected result.
textbox1.Text = datagrid.Rows(e.CommandArgument).Cells(0).Text) and on the gridviews _RowCommand event

Its showing null

I am using VS 2008 (.net version 3.5) AND MS ACESS DB

In visual studio 2005(.net 2.0) the same code is working absolutely fine



Hi everybody,

I have a sql data base.

I use as filter for query a listbox in my asp.net page.

When i click on this listbox its displays one or 3 lines in a gridview.


I have 2 buttons in my gridview which enable editing or deleting.


My problem:

If i have A,B,C on the listbox. I select A which displays 1, 2, 3, 4 lines.

Then I click on the 1st line on edit button, it's allow editing the first line, that it okay.


But if I'm not clicking on update or cancel and I'm selecting the B on the list box which displays 1,2,3 lines.

The next selected line for the edition will be the 1st line from B.


How can I disable to store the selected line when I change the gridview data by selectings another value on my listbox.


I hope it's understandable


thanks for your help


best regards




I am trying to pull all of the associated data from a SharePoint list row to an InfoPath form repeating table.  I would like to select 1 unique value from the row and have the other fields auto-populate with the associated data from that row. 


SharePoint Columns and also names of InfoPath fields 1, 2, 3, 4, 5, 6, 7.

I have a dropdown that is connected to column 1 and I would like to select 1 and have it populate the rest of the fields from that row.  I am currently doing this with information and C# code from:  http://www.bizsupportonline.net/infopath2007/sharepoint-list-retrieve-value-same-row-repeating-table-infopath.htm I have also added conditional formatting to the repeating table that says if Column 1 in the repeating table doesn’t = the dropdown hide all other rows in the table so that only the row I asked for in the dropdown is visible.

Problem: I would like to be able to do math with this data I have selected/visible, currently it does not allow me to.  I would like to do a calculation like this; if 5= NO, set field 4 and field 7 to the product of 2*3. If 5=YES, set field 7 to 2*3=4+6. I may need to explain that better.

I think this http://msdn.microsoft.com/en-us/library/bb509311(office.12).aspx is trying to answer my question but I am not sure because most of the language is over my head as I have no programming experience. I just got lucky finding, copying, and pasting the code to get where I am.

I hope this makes sense to someone...




Hi All,

I have a datagrid in aspx page.Inside of datagrid i am using around 15 controls such as Button,dropdownlist and text box
controls.Once the datagrid binds then the events in the aspx page not firing.Issue occured when the number of rows
greater than 500.The number of rows less then its works fine.If anybody knows the solution please let me know.

"Platform i am working on ASP.NET1.1"




Problem sum data in gridview asp.net 2.0 by category


RAM            1

RAM            2

TOTAL       3

CPU            1

CPU            5

TOTAL       6

HDD           3

HDD           3

TOTAL      6


hi all,

my question is how to Display Asp.net Gridview  with data in Outlook mail item before sending the mail.

those who worked please share your ideas.

thanks in advance



hi all,

my question is how to display Asp.net Gridview Data in Outlook item before sending mail.

i am able to show data but it is not in same format as on the aspx page.

those who worked on it plz share your ideas.


thanks in advance



I have a grid view. When I click edit, the OnRowCommand fires and the row goes into edit state. Label becomes a TextBox. I then attempt to alter this value in some way and click update. This again fires the OnRowCommand event. The problem is, when I attempt to capture the new value, it instead just shows the original value.

In my grid view I have:


<asp:TemplateFieldHeaderText="Start Name">










<asp:TextBoxID="txtStartName"runat="server"Text='<%#Bind("StartName") %>'></asp:TextBox>





In my code behinde i have


textBox = (TextBox)row.Cells[4].FindControl("txtStartName");

Any help would be very appreciated.




I Using this code for delete a row from Grid.

<telerik:GridViewColumn Header="Delete">
							<Button Command="{Binding DeleteCommand, Source={StaticResource vmCriterion}}" CommandParameter="{Binding SelectedRow, Source={StaticResource vmCriterion}}">Delete</Button>                         

But, Imustchoose a Row, Before removing.
Iwantby clickingoneachrowis removed!

please help me.



This is the idea I had.  I created a Gridview which shows a few columns and all the records from a database I created but I want people to click the select button and be able to pull all the information (columns) about the record.  Now I have each record formatted in Formview on a separate page.  Can I somehow make is so when in Gridview they hit select on say record 14 that it takes them to record 14 in Formview as well? I'm using SharePoint Designer.  This seems a little complicated for a novice like me, so a simple selection of one record in Gridview would suffice, however I think my idea is good especially since the records in Formview are nice and formated already.

Any help is appreciated!



How to accomplish the below:

Whenever users selects a row in the gridview, "text" of a asp.net label control changes from 'A' to 'B'. 

Both label and gridview are on the same aspx page.


I have a gridview setup and have the select button enabled.  I also have a gridview that doesnt have a select button enabled..

On them both, if its possible, i would like to make the row itself selectable instead of just enabling the select button.



I thought this was easy but found the properties of GridView.SelectedPersistedDataKey are essentially read-only. I tried to create a new Datakey to no avail. Could anyone to pass a pointer to an example of how to set GridView.SelectedPersistedDataKey?


Can u tell me how can i hide selected rows from girdview using javascript.


Whenever a row is selected in my GridView, I want to take the ID field of my gridview's dataset and form a unique URL with it (use a ?id=1234 parameter). In the Page_Load event I have the following code:

TextBox1.Text = Request.Url.AbsoluteUri.ToString & "?id=" & _
GridView1.SelectedIndex.ToString & Environment.NewLine


The problem is the textbox's value is always the last one clicked on, not the current one. I load the page and click on the row with id 235 it gives me -1, I click on index 43 I get 235, I click on 65 it gives me 43, I click on 90 it gives me 65, etc. etc. So I probably put the code in the wrong event handler. If not Page_Load, then which one should I use?


<< Previous      Next >>

Microsoft   |   Windows   |   Visual Studio   |   Sharepoint   |   Azure