Click here to Skip to main content
14,732,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, Can anyone ahare code snippet for deleting the selected rows(multi select) from a datagridview. Before delete I want to check whether the items going to be deleted are added by the user who has logged in.

Thanks in advance


private void btnDelete_Click(object sender, EventArgs e)
        {
            dgDtParameters.AllowUserToDeleteRows = true;
            System.Data.DataTable objDt = new System.Data.DataTable();
            DataRow objdrDelete;
            DataSet dsAdmin = new DataSet();
            try
            {
                objConnection = new DBAccess();
                SqlParameter[] sqlparamAdmin = {
                                                   new SqlParameter("@accountID",iaccountID),
                                                   new SqlParameter("@projectID",iprojectID),
                                                   new SqlParameter("@componentID",icomponentID),
                                                   new SqlParameter("@createdBy",CCommonInfo.iUserID),
                                               };
                dsAdmin = objConnection.GetDataSet("prcDisplayAdmindataTemplate", sqlparamAdmin);
                if (iRoleID == 1 || iRoleID == 2 || iRoleID == 3)
                {
                    if (MessageBox.Show(CompCreatorMsg.Msg_DPDelete, CompCreatorMsg.Title_DPDelete, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        objDt = dsobbtemp.Tables[0];
                        objdrDelete = objDt.NewRow();
                        //if (objDt != null)
                        {
                            for (int i = 0; i < objDt.Rows.Count; i++)
                            {
                                objdrDelete["objectRevisedName"] = objDt.Rows[i]["objectRevisedName"].ToString();
                                srowFilter = "objectRevisedName = '" + objdr["objectRevisedName"].ToString() + "'";
                                if (dsAdmin.Tables[0].Select(srowFilter).Length == 0)
                                {
                                    SqlParameter[] sqlparam = {
                                                  new SqlParameter("@objectID",Convert.ToString(objDt.Rows[i]["objectID"]))
                                              };
                                    iResults = objConnection.ExecuteCommand("prcDeleteDataTemplate", sqlparam);
                                }
                            }
                        }
                        objDt.Clear();
                        objDt.Dispose();
                    }
                }
            }
            catch (Exception exGeneral)
            {
                MessageBox.Show(exGeneral.Message + exGeneral.StackTrace.ToString());
            }
        }
Posted

try
{

sqlConnection.Close();
int nn = dataGridView1.SelectedCells[0].RowIndex;
string s = dataGridView1[0, nn].Value.ToString();
int n = Convert.ToInt32(s);
SqlCommand cmd = new SqlCommand("delete from Master_Vlc where Vlc_Code=" + n, sqlConnection);
ds = new DataSet();



sqlConnection.Open();
sqlDataAdapter = new SqlDataAdapter(cmd);

sqlDataAdapter.Fill(ds, "Master_Vlc");
MessageBox.Show("Value Deleted");
sqlConnection.Close();
dataGridView1.Rows.RemoveAt(nn);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900