更新datagridview C#中的数量

时间:2018-07-19 13:47:30

标签: c# datagridview row updates

我目前正在开发P.O.S软件,我开始编写付款表格代码。但是我希望当用户输入相同的产品时增加数量值而不是将其显示在另一行上,我对其进行了编码,但它在另一行上显示了更新的行和产品

Image of the actual error

 public void dodadi() {
        MySqlConnection connection = Connection.prevzemiKonekcija();

        connection.Open();
        try
        {               
            if (string.IsNullOrWhiteSpace(txtBarajKod.Text))
            {
                return;
            }
            bool Found = false;
            if (dataGridView1.Rows.Count > 0)
            {
                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                    if (Convert.ToString(row.Cells[0].Value) == txtBarajKod.Text)
                    {
                        row.Cells[3].Value = Convert.ToString(1 + Convert.ToInt64(row.Cells[3].Value));
                        Found = true;

                    }
                }
            }
                if(!Found)
            {
                MySqlCommand command;
                MySqlDataAdapter adapter;
                DataTable tabela;
                MySqlDataReader reader;

                string query = "SELECT * FROM artikli WHERE barcode  like '%" + txtBarajKod.Text + "%'";
                command = new MySqlCommand(query, connection);
                adapter = new MySqlDataAdapter(command);
                tabela = new DataTable();
                reader = command.ExecuteReader();
                //dataGridView1.DataSource = tabela;
                //adapter.Fill(tabela);
                if (string.IsNullOrWhiteSpace(txtBarajKod.Text))
                {
                    return;
                }

                if (reader.Read())
                {
                    txtBarajKod.Text = reader.GetString("barcode");
                    txtNaziv.Text = reader.GetString("ProductName");
                    txtCena.Text = reader.GetString("SellPrice");
                    kolicina = 1;
                    txtKolicina.Text = kolicina.ToString();
                }
                else
                {
                    txtBarajKod.Text = "";
                    txtNaziv.Text = "";
                    txtCena.Text = "";
                    txtKolicina.Text = "";
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            connection.Close();
        }
    }


  private void dodadivotabela() {


        cena = float.Parse(txtCena.Text);
        kolicina = float.Parse(txtKolicina.Text);
        konecnacena = cena * kolicina;

        string prvred = txtBarajKod.Text;
        string vtorred = txtNaziv.Text;
        float tretred = cena;
        float cetvrtred = kolicina;
        float pettired = konecnacena;


        dataGridView1.Rows.Add(prvred, vtorred, tretred, cetvrtred, pettired);


    }

这是将数据添加到dgv的第二种方法

1 个答案:

答案 0 :(得分:0)

如果我清楚理解,当您增加第一行的值时,您不希望该值显示在第二行和第三行的第四个单元格上。

如果我是对的,则应在break循环中添加foreach语句,如下所示:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    if (Convert.ToString(row.Cells[0].Value) == txtBarajKod.Text)
    {
        row.Cells[3].Value = Convert.ToString(1 + Convert.ToInt64(row.Cells[3].Value));
        Found = true;
        break;
    }
}

首次满足条件后,break语句将允许您退出foreach循环。如果您忽略它,它将循环遍历所有其他行,并像您的屏幕截图一样更改第4个单元格中的值,因为其他行中的条件仍然满足。

已更新

好,所以您只需清除dataGridView1,然后再将更新的行添加到其中即可。我假设您使用递增的值更新行时将调用dodadivotabela()方法。将dataGridView1.Rows.Clear()添加到您的方法中:

private void dodadivotabela()
{
    cena = float.Parse(txtCena.Text);
    kolicina = float.Parse(txtKolicina.Text);
    konecnacena = cena * kolicina;

    string prvred = txtBarajKod.Text;
    string vtorred = txtNaziv.Text;
    float tretred = cena;
    float cetvrtred = kolicina;
    float pettired = konecnacena;

    dataGridView1.Rows.Clear();
    dataGridView1.Rows.Add(prvred, vtorred, tretred, cetvrtred, pettired);
}