Goran Bibic

Goran Bibic

  • 458
  • 2.9k
  • 201.3k

Increment quantity if have value from textbox in datagrid c#

Nov 18 2018 8:29 AM
Some little help.
 
Need if  exist in datagrid value from textbox1 ( column name: bar_code) to increment cell kolicina(quantity eng.) + 1
  1. private void prijavaAction()  
  2.         {  
  3.   
  4.             int sno = dataGridView1.Rows.Count + 1;  
  5.   
  6.             SqlConnection con = new SqlConnection(cs);  
  7.   
  8.             if (textBox1.Text.All(char.IsDigit))  
  9.   
  10.             {  
  11.                 string queryString = "select bar_kod, ime, cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci artikl u bazi                
  12.                 using (SqlConnection connection = new SqlConnection(cs))  
  13.                 {  
  14.                     SqlCommand command = new SqlCommand(queryString, connection);  
  15.                     connection.Open();  
  16.                     SqlDataReader reader = command.ExecuteReader();  
  17.   
  18.                      
  19.                     var itemAndPrice = textBox1.Text;  
  20.   
  21.                     if (reader.Read())  
  22.                     {                         
  23.   
  24.                         var index = dataGridView1.Rows.Add();  
  25.                         dataGridView1.Rows[index].Cells["redni_broj"].Value = index + 1;  
  26.                         dataGridView1.Rows[index].Cells["bar_kod"].Value = reader["bar_kod"].ToString();  
  27.                         dataGridView1.Rows[index].Cells["naziv_artikla"].Value = reader["ime"].ToString();  
  28.                         dataGridView1.Rows[index].Cells["cijena"].Value = reader["cijena_sa_porezom"].ToString();  
  29.                         dataGridView1.Rows[index].Cells["kolicina"].Value = "1";  
  30.   
  31.   
  32.                         foreach (DataGridViewRow g1 in dataGridView1.Rows)  
  33.                         {  
  34.   
  35.   
  36.                             g1.Cells["ukupno"].Value = (Convert.ToDouble(g1.Cells["kolicina"].Value) * Convert.ToDouble(g1.Cells["cijena"].Value)).ToString("0.00");
  37.                                g1.Cells["kolicina"].Value = Convert.ToInt32(g1.Cells["kolicina"].Value) + 1;
  38.                         }
  39.                     }  
  40.                     else  
  41.                     {  
  42.                         MessageBox.Show("Bar kod ne postoji u bazi!""Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  43.                         textBox1.Text = "";  
  44.                     }
  45.                 }
  46.             }
  47.             else  
  48.             {  
  49.                 MessageBox.Show("Bar kod ne postoji ili nije bar kod!""Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  50.                 textBox1.Text = "";  
  51.             }  
  52.   

 

Answers (12)