C# DataGridView合计行

在网上搜了很多关于DataGridView合计行的设计及源码,都不是很合我心意。于是自己写了一个关于合计行的DLL。以后每次要用到合计行的时候只要引用这个DLL就可以了。

 

效果图如下:

 

 

 

 

引用Dll:

 

附上例子源码:

 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        TSumRow.TSumCols tsc = new TSumRow.TSumCols();
        List<Jyinfo> ListJy = new List<Jyinfo>();

        private void Form1_Load(object sender, EventArgs e)
        {
            DGV.AutoGenerateColumns = false; //禁止自动创建列
            DGV.AllowUserToResizeColumns = false; //禁止自动调整列宽
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetData();
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        private void GetData()
        {
            Jyinfo j = null;
            List<Jyinfo> listjy = new List<Jyinfo>();
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=Test;User ID=sa;Password=123456"))
            {
                conn.Open();
                SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"SELECT [Id],[Name],[Age],[Sl],[Zl],[Dj],[Je] FROM [dbo].[JyInfo]";
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    j = new Jyinfo();

                    j.Id = Convert.ToInt32(reader["Id"]);
                    j.Name = reader["Name"].ToString();
                    j.Age = Convert.ToInt32(reader["Age"]);
                    j.Sl = Convert.ToInt32(reader["Sl"]);
                    j.Zl = Convert.ToInt32(reader["Zl"]);
                    j.Dj = Convert.ToInt32(reader["Dj"]);
                    j.Je = Convert.ToInt32(reader["Je"]);

                    listjy.Add(j);
                }
                reader.Close();
            }
            ListJy = listjy;
            DGV.DataSource = ListJy;


            #region 汇总行  《以下就是显示DataGridView合计行的代码》
            List<TSumRow.TColumns> listTc = new List<TSumRow.TColumns>();
            TSumRow.TColumns Tc = new TSumRow.TColumns();
            Tc.Col_Name = "Col_Id";    //DataGridView列的序号列列名
            Tc.Col_Value = "合计:";   //要显示在合计行的值
            listTc.Add(Tc);

            Tc = new TSumRow.TColumns();
            Tc.Col_Name = "Col_Name";    //DataGridView列的姓名列列名
            var q = from b in ListJy
                    group b by b.Name;
            Tc.Col_Value = q.Count().ToString();//汇总多少个客户
            listTc.Add(Tc);

            Tc = new TSumRow.TColumns();
            Tc.Col_Name = "Col_dj";     //DataGridView列的单价列列名
            Tc.Col_Value = string.Format("{0:F2}", ListJy.Average(a => a.Dj));//平均单价
            listTc.Add(Tc);

            Tc = new TSumRow.TColumns();
            Tc.Col_Name = "Col_je";    //DataGridView列的金额列列名
            Tc.Col_Value = string.Format("{0:F2}", ListJy.Sum(a => a.Je)); ;//总的销售额
            listTc.Add(Tc);

            tsc.AddHz(listTc, DGV, pan);
            #endregion
        }
    }

    /// <summary>
    /// 交易信息实体类
    /// </summary>
    [Serializable]
    public class Jyinfo
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public int Sl { get; set; }
        public int Zl { get; set; }
        public int Dj { get; set; }
        public int Je { get; set; }
    }
}
View Code

合计我使用的是Panel对象。

定义对象:Panel对象名为pan,DataGridView对象名为DGV

 

合计行DLL下载链接(永久有效):http://pan.baidu.com/s/1c2mtZ1I

 

转载请说明出处:http://www.cnblogs.com/haibing0107/p/6823516.html 作者:涂山吕吕

 

posted @ 2017-05-08 09:48  涂山吕吕  阅读(5505)  评论(0编辑  收藏  举报