Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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 15-Jan-13 21:05pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
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[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
 
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
           }
       }
  Permalink  
v2
Comments
vinayak chavan at 16-Jan-13 2:15am
   
not getting clear
Prasad_Kulkarni at 16-Jan-13 2:16am
   
The code using which you're filling your grid simply call it; instead of dataGridView2.Refresh()
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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
    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.
  Permalink  
Comments
Prasad_Kulkarni at 16-Jan-13 2:27am
   
Good suggestion +5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Comments
vinayak chavan at 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;

}
}
vinayak chavan at 16-Jan-13 2:32am
   
but its clear all rows of datagridview
Shanu2rick at 16-Jan-13 2:56am
   
It is because, you are deleting them
vinayak chavan at 16-Jan-13 3:04am
   
pls make changes in above code/...thx in advance
Shanu2rick at 16-Jan-13 3:06am
   
Okey, one more question. Tell me the SelectionMode property value of the dataGridView2.
vinayak chavan at 16-Jan-13 3:16am
   
RowHeaderSelect
Shanu2rick at 16-Jan-13 4:10am
   
Change SelectionMode=FullRowSelect and MultiSelect=True.
Then try again.
vinayak chavan at 16-Jan-13 4:20am
   
no dear Shanu2rick - after delete its blackout
Shanu2rick at 16-Jan-13 4:33am
   
and your selection process is...?
vinayak chavan at 16-Jan-13 4:42am
   
i hv used above query
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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();
}
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
           }
       }
  Permalink  

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

  Print Answers RSS
0 Maciej Los 424
1 ridoy 400
2 DamithSL 230
3 OriginalGriff 179
4 Suvendu Shekhar Giri 150
0 OriginalGriff 8,033
1 DamithSL 6,179
2 Sergey Alexandrovich Kryukov 5,538
3 Maciej Los 5,508
4 Kornfeld Eliyahu Peter 4,539


Advertise | Privacy | Mobile
Web03 | 2.8.141223.1 | Last Updated 16 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100