datagridview中的行总和(C#) [英] sum of rows in datagridview (c#)

查看:68
本文介绍了datagridview中的行总和(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何对行求和以在TOTAL列中显示总数?它必须添加便装,才能,泳衣,礼服和问答..

看起来像这样.

休闲装!天赋 !泳装 !礼服!问题和答案 !总计!

          12          12          12            12                    12                     < -----------------总和应显示在这里

解决方案

你好

以下是一个起点,该起点依赖于带有表达式的DataColumn,并且DataGridView设置了它的DataSource.如果不愿意使用DataTable,请在此处停止.

使用系统;
使用System.Data;
使用System.Linq;
使用System.Windows.Forms;

命名空间WindowsFormsApplication1
{
    公共局部类Form1:表单
    {
        private string [] ColumnNames = {"C1","C2","C3" };
        私人BindingSource bsData = new BindingSource();
        公共Form1()
        {
            InitializeComponent();
        }
        私人void UpDateTotal()
        {
            数据表dt =(数据表)bsData.DataSource;
            如果(bsData.Position!= -1)
            {
                如果(!((((DataRowView)bsData.Current).Row.RowState == DataRowState.Detached))
                {
                    dt.Rows [bsData.Position] .EndEdit();
                }
            }
        }
        私有void _RowChanged(对象发送者,DataRowChangeEventArgs e)
        {
            if(e.Action == DataRowAction.Add || e.Action == DataRowAction.Change || e.Action == DataRowAction.Commit || e.Action == DataRowAction.Change)
            {
                UpDateTotal();
            }
        }
        私有void _ColumnChanged(对象发送者,DataColumnChangeEventArgs e)
        {
            如果(!(e.Row.RowState == DataRowState.Deleted))
            {
                如果(ColumnNames.Contains(e.Column.ColumnName))
                {
                    如果(!(e.Row.RowState == DataRowState.Detached))
                    {
                        如果(Convert.IsDBNull(e.Row [e.Column.ColumnName]))
                        {
                            e.Row [e.Column.ColumnName] = 0;
                        }

                        e.Row.AcceptChanges();
                    }

                    UpDateTotal();
                }
            }
        }
        私有void Form1_Load(对象发送者,EventArgs e)
        {
            var dt = new DataTable();

            dt.Columns.Add(new DataColumn(){ColumnName ="C1",DataType = typeof(int)});
            dt.Columns.Add(new DataColumn(){ColumnName ="C2",DataType = typeof(int)});
            dt.Columns.Add(new DataColumn(){ColumnName ="C3",DataType = typeof(int)});

            dt.Columns.Add(new DataColumn(){ColumnName ="Total",DataType = typeof(int),Expression ="C1 + C2 + C3"});

            dt.Rows.Add(new object [] {12,12,12});
            dt.Rows.Add(new object [] {10,6,1});

            bsData.DataSource = dt;
            dataGridView1.DataSource = bsData;
            dt.ColumnChanged + = _ColumnChanged;
            dt.RowChanged + = _RowChanged;
        }
    }
}


how do you sum the rows to appear the total in the TOTAL column????? it has to add the casual attire, talent, swimsuit, gown and question and answer.. 

it looks like this.

Casual attire ! talent ! swimsuit ! gown ! question and answer ! TOTAL !

          12          12          12            12                    12                      <------------- the sum should show here

解决方案

Hello,

The following is a starting point which relies on a DataColumn with an Expression and the DataGridView has it's DataSource set. If not willing to use a DataTable then stop here.

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string[] ColumnNames = {"C1","C2","C3" };
        private BindingSource bsData = new BindingSource();
        public Form1()
        {
            InitializeComponent();
        }
        private void UpDateTotal()
        {
            DataTable dt = (DataTable)bsData.DataSource;
            if (bsData.Position != -1)
            {
                if (!(((DataRowView)bsData.Current).Row.RowState == DataRowState.Detached))
                {
                    dt.Rows[bsData.Position].EndEdit();
                }
            }
        }
        private void _RowChanged(object sender, DataRowChangeEventArgs e)
        {
            if (e.Action == DataRowAction.Add || e.Action == DataRowAction.Change || e.Action == DataRowAction.Commit || e.Action == DataRowAction.Change)
            {
                UpDateTotal();
            }
        }
        private void _ColumnChanged(object sender, DataColumnChangeEventArgs e)
        {
            if (!(e.Row.RowState == DataRowState.Deleted))
            {
                if (ColumnNames.Contains(e.Column.ColumnName))
                {
                    if (!(e.Row.RowState == DataRowState.Detached))
                    {
                        if (Convert.IsDBNull(e.Row[e.Column.ColumnName]))
                        {
                            e.Row[e.Column.ColumnName] = 0;
                        }

                        e.Row.AcceptChanges();
                    }

                    UpDateTotal();
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            var dt = new DataTable();

            dt.Columns.Add(new DataColumn() { ColumnName = "C1", DataType = typeof(int) });
            dt.Columns.Add(new DataColumn() { ColumnName = "C2", DataType = typeof(int) });
            dt.Columns.Add(new DataColumn() { ColumnName = "C3", DataType = typeof(int) });

            dt.Columns.Add(new DataColumn() { ColumnName = "Total", DataType = typeof(int), Expression = "C1 + C2 + C3" });

            dt.Rows.Add(new object[] {12,12,12 });
            dt.Rows.Add(new object[] {10, 6, 1 });

            bsData.DataSource = dt;
            dataGridView1.DataSource = bsData;
            dt.ColumnChanged += _ColumnChanged;
            dt.RowChanged += _RowChanged;
        }
    }
}


这篇关于datagridview中的行总和(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆