dataGridView底部统计行如何实现

时间:2022-11-08 10:31:10
描述:
  目前有一datagridview控件,里面有个货币类型的列;我要实现的功能是,在此datagridview的底部固定一行,用于对货币列进行统计,并且当滚动垂直滚动条时,该列也一直保持在datagridview底部显示。
  麻烦各位帮忙想个办法实现这样的功能,谢谢

7 个解决方案

#1


加一个控件如何?

#2


可以通过重写DGV实现,具体代码在网上挺多的。

#3


通过数据源计算这两列的和就好。
然后在
DataRow DR = DataSet.Tables[0]NewRow();
DR[1] = "1000.01";
DataSet.Tables[0].Rows.Add(DR);

#4


google  baidu
DataRow dr = ds.Tables[0].NewRow();
dr[0] = "总价:";              
dr[1] = "200"; //总价200元吧
ds.Tables[0].Rows.InsertAt(dr, ds.Tables[0].Rows.Count-1);
                datagridview.DataSource=ds.Tables[0];

#5


挺麻烦的,因为你增加统计行以后,还要考虑到排序的问题。
如果你的datatable每一类是与类型的,还得考虑类型问题。

我当时做的是在底部加了panle,用lable显示,去重写太麻烦了

#6


支持熊猫,能实现最重要。

#7


dataGridView底部统计行如何实现

代码仅供参考:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataBindingComplete += delegate
            {
                AddLable(dataGridView1);
                SetSUM(dataGridView1, "PriceSUM");
            };

            dataGridView1.ColumnWidthChanged += delegate
            {
                AddLable(dataGridView1);
                SetSUM(dataGridView1, "PriceSUM");
            };

            using (SqlConnection conn = new SqlConnection("server=(local);database=Test;user id=sa;password=sa;"))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("Select top 10 * FROM Products", conn))
                {
                    DataTable table = new DataTable();
                    adapter.Fill(table);

                    dataGridView1.DataSource = table;
                }
            }

        }

        /// <summary>
        /// 添加统计行
        /// </summary>
        /// <param name="dataGridView"></param>
        public void AddLable(DataGridView dataGridView)
        {
            Label lblParent, lblChild;
            DataGridViewColumn column;
            int height = dataGridView.Height;
            int width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None);
            int scrollbarheight = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) > dataGridView.Width ? 16 : 0;//水平滚动条高
            int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : 0;//行标题宽度
            int length = rowheaderswidth;

            //
            if (dataGridView.Controls[dataGridView.Name + "____"] != null)
                dataGridView.Controls.Remove(dataGridView.Controls[dataGridView.Name + "____"]);

            lblParent = new Label();
            lblParent.Name = dataGridView.Name + "____";
            lblParent.BackColor = Color.Yellow;
            lblParent.Left = 1;
            lblParent.Top = height - scrollbarheight - 23 - 1;
            lblParent.Height = 23;
            lblParent.Width = width + rowheaderswidth - 1;
            dataGridView.Controls.Add(lblParent);

            for (int i = 0; i < dataGridView.Columns.Count; i++)
            {
                column = dataGridView.Columns[i];

                if (column.Visible)
                {
                    lblChild = new Label();
                    lblChild.Name = column.Name + "____";
                    lblChild.BackColor = Color.Transparent;
                    //lblChild.Text = lblChild.Name;
                    lblChild.AutoSize = true;
                    lblChild.Left = length + ((int)column.Width / 2) - 10 + 1;
                    length += column.Width;
                    lblChild.Top = 3;
                    lblParent.Controls.Add(lblChild);
                }
            }

        }

        /// <summary>
        /// 获取列总和
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <param name="columnName"></param>
        public void SetSUM(DataGridView dataGridView, string columnName)
        {
            if (dataGridView.Controls[dataGridView.Name + "____"] != null)
            {
                decimal sum = 0;

                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    try
                    {
                        if (dataGridView.Rows[i].Cells[columnName].Value != null)
                        {
                            sum += decimal.Parse(dataGridView.Rows[i].Cells[columnName].Value.ToString());
                        }
                    }
                    catch { }
                }

                dataGridView.Controls[dataGridView.Name + "____"].Controls[columnName + "____"].Text = sum.ToString();
            }
        }
    }
}


原文:http://www.mzwu.com/article.asp?id=2276
不足之处欢迎留言,一块完善,,,

#1


加一个控件如何?

#2


可以通过重写DGV实现,具体代码在网上挺多的。

#3


通过数据源计算这两列的和就好。
然后在
DataRow DR = DataSet.Tables[0]NewRow();
DR[1] = "1000.01";
DataSet.Tables[0].Rows.Add(DR);

#4


google  baidu
DataRow dr = ds.Tables[0].NewRow();
dr[0] = "总价:";              
dr[1] = "200"; //总价200元吧
ds.Tables[0].Rows.InsertAt(dr, ds.Tables[0].Rows.Count-1);
                datagridview.DataSource=ds.Tables[0];

#5


挺麻烦的,因为你增加统计行以后,还要考虑到排序的问题。
如果你的datatable每一类是与类型的,还得考虑类型问题。

我当时做的是在底部加了panle,用lable显示,去重写太麻烦了

#6


支持熊猫,能实现最重要。

#7


dataGridView底部统计行如何实现

代码仅供参考:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataBindingComplete += delegate
            {
                AddLable(dataGridView1);
                SetSUM(dataGridView1, "PriceSUM");
            };

            dataGridView1.ColumnWidthChanged += delegate
            {
                AddLable(dataGridView1);
                SetSUM(dataGridView1, "PriceSUM");
            };

            using (SqlConnection conn = new SqlConnection("server=(local);database=Test;user id=sa;password=sa;"))
            {
                using (SqlDataAdapter adapter = new SqlDataAdapter("Select top 10 * FROM Products", conn))
                {
                    DataTable table = new DataTable();
                    adapter.Fill(table);

                    dataGridView1.DataSource = table;
                }
            }

        }

        /// <summary>
        /// 添加统计行
        /// </summary>
        /// <param name="dataGridView"></param>
        public void AddLable(DataGridView dataGridView)
        {
            Label lblParent, lblChild;
            DataGridViewColumn column;
            int height = dataGridView.Height;
            int width = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None);
            int scrollbarheight = dataGridView.Columns.GetColumnsWidth(DataGridViewElementStates.None) > dataGridView.Width ? 16 : 0;//水平滚动条高
            int rowheaderswidth = dataGridView.RowHeadersVisible ? dataGridView.RowHeadersWidth : 0;//行标题宽度
            int length = rowheaderswidth;

            //
            if (dataGridView.Controls[dataGridView.Name + "____"] != null)
                dataGridView.Controls.Remove(dataGridView.Controls[dataGridView.Name + "____"]);

            lblParent = new Label();
            lblParent.Name = dataGridView.Name + "____";
            lblParent.BackColor = Color.Yellow;
            lblParent.Left = 1;
            lblParent.Top = height - scrollbarheight - 23 - 1;
            lblParent.Height = 23;
            lblParent.Width = width + rowheaderswidth - 1;
            dataGridView.Controls.Add(lblParent);

            for (int i = 0; i < dataGridView.Columns.Count; i++)
            {
                column = dataGridView.Columns[i];

                if (column.Visible)
                {
                    lblChild = new Label();
                    lblChild.Name = column.Name + "____";
                    lblChild.BackColor = Color.Transparent;
                    //lblChild.Text = lblChild.Name;
                    lblChild.AutoSize = true;
                    lblChild.Left = length + ((int)column.Width / 2) - 10 + 1;
                    length += column.Width;
                    lblChild.Top = 3;
                    lblParent.Controls.Add(lblChild);
                }
            }

        }

        /// <summary>
        /// 获取列总和
        /// </summary>
        /// <param name="dataGridView"></param>
        /// <param name="columnName"></param>
        public void SetSUM(DataGridView dataGridView, string columnName)
        {
            if (dataGridView.Controls[dataGridView.Name + "____"] != null)
            {
                decimal sum = 0;

                for (int i = 0; i < dataGridView.Rows.Count; i++)
                {
                    try
                    {
                        if (dataGridView.Rows[i].Cells[columnName].Value != null)
                        {
                            sum += decimal.Parse(dataGridView.Rows[i].Cells[columnName].Value.ToString());
                        }
                    }
                    catch { }
                }

                dataGridView.Controls[dataGridView.Name + "____"].Controls[columnName + "____"].Text = sum.ToString();
            }
        }
    }
}


原文:http://www.mzwu.com/article.asp?id=2276
不足之处欢迎留言,一块完善,,,