Mahmoud Fadl

Mahmoud Fadl

  • 1.4k
  • 319
  • 12.3k

How to update selected rows in datagridview

Nov 18 2017 6:42 AM
Hi, this first topic for me here
I'm happy to join with you, I want to update selected rows in datagridview based condition
with not editting rows in datagridview , and count selected rows in messagebox
i'ts not problem for me but the real problem the selected rows not count correctly and not updated correctly
i don't know where problem
 
 
The code below
 
 
  1. List selectedRows = (from row in DGVweight.Rows.Cast()  
  2.                                       where Convert.ToBoolean(row.Cells[0].Value) == true  
  3.                                       select row).ToList();  
  4.   
  5. if (selectedRows.Count > 0)  
  6. {  
  7.     if (MessageBox.Show(string.Format("Will be delivered Total {0} reels to Inventory ", selectedRows.Count), "Confirmation Delivered to Inventory", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)  
  8.     {  
  9.         try  
  10.         {  
  11.             DataAccessLayer.Open();  
  12.             foreach (DataGridViewRow row in selectedRows)  
  13.             {  
  14.                 using (SqlConnection con = new SqlConnection(DataAccessLayer.con))  
  15.                 {  
  16.                     using (SqlCommand cmd = new SqlCommand("UPDATE Weights set Status=@Delivered , DeliveryDate= @Date, UserName=@UserName WHERE ReelID = @ReelID", DataAccessLayer.cn))  
  17.                     {  
  18.                         cmd.CommandType = CommandType.Text;  
  19.                         cmd.Parameters.AddWithValue("@Delivered""Delivered");  
  20.                         cmd.Parameters.AddWithValue("@Date", dateTimePicker1.Value);  
  21.                         cmd.Parameters.AddWithValue("@ReelID", row.Cells["ReelID"].Value);  
  22.                         cmd.Parameters.AddWithValue("@UserName", GlobalVariables.CurrentUser);  
  23.                         cmd.ExecuteNonQuery();  
  24.                     }  
  25.   
  26.                 }  
  27.             }  
  28.             DataAccessLayer.Close();  
Thank you. 
 
 
 
 
 
image 1 
 
 
 
image 2 !!!  

Answers (1)