Question

I want to delete the selected row from DataGridView as well as the database. My code works only for the first row, for every other row it gives exception "Index was out of Range".

        SqlConnection con = new SqlConnection(conString);
        SqlCommand cmd = new SqlCommand();
        string itemId;
        if (dgPartyDetails.SelectedCells.Count > 0)
        {
            int selectedrowindex = dgPartyDetails.SelectedCells[0].RowIndex;
            try
            {
                DataGridViewRow selectedRow = dgPartyDetails.SelectedRows[selectedrowindex];
                if (selectedRow.Selected == true)
                {
                    dgPartyDetails.Rows.RemoveAt(selectedrowindex);
                    itemId = Convert.ToString(selectedRow.Cells[0].Value);
                    con.Open();
                    cmd.CommandText = "DELETE FROM tblParty WHERE Id=" + itemId + "";
                    cmd.Connection = con;
                    int count = cmd.ExecuteNonQuery();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }       
Était-ce utile?

La solution

It appears that you removed the DataGridRow before obtaining it's value.

dgPartyDetails.Rows.RemoveAt(selectedrowindex);
itemId = Convert.ToString(selectedRow.Cells[0].Value);

Reversing the order of these statement would fix your problem.

itemId = Convert.ToString(selectedRow.Cells[0].Value);
dgPartyDetails.Rows.RemoveAt(selectedrowindex);

Autres conseils

maybe u can try sqldatasource. for example:

<asp:SqlDataSource ID="sdsJur" runat="server" 
           ConnectionString="<%$ ConnectionStrings:NilaiConnectionString %>" 
           DeleteCommand="DELETE FROM [Jurusan] WHERE [Kd_Jurusan] = @Kd_Jurusan" 
           InsertCommand="INSERT INTO [Jurusan] ([Jurusan]) VALUES (@Jurusan)" 
           SelectCommand="SELECT * FROM [Jurusan]" 
           UpdateCommand="UPDATE [Jurusan] SET [Jurusan] = @Jurusan WHERE [Kd_Jurusan] = @Kd_Jurusan">
           <DeleteParameters>
               <asp:Parameter Name="Kd_Jurusan" Type="Int32" />
           </DeleteParameters>
           <InsertParameters>
               <asp:FormParameter FormField="tbJurusan" Name="Jurusan" />
           </InsertParameters>
           <UpdateParameters>
               <asp:Parameter Name="Jurusan" Type="String" />
               <asp:Parameter Name="Kd_Jurusan" Type="Int32" />
           </UpdateParameters>
</asp:SqlDataSource>`

and then u can use Gridview(I think similar to datagrigview). for example:

<asp:GridView ID="gvJurusan" runat="server" AllowPaging="True" 
     AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Kd_Jurusan"DataSourceID="sdsJur">
     <Columns>
         <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
         <asp:BoundField DataField="Kd_Jurusan" HeaderText="Kd_Jurusan" InsertVisible="False" ReadOnly="True" SortExpression="Kd_Jurusan" />
         <asp:BoundField DataField="Jurusan" HeaderText="Jurusan" SortExpression="Jurusan" />
     </Columns>
</asp:GridView>`

hope it usefull

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top