Increment quantity if have value from textbox in datagrid c#

  • 450 Views
  • Last Post 22 November 2018
Stylus STYLUS posted this 18 November 2018

Some little help. Need if exist in datagrid value from textbox1 ( column name: bar_code) to increment cell kolicina (quantity eng.) + 1  

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();  

                    var itemAndPrice = textBox1.Text;  

                    if (reader.Read())  
                    {                         

                        var index = dataGridView1.Rows.Add();  
                        dataGridView1.Rows[index].Cells["redni_broj"].Value = index + 1;  
                        dataGridView1.Rows[index].Cells["bar_kod"].Value = reader["bar_kod"].ToString();  
                        dataGridView1.Rows[index].Cells["naziv_artikla"].Value = reader["ime"].ToString();  
                        dataGridView1.Rows[index].Cells["cijena"].Value = reader["cijena_sa_porezom"].ToString();  
                        dataGridView1.Rows[index].Cells["kolicina"].Value = "1";  

                        foreach (DataGridViewRow g1 in dataGridView1.Rows)  
                        {  

                            g1.Cells["ukupno"].Value = (Convert.ToDouble(g1.Cells["kolicina"].Value) * Convert.ToDouble(g1.Cells["cijena"].Value)).ToString("0.00");  

                                   g1.Cells["kolicina"].Value = Convert.ToInt32(g1.Cells["kolicina"].Value) + 1;


                        }
                    }  
                    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 = "";  
            }  
}  

 

admin posted this 22 November 2018

I think the best way you should add a datasource to your DataGridView

for example

define an OrderDetails class

public class OrderDetails{
    public int OrderID { get; set; }
    public int ProductID { get; set; }
    public int Quantity { get; set; }
    //etc...
}

Next, You should get all product

List<Product> list = ProductService.GetAll();
//Declare a ProductService class to fetch all products from the product table

Using linq to find data

var product = (from p in list
            where p.barcode == txtBarcode.text
            select p).SingleOrDefault();

var orderDetail = (from o in (orderdetailbindingsource.DataSource as List<OrderDetails&gt
                  where o.ProductID = product.ProductID).SingleOrDefault();
if(orderDetail != null)
   orderDetail.Quantity += 1;
//etc...
                  

I hope you can understand the way I've showed above

Close