Add row in datagrid after select from SQL C#

  • 619 Views
  • Last Post 25 November 2018
  • Topic Is Solved
Stylus STYLUS posted this 21 October 2018

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

private void prijavaAction()
        {

            int sno = dataGridView1.Rows.Count + 1;

            SqlConnection con = new SqlConnection(cs);

                if (textBox1.Text.All(char.IsDigit))

                {
                    string queryString = "select bar_kod, ime, cijena_sa_porezom from roba_usluge WHERE bar_kod = '" + textBox1.Text + "'";// pronaci artikl u bazi              
                    using (SqlConnection connection = new SqlConnection(cs))
                    {
                        SqlCommand command = new SqlCommand(queryString, connection);
                        connection.Open();
                        SqlDataReader reader = command.ExecuteReader();


                        if (reader.Read())

                        {
                            for (int i = 1; i < dataGridView1.Rows.Count; i++)
                            {
                                dataGridView1.Rows[i].Cells["redni_broj"].Value = i;
                                dataGridView1.Rows[i].Cells["bar_kod"].Value = reader["bar_kod"].ToString();
                                dataGridView1.Rows[i].Cells["naziv_artikla"].Value = reader["ime"].ToString();
                                dataGridView1.Rows[i].Cells["cijena"].Value = reader["cijena_sa_porezom"].ToString();
                            }


                        // dataGridView1.Rows.Add(sno, nazivartikla, 1, cijena, cijena);
                         dataGridView1.Rows.Add();


                        reader.Close();


                        }
                        else
                        {
                            MessageBox.Show("Bar kod ne postoji u bazi!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            textBox1.Text = "";


                        }
                    }

                }

                else
                {
                    MessageBox.Show("Bar kod ne postoji ili nije bar kod!", "Obavještenje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    textBox1.Text = "";
                }
}
Order By: Standard | Newest | Votes
Stylus STYLUS posted this 22 October 2018

Result of my code is

add row to datagridview c#

I have created column in datagrid

Stylus STYLUS posted this 22 October 2018

I have created column in datagrid like on picture

 

Stylus STYLUS posted this 22 October 2018

No help?

admin posted this 22 November 2018

You should add a datasource to your DataGridView, then binding data to the DataGridView

For Example:

List<Product> list = ProductService.GetAll();
productBindingSource.DataSource = list;

Adding data directly to the DataGridView not a good solution

If you have a large amount of data, when you add data directly to DataGridView, it will be very slow

Peter Mbugua posted this 25 November 2018

Suggest you use book to check if barcode exist in datagridview something like this

bool barcodeexist= false;
foreach (datagridviewrow row in datagridview1.Rows){ 
int cqty=convert.ToInt32(row.Cells[index].Value)+1;
if(textbox 1.text==row.Cells[barcode index].Value.ToString()){ 
     row.Cells[index].Value=cqty;
     barcodeexist=true;
if(barcodeexist){ 
     calculatetotal(); 
} } }
if(barcodeexist==false){ //Addnewrow }
  • Liked by
  • Stylus STYLUS
Close