locked
I have a select command in my gridview and would like another command link button, I have tried but when i click my button the row doesnt select RRS feed

  • Question

  • User1912540128 posted

    I click the link and btnNotes_Click fires but the row doesnt get selected, so I get a System.ArgumentOutOfRangeException on GridViewRow row = GVBugs.Rows[GVBugs.SelectedIndex];

    protected void btnNotes_Click(object sender, EventArgs e)//EventArgs e)//object sender, GridViewCommandEventArgs e GridViewCommandEventArgs
    {
    GridViewRow row = GVBugs.Rows[GVBugs.SelectedIndex];
    // DataRow[] row = GetDataRow(sender, e);

    int IssueId = row.Cells[20].ToString().ToInt();// row[0][20].ToString().ToInt();

    // DataRow[] row = GetDataRow(sender, e); // GV_Select(sender, e);

    // int IssueId = row[0][20].ToString().ToInt();
    Response.Redirect(String.Format("Notes.aspx?IssueId={0}", 3));//IssueId


    }

    <asp:GridView ID="GVBugs" runat="server"
    DataKeyNames="IssueId"
    AutoGenerateColumns="False"
    OnSelectedIndexChanged="GvSelect"
    OnRowCreated="GVBugs_OnRowCreated"
    CssClass="gridView" GridLines="Vertical" Width="100%">
    <Columns>
    <asp:CommandField HeaderText="View" ShowSelectButton="True" SelectText="History" />
    <asp:TemplateField HeaderText="View" ItemStyle-Width="75px" ItemStyle-HorizontalAlign="Center">
    <ItemTemplate>
    <asp:LinkButton Visible="true" ID="btnNotes" runat="server" CausesValidation="False" OnClick="btnNotes_Click"  
    Text="Notes" /> 
    </ItemTemplate>
    </asp:TemplateField>
    <asp:CommandField HeaderText="Notes2" SelectText="Notes2" ShowSelectButton="True" />
    <asp:BoundField DataField="AddDate" HeaderText="Added" SortExpression="AddDate" Visible="true" DataFormatString="{0:d}" />
    </Columns>
    <EmptyDataTemplate>
    No Data Found.
    </EmptyDataTemplate>
    </asp:GridView>

    Monday, June 15, 2020 8:31 PM

All replies

  • User-943250815 posted

    Istead of use button click, you have to use GridView.RowCommand, so you can have multiple buttons each with a specific command name
    Just in case for exisitng SelectButton command name is "Select"
    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.gridview.rowcommand?view=netframework-4.8

    Tuesday, June 16, 2020 12:40 AM
  • User-719153870 posted

    Hi mikeasp30,

    System.ArgumentOutOfRangeException

    First of all, please make sure your Gridview GVBugs has at least 21 columns since you are trying to get the 21st cell.

    int IssueId = row.Cells[20].ToString().ToInt();

    I guess what you really want here is the text of the cell instead of the cell control itself, right?

    I built a demo here, please check if it helps,

    aspx:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:GridView ID="GVBugs" runat="server"
                    DataKeyNames="IssueId"
                    AutoGenerateColumns="False"
                    CssClass="gridView" GridLines="Vertical" Width="100%">
                    <Columns>
                        <asp:CommandField HeaderText="View" ShowSelectButton="True" SelectText="History" />
                        <asp:TemplateField HeaderText="View" ItemStyle-Width="75px" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:LinkButton Visible="true" ID="btnNotes" runat="server" CausesValidation="False" OnClick="btnNotes_Click"
                                    Text="Notes" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:CommandField HeaderText="Notes2" SelectText="Notes2" ShowSelectButton="True" />
                        <asp:BoundField DataField="AddDate" HeaderText="Added" SortExpression="AddDate" Visible="true" DataFormatString="{0:d}" />
                    </Columns>
                    <EmptyDataTemplate>
                        No Data Found.
                    </EmptyDataTemplate>
                </asp:GridView>
            </div>
        </form>
    </body>
    </html>

    cs:

            protected void Page_Load(object sender, EventArgs e)
            {
                DataTable dt = new DataTable();
                dt.Columns.AddRange(new DataColumn[5] { new DataColumn("FirstName"), new DataColumn("LastName"), new DataColumn("IssueId"), new DataColumn("AddDate"), new DataColumn("username") });
                dt.Rows.Add("AA", "aa", "1", "1", "Aa");
                dt.Rows.Add("BB", "bb", "2", "2", "Bb");
                dt.Rows.Add("CC", "cc", "3", "3", "Cc");
                dt.Rows.Add("DD", "dd", "4", "4", "Dd");
                dt.Rows.Add("EE", "ee", "5", "5", "Ee");
                dt.Rows.Add("FF", "ff", "6", "6", "Ff");
                dt.Rows.Add("GG", "gg", "7", "7", "Gg");
                GVBugs.DataSource = dt;
                GVBugs.DataBind();
            }
            protected void btnNotes_Click(object sender, EventArgs e)
            {
                LinkButton lbt = (LinkButton)sender;
                GridViewRow row = (GridViewRow)lbt.NamingContainer;
                int IssueId = Convert.ToInt32(row.Cells[3].Text.ToString());
                Response.Write(String.Format("Notes.aspx?IssueId={0}", IssueId));
            }

    result:

    Best Regard,

    Yang Shen

    Tuesday, June 16, 2020 5:32 AM