首页 > 解决方案 > 在 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));
            }
        }

标签: c#winformsdatagridviewcountduplicates

解决方案


一种解决方案是使用具有具体类的 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
        }
    }
}

在此处输入图像描述


推荐阅读