我有一个带有datagridview和textbox的简单表单。在datagridview中,有选择(复选框),项目名称,数量和价格列。当我在datagridview中检查项目名称时,它将计算总数并在文本框中显示它,但不会直接更新。我必须单击另一列才能更新该值。
如果在检查datagridview中的项目而不点击其他列来更新值时,如何直接更新总计?
public partial class Form1 : Form
{
//BindingList<Item> items = new BindingList<Item>();
BindingSource items = new BindingSource();
public Form1()
{
InitializeComponent();
items.Add(new Item { Select = false, Name = "A", Quantity = 1, Price = 10000 });
items.Add(new Item { Select = false, Name = "B", Quantity = 2, Price = 20000 });
dataGridView1.DataSource = items;
}
private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
decimal total = 0;
foreach (Item item in items)
{
if (item.Select)
{
total += (item.Quantity * item.Price);
}
}
textBox1.Text = total.ToString();
}
}
public class Item
{
public bool Select { get; set; }
public string Name { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}