【问题标题】:Count Duplicates and sum values in datagridview in C#在 C# 中的 datagridview 中计算重复值和求和值
【发布时间】:2021-06-14 12:45:35
【问题描述】:

我正在尝试在添加新行时计数并删除重复的行,如果该行不存在,它应该为它创建一个新行。 如果添加了新行并且与现有行之一相同,则将其添加到计数中。

它应该寻找是否: “代码”相同,如果“零件”相同。

我得到了什么:

现在更新代码: 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# winforms datagridview count duplicates


【解决方案1】:

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

【讨论】:

  • 效果很好!但前提是 Int“代码”只包含数字,但我有一些混合的地方,字母,数字和 -+。由于“e.KeyCode == Keys.Return”,我将“codecombobox”设置为文本框
  • 现在完美运行。非常感谢@Karen Payne
猜你喜欢
  • 2019-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-16
  • 2012-03-24
  • 2013-09-13
  • 2012-05-04
相关资源
最近更新 更多