Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
C#
private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
               dataGridView2.Refresh();

           }
       }
Posted
Updated 18-Oct-21 11:38am

Presently, you call dataGridView2.Refresh(); in the for loop. This means that after deleting the first of the selected rows, the datagrid gets refreshed, and the selection is lost, consequently the other selected rows won't be deleted.
Just place that at a later position, something like
C#
private void Delete_Click(object sender, EventArgs e)
   {
       objConn1.Open();
       for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
       {
           string sql = "Delete from  [SAPProduction].[ProductionData]  Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value + "'";
           SqlCommand cmd = new SqlCommand(sql, objConn1);
           cmd.ExecuteNonQuery();
       }
       objConn1.Close();
       MessageBox.Show("Record Deleted successfully...");
       dataGridView2.Refresh();
   }

Apart from that change, you should also use a parameterized query.
 
Share this answer
 
Comments
Prasad_Kulkarni 16-Jan-13 2:27am    
Good suggestion +5
Hi,

C#
private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
//               dataGridView2.Refresh();
// call the function which will bind the data to datagridview2 or remove the current row from datagridview2

           }
       }


http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/3970aea7-de83-4c53-8034-83aaa9f7a9a1[^]
 
Share this answer
 
Comments
Suvabrata Roy 15-May-15 3:05am    
Why Down voted ?
datagridview.Refresh() redraws the grid-view from the data source
(presumably a DataSet in your case);

It doesn't reload the DataSet from the database.

IMHO, You can simply bind your code after successful deletion message. That would be more simpler.

C#
private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
               //Instead of this
               dataGridView2.Refresh();
               // Fill your grid here
           }
       }
 
Share this answer
 
v2
Comments
Master Vinu 16-Jan-13 2:15am    
not getting clear
Prasad_Kulkarni 16-Jan-13 2:16am    
The code using which you're filling your grid simply call it; instead of dataGridView2.Refresh()
Refreshing the dataGridView2 will not affect any rows, this is because your dataGridView is attached to a DataSet or DataTable which is same as the last time your code is using. Instead use that DataSet to get the new values form the database.
Have a look at the Solution 2
and add a last line
dataGridView2.DataSource= ds/dt;//Give your datasource(ie a DataSet or DataTable) here
 
Share this answer
 
Comments
Master Vinu 16-Jan-13 2:31am    
private void Delete_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
{
string sql = "Delete from [SAPProduction].[ProductionData] " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "' ";
// Jobcard = your desire Rows Jobcard Id

SqlCommand cmd = new SqlCommand(sql, objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show("Record Deleted successfully...");

SqlDataAdapter dap1 = new SqlDataAdapter(sql, objConn1);
SqlCommandBuilder cmdbd = new SqlCommandBuilder(dap1);
DataTable dtb = new DataTable();
dap1.Fill(dtb);
BindingSource bs = new BindingSource();
bs.DataSource = dtb;
dataGridView2.DataSource = bs;

}
}
Master Vinu 16-Jan-13 2:32am    
but its clear all rows of datagridview
Shanu2rick 16-Jan-13 2:56am    
It is because, you are deleting them
Master Vinu 16-Jan-13 3:04am    
pls make changes in above code/...thx in advance
Shanu2rick 16-Jan-13 3:06am    
Okey, one more question. Tell me the SelectionMode property value of the dataGridView2.
private void Delete_Click(object sender, EventArgs e)
{

if (objConn1.State != ConnectionState.Open)
{
objConn1.Open();
}
for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
{
string sql = "Delete from [SAPProduction].[ProductionData] " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "' ";
// Jobcard = your desire Rows Jobcard Id

SqlCommand cmd = new SqlCommand(sql, objConn1);
//objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show("Record Deleted successfully...");
string sql1 = "SELECT [Jobcard],[Planqty],[Mouldqty],[Operator],[Supervisor],[Profilename],[Process],[Date],[Remark],[Approvedby],[id],[Division] FROM [SAPProduction].[ProductionData] WHERE JobCard ='" + findjobsap.Text.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sql1, objConn1);
DataSet ds = new DataSet();
objConn1.Open();
da.Fill(ds, "ProductionData");
dataGridView2.DataSource = ds;
dataGridView2.DataMember = "ProductionData";
dataGridView2.Update();
}
}
 
Share this answer
 
C#
private void Delete_Click(object sender, EventArgs e)
       {
           for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
           {
               string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id = '" + dataGridView2.CurrentRow.Cells["id"].Value.ToString() + "'  ";
               // Jobcard = your desire Rows Jobcard Id

               SqlCommand cmd = new SqlCommand(sql, objConn1);
               objConn1.Open();
               cmd.ExecuteNonQuery();
               objConn1.Close();
               MessageBox.Show("Record Deleted successfully...");
              
               dataGridView2.DataSource= Give your datasource here.
           }
       }
 
Share this answer
 
After "delete" table try new command "select" table
and connect to Datagridview
i solved the problem
 
Share this answer
 
You will need to re-query the [ProductionData] table, set the datasource, and refresh again for the DataGridView for the records to show properly. This needs to be done after the for loop.

The for loop is also very inefficient. You are connecting, executing the sql statement, and closing the connection for each record. You are also displaying the "Record deleted successfully." after each deletion. A better way to code it is to concatenate the ids in a string separated by commas and use the IN key word in your sql.

string sql = "Delete from  [SAPProduction].[ProductionData]  " + " Where id in ('";
string inClause = String.Empty;

for (int i = 0; i < dataGridView2.SelectedRows.Count; i++)
{
     // Jobcard = your desire Rows Jobcard Id
     inClause += dataGridView2.CurrentRow.Cells["id"].Value.ToString() + ",";
     inClause = inClause.Substring(1, inClause.Length - 1) + ")";
}

SqlCommand cmd = new SqlCommand(sql, objConn1);
objConn1.Open();
cmd.ExecuteNonQuery();
objConn1.Close();
MessageBox.Show("Record Deleted successfully...");
   
//Reload your datasource, set it, and refresh.
}
 
Share this answer
 

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