Click here to Skip to main content
12,695,049 members (29,021 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: ASP.NET
greatful if can let me know:

How to get selected row value in GridView using ASP.net, C#
If I have command button(Template) field , by clicking on that,
I must able to get selected datakey value.
Thanks
Shahnawaz
Posted 10-May-10 0:23am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Please check below sample code:

HTML

<asp:GridView runat="server" ID="gvProjects" AutoGenerateColumns="false" OnRowCommand="gvProjects_RowCommand">
        <Columns>
            <asp:BoundField HeaderText="Projcet Name" DataField="ProjectName" />
            <asp:ButtonField ButtonType="Link" CommandName="Select" Text="Select" />
        </Columns>
    </asp:GridView>


Code behind

protected void gvProjects_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            // If multiple buttons are used in a GridView control, use the
            // CommandName property to determine which button was clicked.
            if (e.CommandName == "Select")
            {
                // Convert the row index stored in the CommandArgument
                // property to an Integer.
                int index = Convert.ToInt32(e.CommandArgument);

                // Retrieve the row that contains the button clicked 
                // by the user from the Rows collection.
                GridViewRow row = gvProjects.Rows[index];
                
            }
        }



Regards,
Jamil
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi!
I Have tried this code,May it Will help you

ASP Code
<asp:gridview id="gvdet" runat="server" autogeneratecolumns="False" xmlns:asp="#unknown">
    BackColor="White" BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" 
    CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" 
    GridLines="Horizontal" Height="271px" Width="346px"
    OnRowCommand="gvdet_sub" >


C# Code
 protected void gvdet_sub(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "cmdname")
            {
                int index = Convert.ToInt32(e.CommandArgument.ToString());

                string str = gvdet.Rows[index].Cells[0].Text.TrimEnd();
                string stnm = gvdet.Rows[index].Cells[1].Text.TrimEnd();
                string stadd = gvdet.Rows[index].Cells[2].Text.TrimEnd();
                
                ScriptManager.RegisterStartupScript(this, this.GetType(),
                "Message", "alert('Your Detail  is " + str + " "+stnm +" "+stadd +"');", true);
                
            }
            if (e.CommandName == "data1")
            {
                int i = Convert.ToInt32(e.CommandArgument.ToString());

                   
            }
            
        }
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

fgagds
<pre lang="text"><a href=""></a>
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170118.1 | Last Updated 16 Aug 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100