locked
Trying to get Selected Row Index from Gridview RRS feed

  • Question

  • User2103134756 posted

    I have a form with buttons and a gridview. The buttons are create, save, and cancel. On the gridview, I have a select and a delete linkbutton. I want all commands to go to a single method called FormCommands. I can check if there is a button or a linkbutton clicked but can't seem to get the Gridview row index that was clicked. I just get the -1. I realize the four dot operators that refer to the parent control is probably not the best way to get the gridview reference but it doesn't give me an error. It also doesn't give me the index. So, here' is the partial code. Any ideas would be appreciated.

    protected void FormCommands(object sender, EventArgs e)
        {
            //Init control variables
            LinkButton lbGridView = new LinkButton();
            Button cmdButton = new Button();
            GridView gridView = new GridView();
            int selectedRowIndex = -1;
            object senderObj;
            string senderType;
    
            //Init command argument text
            string command = string.Empty;
    
            //Get type from sender and 
            //convert to string
            senderObj = sender.GetType();
            senderType = senderObj.ToString();
    
            //if sender is a linkbutton
            if (senderType.Contains("LinkButton"))
            {
                //Get control references
                lbGridView = (LinkButton)sender;
                gridView = (GridView)lbGridView.Parent.Parent.Parent.Parent;
    
                //Get selected index of gridview and command arg
                selectedRowIndex = gridView.SelectedIndex;
                command = lbGridView.CommandArgument;
            }
            //if sender is a command button
            else if (senderType.Contains("Button"))
            {   
                //Get button reference and command arg
                cmdButton = (Button)sender;
                command = cmdButton.CommandArgument;
            }
    
    ... rest of method

    Thursday, June 13, 2019 5:38 PM

Answers

  • User288213138 posted

    Hi oneillj,

    The zero-based index of the selected row in a GridView control. The default is -1, which indicates that no row is currently selected.

    If you want to get the GridView Selected row index, you can get it by the following method.

    The code:

    Aspx:
    <div>
                <asp:GridView ID="GridView1" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="FormCommands">LinkButton</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="FormCommands" />
            </div>
    
    Aspx.cs:
     if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "John Hammond", "United States");
                    dt.Rows.Add(2, "Mudassar Khan", "India");
                    dt.Rows.Add(3, "Suzanne Mathews", "France");
                    dt.Rows.Add(4, "Robert Schidner", "Russia");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    protected void FormCommands(object sender, EventArgs e)
            {
                //Init control variables
                LinkButton lbGridView = new LinkButton();
                Button cmdButton = new Button();
                GridView gridView = new GridView();
                int selectedRowIndex = -1;
                object senderObj;
                string senderType;
    
                //Init command argument text
                string command = string.Empty;
    
                //Get type from sender and 
                //convert to string
                senderObj = sender.GetType();
                senderType = senderObj.ToString();
    
                //if sender is a linkbutton
                if (senderType.Contains("LinkButton"))
                {
                    //Get control references
                    lbGridView = (LinkButton)sender;
                    gridView = (GridView)lbGridView.Parent.Parent.Parent.Parent;
                    //Get selected index of gridview and command arg
                    //selectedRowIndex = gridView.SelectedIndex;
                    //command = lbGridView.CommandArgument;
                    var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
    
                    Response.Write(rowIndex.ToString());
    
                }
                //if sender is a command button
                else if (senderType.Contains("Button"))
                {
                    //Get button reference and command arg
                    cmdButton = (Button)sender;
                    command = cmdButton.CommandArgument;
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 14, 2019 6:24 AM

All replies

  • User-1847850515 posted

    hi, 

    you can make a method in dgv to save index of selected line

    method in onRowCommand

    you'll have 

    protected void GVEXAMPLE_RowCommand(object sender, GridViewCommandEventArgs e)
    {
    }

    and in you can put 

    index = Convert.ToInt32(e.CommandArgument);

    you declare int index before load and it'll be ok

    Thursday, June 13, 2019 6:10 PM
  • User2103134756 posted

    I could just as easily use SelectedIndexChanged event to do the same thing. But what I really want to do is force all form commands through the one method and not have to use two places to trap them.

    Thursday, June 13, 2019 7:34 PM
  • User288213138 posted

    Hi oneillj,

    The zero-based index of the selected row in a GridView control. The default is -1, which indicates that no row is currently selected.

    If you want to get the GridView Selected row index, you can get it by the following method.

    The code:

    Aspx:
    <div>
                <asp:GridView ID="GridView1" runat="server">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:LinkButton ID="LinkButton1" runat="server" OnClick="FormCommands">LinkButton</asp:LinkButton>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:Button ID="Button1" runat="server" Text="Button" OnClick="FormCommands" />
            </div>
    
    Aspx.cs:
     if (!IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id"), new DataColumn("Name"), new DataColumn("Country") });
                    dt.Rows.Add(1, "John Hammond", "United States");
                    dt.Rows.Add(2, "Mudassar Khan", "India");
                    dt.Rows.Add(3, "Suzanne Mathews", "France");
                    dt.Rows.Add(4, "Robert Schidner", "Russia");
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
    protected void FormCommands(object sender, EventArgs e)
            {
                //Init control variables
                LinkButton lbGridView = new LinkButton();
                Button cmdButton = new Button();
                GridView gridView = new GridView();
                int selectedRowIndex = -1;
                object senderObj;
                string senderType;
    
                //Init command argument text
                string command = string.Empty;
    
                //Get type from sender and 
                //convert to string
                senderObj = sender.GetType();
                senderType = senderObj.ToString();
    
                //if sender is a linkbutton
                if (senderType.Contains("LinkButton"))
                {
                    //Get control references
                    lbGridView = (LinkButton)sender;
                    gridView = (GridView)lbGridView.Parent.Parent.Parent.Parent;
                    //Get selected index of gridview and command arg
                    //selectedRowIndex = gridView.SelectedIndex;
                    //command = lbGridView.CommandArgument;
                    var rowIndex = ((GridViewRow)((Control)sender).NamingContainer).RowIndex;
    
                    Response.Write(rowIndex.ToString());
    
                }
                //if sender is a command button
                else if (senderType.Contains("Button"))
                {
                    //Get button reference and command arg
                    cmdButton = (Button)sender;
                    command = cmdButton.CommandArgument;
                }
            }

    The result:

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 14, 2019 6:24 AM
  • User2103134756 posted

    Thanks, samwu. That did the trick. Much appreciation.

    Friday, June 14, 2019 8:50 PM