gpt4 book ai didi

c# - 计算总和 - Datagridview CellFormatting 事件性能太慢

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:35 25 4
gpt4 key购买 nike

我正在使用 dataGridView1_CellFormatting 对每个列求和。但这使我的 datagridview 在滚动时非常慢。 Especially 当我有大量数据时。

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

Decimal sum = 0, sum2 = 0, sum3 = 0;
for (int i = 0; i < CustomersGrid.Rows.Count; ++i)
{

sum += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[7].Value);
sum2 += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[6].Value);
sum3 += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[8].Value);
}

Quantitytxt.Text = sum2.ToString() + " ";
Sumtxt.Text = string.Format("{0:0.00}", sum).Replace(",", ".") + "€" + " ";
DiscountSumtxt.Text = string.Format("{0:0.00}", sum3).Replace(",", ".") + "€" + " ";

}

有没有更有效的方法呢?仅当我将获得此单元格时才计算总和的示例?或者是否还有其他事件或方法?我也试过这个。

 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{

Decimal sum = 0, sum2 = 0, sum3 = 0;
for (int i = 0; i < CustomersGrid.Rows.Count; ++i)
{
if (e.ColumnIndex == 7 || e.ColumnIndex == 6 || e.ColumnIndex == 8)
{
sum += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[7].Value);
sum2 += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[6].Value);
sum3 += Convert.ToDecimal(CustomersGrid.Rows[e.RowIndex].Cells[8].Value);
}
}
Quantitytxt.Text = sum2.ToString() + " ";
Sumtxt.Text = string.Format("{0:0.00}", sum).Replace(",", ".") + "€" + " ";
DiscountSumtxt.Text = string.Format("{0:0.00}", sum3).Replace(",", ".") + "€" + " ";

}

它看起来性能好一点,但是当我滚动我的 datagridview 时它仍然很慢。

最佳答案

documentations中已经提到了:

The CellFormatting event occurs every time each cell is painted, so you should avoid lengthy processing when handling this event. This event also occurs when the cell FormattedValue is retrieved or its GetFormattedValue method is called.

处理 CellFormatting对于所有单元格来说,计算太多了 Sum .

如果您使用的是 DataSource喜欢DataTable这引发了ListChanged事件,计算Sum , 你可以依靠 ListChanged事件。

作为另一种选择,您可以依赖 RowsAdded , RowsRemovedCellValueChanged DataGridView的事件计算Sum .

示例 - 数据表 - 列的总和

DataTable提高 ListChange事件。您可以订阅更新文本框的事件:

private async void Form1_Load(object sender, EventArgs e)
{
// Define data table
var dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Price", typeof(int));

// Fill data
dt.Rows.Add("Product 1", 100);
dt.Rows.Add("Product 2", 200);

// Set data source of data grid view
this.dataGridView1.DataSource = dt;

// Automatically update text box, by SUM of price
textBox1.Text = $"{dt.Compute("SUM(Price)", ""):F2}";
dt.DefaultView.ListChanged += (obj, args) =>
textBox1.Text = $"{dt.Compute("SUM(Price)", ""):F2}";
}

示例 - 列表 - 属性总和

List<T>不提高 ListChanged事件。您可以使用 BindingSource作为数据源和句柄 ListChanged BindingSource的事件相反:

public class Product
{
public string Name { get; set; }
public int Price { get; set; }
}

private async void Form1_Load(object sender, EventArgs e)
{
// Define list
var list = new List<Product>();

// Fill data
list.Add(new Product { Name = "Product 1", Price = 100 });
list.Add(new Product { Name = "Product 2", Price = 200 });

// Set data source of data grid view
var bs = new BindingSource();
bs.DataSource = list;
this.dataGridView1.DataSource = bs;

// Automatically update text box, by SUM of price
textBox1.Text = $"{list.Sum(x => x.Price):F2}";
bs.ListChanged += (obj, args) =>
textBox1.Text = $"{list.Sum(x => x.Price):F2}";
}

关于c# - 计算总和 - Datagridview CellFormatting 事件性能太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54782726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com