计算 C# 中 datagridview 中的重复项和求和值
Count Duplicates and sum values in datagridview in C#
我正在尝试在添加新行时计数并删除重复行,如果该行不存在,它应该为其创建一个新行。
如果添加了新行并且与现有行之一相同,则会将其添加到计数中。
它应该寻找是否:
'Code' 是相同的,如果 'Part' 是相同的。
我得到的:
现在更新代码:
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();
}
Class已更新为字符串:
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));
}
}
一种解决方案是使用具有具体 class 的 BindingList,而不是直接从 DataGridView 工作。在下面的示例中,有两个 ComboBox 控件用于 Code 和 Parts,一个 TextBox 用于 Count。
Class 存储信息
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判断一个Product是否存在,如果存在则添加计数,如果不存在则添加新记录。请注意,如果您需要检查或更改 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
}
}
}
我正在尝试在添加新行时计数并删除重复行,如果该行不存在,它应该为其创建一个新行。 如果添加了新行并且与现有行之一相同,则会将其添加到计数中。
它应该寻找是否: 'Code' 是相同的,如果 'Part' 是相同的。
我得到的:
现在更新代码: 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();
}
Class已更新为字符串:
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));
}
}
一种解决方案是使用具有具体 class 的 BindingList,而不是直接从 DataGridView 工作。在下面的示例中,有两个 ComboBox 控件用于 Code 和 Parts,一个 TextBox 用于 Count。
Class 存储信息
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判断一个Product是否存在,如果存在则添加计数,如果不存在则添加新记录。请注意,如果您需要检查或更改 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
}
}
}