have a gridview with merged columns. When I try to delete a row in my gridview, my previous row index is picked and previous row is deleted. No clue why this is happening...Experts...Please help!!!

Here is my code for delete:

protected void grdItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //get the id of selected row
        GridViewRow row = grdItems.Rows[e.RowIndex];
        if (row != null)
        {
            grdItems.Rows[e.RowIndex].Cells[0].FindControl("lblRowID");
            Label lblRowID = (Label)grdItems.Rows[e.RowIndex].Cells[0].FindControl("lblRowID");
            string id = lblRowID.Text;
            DeleteRecord(id);
        }
        calculatetot();
        populateItems();
        
    }
    private void DeleteRecord(string ID)
    {
        SqlConnection myConnection = new SqlConnection(connectionString);
        string sqlstmt = "delete bod_items where rowid = @rowID";

        try
        {
            myConnection.Open();
            SqlCommand cmd = new SqlCommand(sqlstmt, myConnection);
            cmd.Parameters.AddWithValue("@rowid", ID);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (Exception err)
        {
            lblResult.Text = "Error deleting record. ";
            lblResult.Text += err.Message;
        }
        finally
        {
            myConnection.Close();
        }

    }

Here is my code for gridview:

<asp:GridView ID="grdItems"  runat="server" AllowSorting="True"
            BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
            CellPadding="4" ForeColor="Black" GridLines="Vertical" width="670px" OnRowCancelingEdit="grdItems_RowCancelingEdit" OnRowEditing="grdItems_RowEditing" OnRowUpdating="grdItems_RowUpdating" DataKeyNames="rowid" AutoGenerateColumns="False" OnRowDeleting="grdItems_RowDeleting" Font-Size="Smaller" ShowFooter="True" OnDataBound="grdItems_DataBound">
            <Columns>
            <asp:TemplateField HeaderText="ROWID" SortExpression="rowid">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRowid" runat="server" Text='<%# Bind("rowid") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblRowID" runat="server" Text='<%# Bind("rowid") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:TemplateField>                
                <asp:BoundField DataField="MTN" HeaderText="MTN" SortExpression="MTN" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:BoundField DataField="cAccNum" HeaderText="ACCOUNT NO." SortExpression="cAccNum" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:BoundField DataField="summInvNum" HeaderText="INVOICE NO." SortExpression="summInvNum" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="AMOUNT" SortExpression="invItemAmt">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtAmt" runat="server" Text='<%# Bind("invItemAmt") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblAmt" runat="server" Text='<%# Bind("invItemAmt") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                  <FooterTemplate>   
                  <table border="0" cellpadding="0" cellspacing="0" width="100%">   
                  <tr><td width="50%">Total for MTN:</td> </tr>   
                  <tr><td width="50%">Total Amt Due:</td> </tr>   
                  </table>   
                  </FooterTemplate>
             </asp:TemplateField>
      <asp:TemplateField HeaderText = "TOTAL AMOUNT"> 
      <ItemTemplate>
      <asp:Literal ID = "AmountLiteral" runat="server"></asp:Literal>
      </ItemTemplate>        
      <FooterTemplate> <table border="0" cellpadding="0" cellspacing="0" width="100%">       
      <tr><asp:Literal ID = "amountTotal"  runat = "server"></asp:Literal></tr>
      <tr><asp:Literal ID = "amountTotal1"  runat = "server"></asp:Literal></tr>
      </table>      
      </FooterTemplate>   
      </asp:TemplateField>   
                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True">
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:CommandField>                
            </Columns>                                  
            <FooterStyle ForeColor="Black" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BorderColor="Black" BorderStyle="Solid" />
        </asp:GridView>

you just need to write below line of code in your rowdeleting event of gridview. No need to check for row != null condition :

protected void grdItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    string id = grdItems.DataKeys[e.RowIndex].Value.ToString();
    DeleteRecord(id);
    calculatetot();
    populateItems();
}

I hope it will solve your problem.

have a gridview with merged columns. When I try to delete a row in my gridview, my previous row index is picked and previous row is deleted. No clue why this is happening...Experts...Please help!!!

Here is my code for delete:

protected void grdItems_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //get the id of selected row
        GridViewRow row = grdItems.Rows[e.RowIndex];
        if (row != null)
        {
            grdItems.Rows[e.RowIndex].Cells[0].FindControl("lblRowID");
            Label lblRowID = (Label)grdItems.Rows[e.RowIndex].Cells[0].FindControl("lblRowID");
            string id = lblRowID.Text;
            DeleteRecord(id);
        }
        calculatetot();
        populateItems();
        
    }
    private void DeleteRecord(string ID)
    {
        SqlConnection myConnection = new SqlConnection(connectionString);
        string sqlstmt = "delete bod_items where rowid = @rowID";

        try
        {
            myConnection.Open();
            SqlCommand cmd = new SqlCommand(sqlstmt, myConnection);
            cmd.Parameters.AddWithValue("@rowid", ID);
            cmd.CommandType = CommandType.Text;
            cmd.ExecuteNonQuery();
        }
        catch (Exception err)
        {
            lblResult.Text = "Error deleting record. ";
            lblResult.Text += err.Message;
        }
        finally
        {
            myConnection.Close();
        }

    }

Here is my code for gridview:

<asp:GridView ID="grdItems"  runat="server" AllowSorting="True"
            BackColor="White" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px"
            CellPadding="4" ForeColor="Black" GridLines="Vertical" width="670px" OnRowCancelingEdit="grdItems_RowCancelingEdit" OnRowEditing="grdItems_RowEditing" OnRowUpdating="grdItems_RowUpdating" DataKeyNames="rowid" AutoGenerateColumns="False" OnRowDeleting="grdItems_RowDeleting" Font-Size="Smaller" ShowFooter="True" OnDataBound="grdItems_DataBound">
            <Columns>
            <asp:TemplateField HeaderText="ROWID" SortExpression="rowid">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtRowid" runat="server" Text='<%# Bind("rowid") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblRowID" runat="server" Text='<%# Bind("rowid") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:TemplateField>                
                <asp:BoundField DataField="MTN" HeaderText="MTN" SortExpression="MTN" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:BoundField DataField="cAccNum" HeaderText="ACCOUNT NO." SortExpression="cAccNum" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:BoundField DataField="summInvNum" HeaderText="INVOICE NO." SortExpression="summInvNum" >
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:BoundField>
                <asp:TemplateField HeaderText="AMOUNT" SortExpression="invItemAmt">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtAmt" runat="server" Text='<%# Bind("invItemAmt") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="lblAmt" runat="server" Text='<%# Bind("invItemAmt") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                  <FooterTemplate>   
                  <table border="0" cellpadding="0" cellspacing="0" width="100%">   
                  <tr><td width="50%">Total for MTN:</td> </tr>   
                  <tr><td width="50%">Total Amt Due:</td> </tr>   
                  </table>   
                  </FooterTemplate>
             </asp:TemplateField>
      <asp:TemplateField HeaderText = "TOTAL AMOUNT"> 
      <ItemTemplate>
      <asp:Literal ID = "AmountLiteral" runat="server"></asp:Literal>
      </ItemTemplate>        
      <FooterTemplate> <table border="0" cellpadding="0" cellspacing="0" width="100%">       
      <tr><asp:Literal ID = "amountTotal"  runat = "server"></asp:Literal></tr>
      <tr><asp:Literal ID = "amountTotal1"  runat = "server"></asp:Literal></tr>
      </table>      
      </FooterTemplate>   
      </asp:TemplateField>   
                <asp:CommandField ShowEditButton="True" ShowDeleteButton="True">
                    <HeaderStyle Font-Size="Small" HorizontalAlign="Center" />
                    <ItemStyle Font-Names="Arial" Font-Size="Smaller" />
                </asp:CommandField>                
            </Columns>                                  
            <FooterStyle ForeColor="Black" />
            <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
            <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BorderColor="Black" BorderStyle="Solid" />
        </asp:GridView>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.