单击按钮时更改 DataGridView 单元格的值 [英] Changing Values of DataGridView Cell on Button Click

查看:48
本文介绍了单击按钮时更改 DataGridView 单元格的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想问问当我选择某一行时,如何更新或更改 DataGridView 中列 'quantity' 的单元格值.我的 DataGridView 中有 3 列,它们是 Item NameQuantityPrice.每次我点击表单上的增加"或减少"按钮时,所选行项目的数量将在数量列中增加或减少 1,并且价格也会在每次数量增加或减少时更新.这是我将项目添加到 DataGridView 时的代码.

I just want to ask how I can update or change the cell value of column 'quantity' in my DataGridView when I select a certain row. I have 3 columns in my DataGridView which are Item Name, Quantity and Price. Every time I click the button 'add' or 'less' on my form, the quantity of the selected row item will either add to or subtract 1 from the quantity column and the price will also update every time the quantity increments or decrements. Here is my code when I add my item to my DataGridView.

我不确定将项目添加到数据网格是否正确.

I'm not really sure if I got it right to add items to my datagrid.

cmd = new MySqlCommand("SELECT * FROM tblmenu", dbConn);
MySqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
    Button btn = new Button();
    btn.Text = rdr["menuName"].ToString();
    btn.Width = 126;
    btn.Height = 80;
    btn.Click += delegate
    {
        MySqlConnection cnn2 = new MySqlConnection(sqlConn.connString);
        cnn2.Open();
        cmd = new MySqlCommand("SELECT menuName, menuPrice FROM tblmenu WHERE menuName = @name", cnn2);
        cmd.Parameters.AddWithValue("@name", btn.Text);
        MySqlDataReader rdr2 = cmd.ExecuteReader();
        while (rdr2.Read())
        {
            dataGridView1.Rows.Add(rdr2.GetString("menuName").ToUpper(), 1, rdr2.GetDouble("menuPrice"));
        }
        dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        lblQty.Text = dataGridView1.RowCount.ToString();
    };
}

我已经试过了,但是当我点击另一组物品时,前一个选择的物品的数量仍然增加.

I have tried this but when I click another set of item, the quantity from the previous selected item still increments.

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
    Add.Click += delegate
    {
        dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) + 1;
    };
    Minus.Click += delegate
    {
        dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["quantity"].Value) - 1;
    };
}

推荐答案

首先,使用 dataGridView1_CellContentClick 根本达不到目的.您打算在添加或减去按钮单击时增加或减少数量.我会使用 dataGridView1.CurrentRow 属性来获取 CurrentRow 并操纵数量列.为了更新价格列,您应该有一个 Rate 列或一个恒定的 Rate 值来将它与数量相乘.你有吗?如果不是,那么我看不出你是如何计算价格的.但是假设你有一个:

First of all, using dataGridView1_CellContentClick does not serve the purpose at all. You intend to increase or decrease quantity on Add or Minus button click. I would use dataGridView1.CurrentRow property to get hold of CurrentRow and manipulate quantity column.For updating price column, you should have a Rate column or a constant Rate value to multiply it with quantity. Do you have it? If not then I don't see how you arrive on price calculation. However assuming you have one :

void quantity_change(object sender, EventArgs e){
    var row=dataGridView1.CurrentRow;

    if(row==null || row.Index<0) 
    return;
    var unit = (sender==add)?1:-1;
    var quantity = Convert.ToInt32(row.Cells["quantity"].Value) + unit;

    row.Cells["quantity"].Value = quantity;
    //assuming you have rate column...
    var rate = Convert.ToDouble(row.Cells["Rate"].Value);
   row.Cells["Price"].Value = quantity * rate;
}

现在绑定您的 Add & 的 Click 事件减号按钮到表单构造函数中的上述方法.

Now bind Click event of your Add & Minus Buttons to the method above in Form constructor.

public myForm(){
    InitializeComponents();
    Add.Click+=quantity_change;
    Minus.Click+=quantity_change;
}

如果需要,您也可以使用表单的 Load 事件.

You can also use form's Load event if you want.

这篇关于单击按钮时更改 DataGridView 单元格的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆