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);
            }

        }       
有帮助吗?

解决方案

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);

其他提示

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top