• 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;

  • 相关阅读:
    &【12】Python 函数
    &【11】Python 流程控制
    &【09】Python 数据类型之dictionary
    &【07】Python 数据类型之元组
    &【08】Python 数据类型之set
    &【06】Python 数据类型之list
    &【05】Python 彻底搞懂分片操作
    &【04】Python 数据结构之序列
    SpringBoot-HelloWorld(三)
    SpringBoot-了解微服务(二)
  • 原文地址:https://www.cnblogs.com/quanxie/p/2995799.html
Copyright © 2020-2023  润新知