c#(WinForm)如何让DataGridView在最后一行增加合计功能?,谢谢啊

解决方案 »

  1.   

    this.dataGridView1.Rows.Add("dd","cc");
      

  2.   

    请问 DataGridView 的数据源是DataTable吗?参考
      

  3.   


    数据源是一个SQL查询的表, DataSet ds = datacon.getds(sql, "ALLuser");
                    this.dataGridView1.DataSource = ds.Tables[0];
      

  4.   

    如果你不是绑定数据源的话,就这样
    this.dataGridView1.Rows.Add("dd","cc");
    绑定了就在数据集里添加
    ds.Tables[0].Rows.Add("1","2","3");
      

  5.   

    哦 那很好 DataTable dt = ds.Table[0]; //ds是DataSetDataRow dr = dt.NewRow(); //在数据源上在追加一行
    dt.Rows.Add(dr);double total1 = 0;
    for (int i = 0; i < dt.Rows.Count -1; i++)  应该循环DataTable
    {
      total1 += Convert.ToDouble(dt.Rows[i]["列名"].ToString())
      //......
    }
    dt.Rows[dt.Rows.Count - 1]["列名"] = total1.ToString(); //不需要合计的列就给他""
    //......列还没写完datagridview1.DataSource = dt; //需要自己去扩展
    或者用SQL语句也可以实现的.....
      

  6.   

    public void AddRow(DataGridView dg, string value)
            {
                if (dg.Rows.Count > 1)
                {
                    DelRow(dg, dg.Rows.Count - 1);
                }
                dg.Rows.Add();
                for (int i = 0; i < dg.Columns.Count; i++)
                {
                    dg.Rows[dg.Rows.Count - 1].Cells[i].Value = value;
                }
                dg.CurrentCell = dg.Rows[dg.Rows.Count - 1].Cells[0];
                TotalRow(dg);
            }
         
        
            public void TotalRow(DataGridView dg)
            {
                dg.Rows.Add();
                DataGridViewRow dgr = dg.Rows[dg.Rows.Count - 1];
                dgr.ReadOnly = true;
                dgr.DefaultCellStyle.BackColor = System.Drawing.Color.Khaki;
                dgr.Cells[0].Value = "合计";
                for (int i = 0; i < dg.Rows.Count - 1; i++)
                {
                    dgr.Cells[2].Value = Convert.ToSingle(dgr.Cells[2].Value) + Convert.ToSingle(dg.Rows[i].Cells[2].Value);
                }
            }
      

  7.   


    如果第一列也要合计  可以把合计两个字写这里      dataGridView1.Rows[dt.Rows.Count - 1].HeaderCell.Value = "合计:";
          dataGridView1.RowHeadersWidth = 50;