在按钮上更改DataGridView单元格的值单击

问题描述:

我只想问我如何在我选择特定行时在DataGridView中更新或更改列'数量'的单元格值。 我有我的DataGridView中的3列是Item Name,QuantityPrice。 每次我在表单上点击“添加”或“更少”按钮时,所选行项目的数量将在数量列中增加或减少1,每次数量增加或减少时价格也会更新。这是我的代码,当我的项目添加到我的DataGridView。在按钮上更改DataGridView单元格的值单击

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

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

我试过这个,但是当我点击另一组物品时,上一个选定物品的数量仍然增加。

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值以将其与数量相乘。你有吗?如果没有,那么我不会看到你如何到达价格计算。但是假设你有一个:

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

现在绑定单击您添加&减号按钮的事件上面的方法在窗体构造函数。

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

如果需要,您也可以使用窗体的Load事件。

+0

哇。这有效:)现在我的问题是如何每次我点击添加或减号按钮动态乘数列或价格。 – LazyPirate

+0

我根据您的价格更新要求改进了我的答案。请标记我的答案,如果它解决了你的问题。请仔细阅读@Verdolino的答案。这是一个正确的做法和一个非常好的答案。值得我高兴。 –

+0

@LazyPirate我很高兴我的答案确实解决了你的问题。 –

我建议你考虑绑定你的DataGridView到数据源。如果可以避免,您不应该在UI上执行数据操作。

为了证明这一点,

private void quantityChangeClick(object sender, EventArgs e) 
{ 
    addToQuantity((Button)sender == this.Add ? 1 : -1); 
    updateTotal(); 
} 

private void addToQuantity(int howMuch) 
{ 
    var selectedRowIndices = dataGridView1.SelectedRows.OfType<DataGridViewRow>().Select(ro => ro.Index); 
    this.rows.Where((r, i) => selectedRowIndices.Contains(i)).ToList().ForEach(
     r => r.Quantity = Math.Max(0, r.Quantity + howMuch)); 

    this.dataGridView1.Refresh(); 
} 

// calculate the total from the data source as well 
private void updateTotal() 
{ 
    var total = Math.Round(this.rows.Sum(r => r.Quantity * r.Price), 2, MidpointRounding.AwayFromZero); 
    this.TotalLabel.Text = string.Format("₱{0:0.00}", total); 
} 

我已经使用了一些假的数据来演示这个我的结束,也就是行类。实际上,您可以做类似的事情,并将数据库中的每条记录添加到数据源行。

private class Row 
{ 
    public string ItemName { get; set; } 
    public int Quantity { get; set; } 
    public double Price { get; set; } 
    public Row(string i, int q, double p) 
    { 
     this.ItemName = i; 
     this.Quantity = q; 
     this.Price = p; 
    } 
} 

private List<Row> rows = new List<Row>(); 

private void Form1_Load(object sender, EventArgs e) 
{ 
    // instead, here you will add your rows from SQL 
    rows.AddRange(new Row[] 
    { 
     new Row("item1", 0, 500), 
     new Row("item2", 0, 400), 
     new Row("item3", 0, 850) 
    }); 
    this.dataGridView1.DataSource = rows; 
    dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; 
    Add.Click += quantityChangeClick; 
    Minus.Click += quantityChangeClick; 
} 

Example