c# - 在 C# 中的 datagridview 中计算重复值和求和值
问题描述
我正在尝试在添加新行时计数并删除重复的行,如果该行不存在,它应该为它创建一个新行。如果添加了新行并且与现有行之一相同,则将其添加到计数中。
它应该寻找:“代码”是否相同以及“部分”是否相同。
我得到了什么:
现在更新代码: CodeCombox 已替换为 textBoxScanner。
private void addcodes()
{
if (!int.TryParse(CountTextBox.Text, out var count)) return;
var product = _bindingList
.FirstOrDefault(prod =>
prod.Code == Convert.ToString(textBoxScanner.Text) &&
prod.Parts == PartsComboBox.Text);
if (product == null)
{
_bindingList.Add(new Product()
{
Code = Convert.ToString(textBoxScanner.Text),
Parts = PartsComboBox.Text,
Count = count
});
}
else
{
product.Count = product.Count + count;
}
textBoxScanner.Focus();
textBoxScanner.Clear();
}
类已更新为字符串:
public class Product : INotifyPropertyChanged
{
private string _code;
private string _parts;
private int _count;
public string Code
{
get => _code;
set
{
_code = value;
OnPropertyChanged();
}
}
public string Parts
{
get => _parts;
set
{
_parts = value;
OnPropertyChanged();
}
}
public int Count
{
get => _count;
set
{
_count = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
解决方案
一种解决方案是使用具有具体类的 BindingList,而不是直接从 DataGridView 工作。在下面的示例中,有两个用于 Code 和 Parts 的 ComboBox 控件,一个用于 Count 的 TextBox。
存储信息的类
public class Product : INotifyPropertyChanged
{
private int _code;
private string _parts;
private int _count;
public int Code
{
get => _code;
set
{
_code = value;
OnPropertyChanged();
}
}
public string Parts
{
get => _parts;
set
{
_parts = value;
OnPropertyChanged();
}
}
public int Count
{
get => _count;
set
{
_count = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
在添加按钮中,lambda 确定产品是否存在,如果存在,则添加计数,如果不存在则添加新记录。请注意,如果您需要检查或更改 DataGridView 中的当前行,则有一个当前按钮。
public partial class Form1 : Form
{
private readonly BindingList<Product> _bindingList;
public Form1()
{
InitializeComponent();
_bindingList = new BindingList<Product>();
dataGridView1.DataSource = _bindingList;
dataGridView1.AllowUserToAddRows = false;
Shown += OnShown;
}
private void OnShown(object sender, EventArgs e)
{
Controls
.OfType<ComboBox>()
.ToList()
.ForEach(cb => cb.SelectedIndex = 0);
}
private void AddButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(CountTextBox.Text, out var count)) return;
var product = _bindingList
.FirstOrDefault(prod =>
prod.Code == Convert.ToInt32(CodeComboBox.Text) &&
prod.Parts == PartsComboBox.Text);
if (product == null)
{
_bindingList.Add(new Product()
{
Code = Convert.ToInt32(CodeComboBox.Text),
Parts = PartsComboBox.Text,
Count = count
});
}
else
{
product.Count = product.Count + count;
}
}
private void CurrentButton_Click(object sender, EventArgs e)
{
if (_bindingList.Count >0 && dataGridView1.CurrentRow != null)
{
var product = _bindingList[dataGridView1.CurrentRow.Index];
MessageBox.Show($"{product.Code}\n{product.Parts}\n{product.Count}");
}
else
{
// no current row
}
}
}
推荐阅读
- sql - 来自最大子记录数的表约束
- c# - SignalR 未通知数据库更改
- ios - 检查文本字段的有效输入(双精度,长度 2)
- android - Android Compose 生产准备好了吗?
- reactjs - 是的 / Formik 带去抖动的异步验证
- c++ - 编译器错误`在 if 语句中分配不兼容的类型`
- javascript - 无法让数据表处理简单的 JSON 数据
- node.js - NodeJs 中的内存泄漏
- maven - 在不使用 artifactId 的情况下安装 maven 生成的工件
- javascript - 使用greensock为具有相同类Javascript的元素独立触发动画