how to get a id of the row in which a button is clicked

Last post 07-08-2009 4:32 AM by Qin Dian Tang - MSFT. 12 replies.

Sort Posts:

  • how to get a id of the row in which a button is clicked

    07-03-2009, 12:39 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

    Hi all,

     I have a nested grid view, in which child grid is below(like collapse and extend) the parent one.

    Now in child gridview has columns with id as datakey , image button,etc.

    When i clicked in a image button in child grid i want to get the row id,

    Can some one help me ? 

  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 3:35 AM
    • All-Star
      91,768 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    Try:

    protected void ImageButton1_Click(object sender, EventArgs e)
    {
            ImageButton b = (ImageButton)sender;
            GridViewRow row = (GridViewRow)b.NamingContainer;
            if (row != null)
            {
                 int rowIndex = row.RowIndex;
                //do something 
            }
    
    } 


    "Code,Beer and Music ~ my way of being a programmer"

  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 3:51 AM
    • Participant
      1,391 point Participant
    • manojlev
    • Member since 09-08-2008, 10:43 AM
    • Mumbai
    • Posts 262

     Hi,


    Globally declare your child GridView as follows

    GridView childGrid;

    In ParentGridviews DataBound event find out the childGrid control

    as follows :

     protected void ParentGrid_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                childGrid = (GridView)e.Row.FindControl("childGrid");           
            }
        }

     protected void childGrid_RowDataBound(object sender, GridViewRowEventArgs e) {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                string rowid = childGrid.DataKeys[e.Row.RowIndex].Values[0].ToString();
         // use this rowid for your further process of code.
     }
    }

     

    hope this may help u.

    Thank You
    Manoj Chavan
    -------------------------------------------
    Web Application Developer(C#.NET, ASP.NET, C++.NET)
    My Blog
    My Site
    Mauli softwares
    Always think high, Always Dream High.
    If this post helps you then plz Mark It as Answer
  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 5:12 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

     

    Thanks vinz, Bur i am getting an error while using that row value.

     

    string

     

    s = ((DataRowView)row.DataItem)["ID"].ToString();

    error::

    Object reference not set to an instance of an object.

    Can u help me further to get that grid view row?

     

  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 5:19 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

     thanx manoj,

      But i don't have childGrid_RowDataBound , as i am binding at ParentGrid_RowDataBound( the child grid,

    So how can i do this in button click event?

  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 5:22 AM
    • All-Star
      91,768 point All-Star
    • vinz
    • Member since 10-05-2007, 3:47 PM
    • Cebu, Philippines
    • Posts 13,769
    • TrustedFriends-MVPs

    gaurnitai:

    Thanks vinz, Bur i am getting an error while using that row value.



    string

     

    s = ((DataRowView)row.DataItem)["ID"].ToString();

    error::

    Object reference not set to an instance of an object.

    Can u help me further to get that grid view row?

    Try to do something like this:

    protected void ImageButton1_Click(object sender, EventArgs e)
    {
            ImageButton b = (ImageButton)sender;
            GridViewRow row = (GridViewRow)b.NamingContainer;
            if (row != null)
            {
     	     //Get the Row Index
                 int rowIndex = row.RowIndex;
                 //Get the DataKey value
                 string key = GridView1.DataKeys[rowIndex].Value.ToString();
                 //Get the Row value of BoundField
                 string rowValue = row.Cell[0].Text;
                 
            }
    
    } 




    "Code,Beer and Music ~ my way of being a programmer"

  • Re: how to get a id of the row in which a button is clicked

    07-03-2009, 7:41 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

    vinz:

    gaurnitai:

    Thanks vinz, Bur i am getting an error while using that row value.



    string

     

    s = ((DataRowView)row.DataItem)["ID"].ToString();

    error::

    Object reference not set to an instance of an object.

    Can u help me further to get that grid view row?

    Try to do something like this:

     

    1. protected void ImageButton1_Click(object sender, EventArgs e)   
    2. {   
    3.         ImageButton b = (ImageButton)sender;   
    4.         GridViewRow row = (GridViewRow)b.NamingContainer;   
    5.         if (row != null)   
    6.         {   
    7.          //Get the Row Index   
    8.              int rowIndex = row.RowIndex;   
    9.              //Get the DataKey value   
    10.              string key = GridView1.DataKeys[rowIndex].Value.ToString();   
    11.              //Get the Row value of BoundField   
    12.              string rowValue = row.Cell[0].Text;   
    13.                 
    14.         }   
    15.   
    16. }   




     

     

    The how to get the child gridview id, Because it is nested inside the  Item Template..

  • Re: how to get a id of the row in which a button is clicked

    07-04-2009, 4:50 AM
    • Participant
      1,391 point Participant
    • manojlev
    • Member since 09-08-2008, 10:43 AM
    • Mumbai
    • Posts 262

     hi,
    you can write as follows :

     protected void btn1_Click(object sender, EventArgs e)
        {
           foreach(GridViewRow row in ParentGrid.Rows){
            	childGrid = (GridView)ParentGrid.Rows[row.RowIndex].FindControl("childGrid");   
    	   	foreach(GridViewRow row1 in childGrid.Rows)
            	{
                    	int code = Convert.ToInt32(chilGrid.DataKeys[row1.RowIndex].Values[0].ToString());                     
    			// write your code here whatever u want
            	}
    	}
        }
     
    I hope this may help u.



     

    Thank You
    Manoj Chavan
    -------------------------------------------
    Web Application Developer(C#.NET, ASP.NET, C++.NET)
    My Blog
    My Site
    Mauli softwares
    Always think high, Always Dream High.
    If this post helps you then plz Mark It as Answer
  • Re: how to get a id of the row in which a button is clicked

    07-04-2009, 5:25 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

    manojlev:

     hi,
    you can write as follows :

    1. protected void btn1_Click(object sender, EventArgs e)   
    2.    {   
    3.       foreach(GridViewRow row in ParentGrid.Rows){   
    4.         childGrid = (GridView)ParentGrid.Rows[row.RowIndex].FindControl("childGrid");      
    5.     foreach(GridViewRow row1 in childGrid.Rows)   
    6.         {   
    7.                 int code = Convert.ToInt32(chilGrid.DataKeys[row1.RowIndex].Values[0].ToString());                        
    8.         // write your code here whatever u want   
    9.         }   
    10. }   
    11.    }  
    1. I hope this may help u.  



     

     

     

    Thanks sir foe ur reply, But i want that perticuler child row only , Is it possible to get that one child row only?

  • Re: how to get a id of the row in which a button is clicked

    07-04-2009, 6:11 AM
    • Participant
      1,391 point Participant
    • manojlev
    • Member since 09-08-2008, 10:43 AM
    • Mumbai
    • Posts 262

     Hi,

       You can write above code as follows

       

  • protected void btn1_Click(object sender, EventArgs e)   
  •    {   
  •       foreach(GridViewRow row in ParentGrid.Rows){   
  •         childGrid = (GridView)ParentGrid.Rows[row.RowIndex].FindControl("childGrid");      
  •       }

             foreach(GridViewRow row1 in childGrid.Rows)   

  •         {   
  •                 int code = Convert.ToInt32(chilGrid.DataKeys[row1.RowIndex].Values[0].ToString());                        
  •         // write your code here whatever u want   
  •         }   
  •    }  
  • OR ELSE TRY MY FIRST SUGGESTED CODE FOR DATA BIND EVENT BY ADDING YOUR CHILDGRIDS EVENT TO .aspx.cs

    PAGE.

     

     

    I hope it helps u.

     

     

    Thank You
    Manoj Chavan
    -------------------------------------------
    Web Application Developer(C#.NET, ASP.NET, C++.NET)
    My Blog
    My Site
    Mauli softwares
    Always think high, Always Dream High.
    If this post helps you then plz Mark It as Answer
  • Re: how to get a id of the row in which a button is clicked

    07-04-2009, 6:19 AM
    • Member
      454 point Member
    • Deeno20
    • Member since 03-25-2009, 7:21 AM
    • Kumavat
    • Posts 245

    this will give u the valu of unique ID which u can save in to session or any varriable which u can use later

    write this code under SelectedIndexChanging

    childgridview1.Rows(e.NewSelectedIndex).Cells.Item(coloumn number).Text

    Happy TO Help
  • Re: how to get a id of the row in which a button is clicked

    07-04-2009, 6:56 AM
    • Member
      11 point Member
    • gaurnitai
    • Member since 06-01-2009, 12:32 PM
    • Posts 30

    manojlev:

     Hi,

       You can write above code as follows

       

  • protected void btn1_Click(object sender, EventArgs e)   
  •    {   
  •       foreach(GridViewRow row in ParentGrid.Rows){   
  •         childGrid = (GridView)ParentGrid.Rows[row.RowIndex].FindControl("childGrid");      
  •       }

             foreach(GridViewRow row1 in childGrid.Rows)   

  •         {   
  •                 int code = Convert.ToInt32(chilGrid.DataKeys[row1.RowIndex].Values[0].ToString());                        
  •         // write your code here whatever u want   
  •         }   
  •    }  
  • OR ELSE TRY MY FIRST SUGGESTED CODE FOR DATA BIND EVENT BY ADDING YOUR CHILDGRIDS EVENT TO .aspx.cs

    PAGE.

     

     

    I hope it helps u.

     

     

     

    sir according to ur code child grid will correspond to the last trow of main grid not the one that i am desired,

    Because child grid can be of any main row...

  • Re: how to get a id of the row in which a button is clicked

    07-08-2009, 4:32 AM
    Answer

    Hi gaurnitai,

    You have a ImageButton in the child GridView, you want to get the id of the clicked row in this child GridView, right. Then you can bind CommandArgument to ID field and handle its Click event to get:

    <asp:ImageButton runat="server" ID="ImageButton1" Text="Check" CommandArgument='<%# Eval("ID") %>' OnClick="ImageButton1_Click" />

    protected void ImageButton1_Click(object sender, EventArgs e)
        {
            string id = ((ImageButton)sender).CommandArgument;
            GridViewRow gvr = (GridViewRow)((ImageButton)sender).Parent.Parent;
            int row_index = gvr.RowIndex;
        }

    Thanks,

     

    Qin Dian Tang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (13 items)