【问题标题】:How to Sum datagridview Column by Checkbox to display on the textbox如何通过复选框对datagridview列求和以显示在文本框上
【发布时间】:2021-09-14 09:14:40
【问题描述】:

我需要一些帮助。我有一个有 9 列的 DataGridView。第一列是Checkbox - 如果选中复选框,则值为真。第三列是Gross Weight。我需要对选中复选框的每一行的总重量求和,并将其显示在txtGW.Text

Gross Weight 中的值是双精度值。

这是我的代码:

for (int i = 0; i < datagridviewLog.Rows.Count; i++)
{
    string temp2 = datagridviewLog.Rows[i].Cells[0].Value.ToString();

    // if cell is 0 = true / checked
    if (temp2.ToLower() == "true") 
    {
        double sum = 0;
        sum += double.Parse(datagridviewLog.Rows[i].Cells[3].Value.ToString());
        txtBKselect.Text = sum.ToString();
    }
}

【问题讨论】:

  • 不要使用图片来展示您的代码,而是使用工具栏中的代码按钮发布代码。
  • @KarenPayne 哦,对不起,我是这里的新手。

标签: c# datagridview


【解决方案1】:

考虑通过创建一个代表要显示的数据的class 来加载您的DataGridView,而不是触摸单元格数据,然后将此数据的列表分配给BindingSource,其中BindingSource 成为DataGridView 的DataSource。

此外,您可以通过将 BindingSource.Current 转换为列表类型来访问行/单元格数据。

在以下示例中,GetCheckedButton_Click 使用 lambda 从 BindingSource 中选择,然后是 summing 双属性 Value

总和被放入一个 TextBox 中,或者如果没有行检查,则 TextBox 的 .Text 属性被清空。

namespace DataGridViewCheckedSum
{
    public partial class Form1 : Form
    {
        private readonly BindingSource _bindingSource = 
            new BindingSource();
        
        public Form1()
        {
            InitializeComponent();
            
            Shown += OnShown;
        }

        private void OnShown(object sender, EventArgs e)
        {

            // Mocked data for demonstration of getting checked and sum
            _bindingSource.DataSource = new List<ItemRecord>()
            {
                new ItemRecord() {Selected = false, Value = 10},
                new ItemRecord() {Selected = false, Value = 5},
                new ItemRecord() {Selected = false, Value = 20},
                new ItemRecord() {Selected = false, Value = 15},
            };

            dataGridView1.DataSource = _bindingSource;
            
            dataGridView1.Columns[0].HeaderText = "";
            dataGridView1.Columns[0].Width = 30;
        }


        /// <summary>
        /// Get checked from column 0, get sum of second column
        /// and display sum or nothing if nothing checked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void GetCheckedButton_Click(object sender, EventArgs e)
        {
            var results = ((List<ItemRecord>)_bindingSource.DataSource)
                .Where(itemRecord => itemRecord.Selected).ToList();

            sumTextBox.Text = results.Count == 0 ? 
                "" : 
                $"{results.Sum(itemRecord => itemRecord.Value)}";

        }

        private void CurrentRowButton_Click(object sender, EventArgs e)
        {
            if (_bindingSource.Current != null)
            {
                ItemRecord current = (ItemRecord)_bindingSource.Current;
                MessageBox.Show($"{(current.Selected ? "Yes" : "No")} {current.Value}");
            }
            else
            {
                MessageBox.Show("No current row");
            }
        }
    }

    /// <summary>
    /// Place in it's own class file
    /// </summary>
    class ItemRecord
    {
        public bool Selected { get; set; }
        public double Value { get; set; }
    }
}

【讨论】:

    猜你喜欢
    • 2011-11-13
    • 2017-12-25
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多