Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a gridview there I placed some bound field and also added an image button in the last column of the grid as template field.

What I want is when I click on the image button my page should redirect to a new page with one of column values, say the id of the row where I clicked.

I can use session or query string for redirecting but how to get the row values?

I tried in many ways but I get a null reference error.
I'm unable to get the selected row using an image button.

How can I code the image button to select a row and get its values?
Please help me.
Thanks in advance...
Posted
Updated 8-Feb-18 1:02am
v3
Comments
Dalek Dave 26-Aug-10 4:35am    
Edited for Readability, Grammar and Syntax.

go to selection changing event and write the code below:
Selection changeing Event

int index=gridvview.selectedindex;
session["id"]=(gridview.rows[index].cells[0].text).tostring();

in this way you get all your cell's values from selected row
 
Share this answer
 
v3
Comments
aswathy.s.88 26-Aug-10 4:29am    
Thanks for your help but this is not working....
Ziee-M 8-Feb-18 7:38am    
sharmarun solution is if i may say, is the standard way to get your row index. There are 2 informations not clear thought. This code goes in the OnSelectedIndexChanged event. And you have to add the event in the markup : <asp:GridView ID="GridView" Runat="server" OnSelectedIndexChanged="yourSelectedIndexChangeEventName" AutoPostBack="True"/>
AutoPostBack must be set to true, to allow a post back when the selected row is changed.
Try exploring more on this MSDN Article[^].

Basically, you need to get the index of the row where the button is located. You can then get the specific row using that index:

GridViewRow row = ContactsGridView.Rows[index];

You can then get the content of the column that you want, for example:

String s = row.Cells[2].Text;

The variable s will have the value of third cell of the selected row (where your button was clicked) of you gridview.

There is an example on the link I shared.
 
Share this answer
 
One way is to add Click event for that image button and get the row as,

GridviewRow gvRow = (sender as ImageButton).NamingContainer as GridviewRow;


and then access the DataKey or ID which you want to pass.
PS: Code is not tested.

Alternatively you can use RowCommand.
 
Share this answer
 
Set the DataKeyNames of the grid view of the column name (say the ID from the DB which is in the Result set).

Now in the SelectedIndexChanged event handler writ the following code

SQL
int selectedIndex = GridView1.SelectedIndex;
string customID = (GridView1.DataKeys[selectedIndex]["pkCityId"]).ToString();
//now write Ur custom logic for creating the URL and redirecting to the
//new url


simple isn't ???:cool:

For clarity the name of my Gridview is GridView1 and the DataKeyNames (the column which actually holds the ID value) is pkCityID.
 
Share this answer
 
v2
Set commandArgument like this
XML
<asp:ImageButton ID="ibtnEdit"  runat="server" CommandArgument='<%# Container.DataItemIndex+1 %>' />

then in gridview rowcommand event
VB
dim intRow as integer = CInt(e.CommandArgument)

then by using that row number u can get the row values
VB
gridview.Rows(intRow).Cells (0).Text
 
Share this answer
 
On image button click

GridViewRow selectedRow = ((ImageButton)sender).Parent.Parent as GridViewRow;
        ImageButton imageButton = (ImageButton)selectedRow.FindControl("imgBtn");
          if (imageButton != null)
          {
            //add your logic here
          }
 
Share this answer
 
Comments
usmanmirza191 24-Jan-16 5:32am    
Good One!!
Hi Friends!

Here's a way to get the values of each cell in a Grids' row (in case of binding data from code).
Consider a Button Field ('Select') in this case and give its CommandName to Select
Write in Row_Command event
C#
if (e.CommandName == "select")
 { 
  //code here
}
 
Share this answer
 
v3
Write this code in selected_index_changed function defile datakey property of gridview then write below code:
Session["updaterec"] = GridView1.SelectedDataKey [0].ToString ();
Response.Redirect("your desire page");
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900