Goran Bibic

Goran Bibic

  • 451
  • 2.9k
  • 177.2k

Add row in datagrid after select from SQL C#

Oct 21 2018 12:35 PM
Add row in datagrid after select from sql query

First time when select is empty row, second is full row

Some help please

Need when select from db value to insert in datagrid

Query is ok
 
Problem is from line 22  to line 33
  1. private void prijavaAction()  
  2.         {  
  3.             int sno = dataGridView1.Rows.Count + 1;  
  4.             SqlConnection con = new SqlConnection(cs);  
  5.                 if (textBox1.Text.All(char.IsDigit))  
  6.                 {  
  7.                     string queryString = "select bar_kod, ime, cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci artikl u bazi
  8.                     using (SqlConnection connection = new SqlConnection(cs))  
  9.                     {  
  10.                         SqlCommand command = new SqlCommand(queryString, connection);  
  11.                         connection.Open();  
  12.                         SqlDataReader reader = command.ExecuteReader();  
  13.                         if (reader.Read())  
  14.                         {  
  15.                             for (int i = 1; i < dataGridView1.Rows.Count; i++)  
  16.                             {  
  17.                                 dataGridView1.Rows[i].Cells["redni_broj"].Value = i;  
  18.                                 dataGridView1.Rows[i].Cells["bar_kod"].Value = reader["bar_kod"].ToString();  
  19.                                 dataGridView1.Rows[i].Cells["naziv_artikla"].Value = reader["ime"].ToString();  
  20.                                 dataGridView1.Rows[i].Cells["cijena"].Value = reader["cijena_sa_porezom"].ToString();  
  21.                             } 
  22.                         // dataGridView1.Rows.Add(sno, nazivartikla, 1, cijena, cijena);  
  23.                          dataGridView1.Rows.Add();
  24.                         reader.Close();
  25.                         }  
  26.                         else  
  27.                         {  
  28.                             MessageBox.Show("Bar kod ne postoji u bazi!""ObavjeÅ¡tenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  29.                             textBox1.Text = "";
  30.                         }  
  31.                     }
  32.                 }
  33.                 else  
  34.                 {  
  35.                     MessageBox.Show("Bar kod ne postoji ili nije bar kod!""ObavjeÅ¡tenje", MessageBoxButtons.OK, MessageBoxIcon.Information);  
  36.                     textBox1.Text = "";  
  37.                 }  
  38. }

Answers (3)