zoukankan      html  css  js  c++  java
  • DataGridView 添加统计行

    文章有点潦草,不过应该看的懂,,,,

    2种方式:

    ----------------------------------------------------------------------------------------------------------------------------------

    方式一:(数据直接绑定)

    缺点:列值运输的值  直接写死的,如果需要添加写列值,完全需要重写绑定下,就这点有些不好,暂时也就这样弄了;
    这个希望多交流交流,,,

    列头排序

    添加两个事件,点击列头排序

    定义集合 记录排序数

     private List<object[]> list_obj = new List<object[]>();

      //点击排序
            private void dgvSubbill_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex >= 0 || this.dgvSubbill.Rows.Count == 0)
                    return;
                if (list_obj.Count == 0)
                {
                    colindex = e.ColumnIndex;
                    int RowIndex = this.dgvSubbill.Rows.Count - 1;
                    list_obj.Add(((DataTable)dgvSubbill.DataSource).Rows[RowIndex].ItemArray);
                    this.dgvSubbill.Rows.Remove(dgvSubbill.Rows[this.dgvSubbill.Rows.Count - 1]); //移除
                }
            }
            //排序后
            string direction = "DESC";
            private void dgvSubbill_Sorted(object sender, EventArgs e)
            {
                if (list_obj.Count == 0)
                    return;
                if (this.dgvSubbill.Rows.Count > 0)
                {
                    DataTable dt = ((DataTable)dgvSubbill.DataSource);
                    DataView View = dt.DefaultView;
                    if (direction == "ASC")
                    {
                        View.Sort = dt.Columns[colindex].ColumnName; //ASC DESC  默认升序
                        direction = "DESC";
                    }
                    else
                    {
                        View.Sort = dt.Columns[colindex].ColumnName + " " + "" + direction + ""; //DESC 降序
                        direction = "ASC";
                    }

                    dt = View.ToTable();
                    dt.Rows.Add(list_obj[0]);
                    list_obj.Clear();
                    dgvSubbill.DataSource = dt;
                    this.dgvSubbill.Rows[this.dgvSubbill.Rows.Count - 1].DefaultCellStyle.BackColor = Color.Cyan;
                }
            }

    统计行:

    dgvSubbill.DataSource = dtSubbill; //数据绑定

    if (this.dgvSubbill.Rows.Count > 0)
        {

     for (int i = 0; i < this.dgvSubbill.Rows.Count; i++)
                    {

    //运算 列值……

    }

    }

      //添加合计行(追加绑定行)
    int RowIndex = this.dgvSubbill.Rows.Count;
    DataRow dr = ((DataTable)dgvSubbill.DataSource).NewRow();

    dr[0] = ""; dr[7] = ""; dr[8] = ""; dr[9] = "";

    ………………

    ………………

      ((DataTable)dgvSubbill.DataSource).Rows.Add(dr);
    this.dgvSubbill.Rows[RowIndex].DefaultCellStyle.BackColor = Color.Cyan;  //合计行颜色 

    ------------------------------------------------------------------------------------------------------------------------------------------------------------------

     方式二:

    手动绑定

     private List<DataGridViewRow> list_row = new List<DataGridViewRow>();

      //合计行
                    DataGridViewRow dgv_row = new DataGridViewRow();
                    object[] objs = { "合计", "", "", dgv_TotalCharge, dgv_CostPackingList, dgv_TransportCost,""};

          dgv_row.CreateCells(this.dgvbsn_Subbill, objs);
                    dgv_row.DefaultCellStyle.BackColor = Color.Cyan;
                    dgvbsn_Subbill.Rows.Add(dgv_row);

      //排序后发生
            private void dgvbsn_Subbill_Sorted(object sender, EventArgs e)
            {
                if (this.dgvbsn_Subbill.Rows.Count > 0)
                {
                    this.dgvbsn_Subbill.Rows.Add(list_row[0]);
                    this.list_row.Clear();
                }
            }
            //用户点击时发生
            private void dgvbsn_Subbill_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
            {
                if (e.RowIndex >= 0 || this.dgvbsn_Subbill.Rows.Count == 0)
                    return;
                if (list_row.Count <= 0)
                {
                    int RowIndex = this.dgvbsn_Subbill.Rows.Count - 1;
                    list_row.Add(this.dgvbsn_Subbill.Rows[RowIndex]);
                    this.dgvbsn_Subbill.Rows.RemoveAt(RowIndex);
                }
            }

    -------------------------------------------------------------------------------------------------------------------------------------------

      //其他操作选择统计行之后的阻止(或 批量处理)     int RowIndex = dgvSubbill.Rows.Count - 2;
                    int RowsCount = this.dgvSubbill.SelectedRows[0].Index - 1;
                    if (RowsCount == RowIndex)
                        return;

  • 相关阅读:
    程序员佛祖保佑无bug、发发发 -注释代码
    27 友盟项目--azkaban资源调度
    26 友盟项目--数据可视化
    25 友盟项目--sqoop从hive导出数据到mysql
    24 友盟项目--优化-flume限速拦截、flume自定义源防丢失--改造exec源守护线程监控目录(防丢失)redis维护key(去重)
    23 友盟项目--sparkstreaming对接kafka、集成redis--从redis中查询月留存率
    22 友盟项目--sparkstreaming对接kafka、集成redis--从redis中存储用户使用app的最小时间戳min , 最大时间戳max
    21 友盟项目--统计连续活跃用户、近期流失用户、留存用户--创建表并插入选择出的数据
    20 友盟项目--统计月活率、沉默用户、周回流用户--创建表并插入选择出的数据
    19 友盟项目--统计新增用户---日新增、周新增、月新增--创建表并插入选择出的数据
  • 原文地址:https://www.cnblogs.com/quanxie/p/2995799.html
Copyright © 2011-2022 走看看