datagridview 增加统计行

xingn 2009-06-15 02:39:04
怎么在datagirview增加一个合计行
怎么在datagirview增加一个合计行
怎么在datagirview增加一个合计行
怎么在datagirview增加一个合计行
...全文
470 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
miandan 2010-01-22
  • 打赏
  • 举报
回复
添加统计数据到表最后一行的做法有问题,一点标题行排序就乱套了
mabaolin 2009-06-16
  • 打赏
  • 举报
回复
如果是sqlserver的话,查询一下rollup函数或cube函数
kevinhu520 2009-06-16
  • 打赏
  • 举报
回复
DataSet myds = datacon.getds(sql, "设备状况汇总表");
DataTable dt = myds.Tables["设备状况汇总表"];
for(int i = 0; i < dt.Rows.Count; i++)
{
sum1 += Int32.Parse(dt.Rows[i][5].ToString());
}
DataRow dr = dt.NewRow();
dr[1] = "合计";
dr[5] = sum1;
dt.Rows.Add(dr);
dataGridView1.DataSource = dt.DefaultView;
ds252743641 2009-06-15
  • 打赏
  • 举报
回复
string connectString = "Server=(local);Integrated Security=True;Database=northwind";
SqlConnection con = new SqlConnection(connectString);
con .Open();
SqlCommand command = con.CreateCommand();
command.CommandText= "select * from Customers";

DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(command);
adapter.Fill(ds, "Customers");

DataTable t = ds.Tables["Customers"];

//你要添加的行
DataRow r = t.NewRow();
r[0] = *****;
r[1] = ***;
t.Rows.Add(r);

this.dataGridView1.DataSource = t;
moonzap 2009-06-15
  • 打赏
  • 举报
回复
sql做法:
可以写个存储过程或者直接检索,自己决定.
以下内容是我直接打在这里的,没有进行过编辑,有问题请见谅,你自己确认下再:

1.select a,b from A where ... order by ...
2.select sum(c)as a ,sum(d) as b from B where ... group by ...
1中取得的内容类似是一行的数据,2中取得的内容就是集计后的内容
定义一个临时表比如#wkt(a,b)有两个字段,按照插入顺序先插入1,再插入2或者你自己指定好1,2共有的排序键.

插入临时表后,再通过画面取得此临时表的数据,绑定到dataGridView上.
j45kp 2009-06-15
  • 打赏
  • 举报
回复
/// <summary>
/// 在 GridView 控件中的某个行被绑定到一个数据记录时发生。此事件通常用于在某个行被绑定到数据时修改该行的内容。
/// </summary>
double sum = 0;
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex >= 0)
{
sum += Convert.ToDouble(e.Row.Cells[7].Text);
}
else if (e.Row.RowType == DataControlRowType.Footer)
{
e.Row.Cells[5].Text = "总薪水:";
e.Row.Cells[6].Text = sum.ToString();
e.Row.Cells[3].Text = "平均薪水:";
e.Row.Cells[4].Text = ((int)(sum / GridView1.Rows.Count)).ToString();
}
foreach (TableCell tc in e.Row.Cells)
{
tc.Attributes["style"] = "border-color:Black";
}
if (e.Row.RowIndex != -1)
{
int id = GridView1.PageIndex * GridView1.PageSize + e.Row.RowIndex + 1;
e.Row.Cells[0].Text = id.ToString();
}
}
lgqiu2008 2009-06-15
  • 打赏
  • 举报
回复
直通窗体统计应该ok
xxyping 2009-06-15
  • 打赏
  • 举报
回复
已经给你回信了!
xingn 2009-06-15
  • 打赏
  • 举报
回复
是从数据库取出数据直接绑定到datagridview上的
desegou 2009-06-15
  • 打赏
  • 举报
回复
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[3].Value = Convert.ToSingle(dgr.Cells[3].Value) + Convert.ToSingle(dg.Rows[i].Cells[3].Value);
}
}
窗口构造函数:FormHelper fh = new FormHelper();
fh.TotalRow(dataGridView1);
moonzap 2009-06-15
  • 打赏
  • 举报
回复
要什么形式的?是从数据库取出数据直接绑定到datagridview上吗?那样的话 可以用SQL实现
nwgogogo 2009-06-15
  • 打赏
  • 举报
回复
在绑定之前将数据源统计好后在数据源中加一行,再绑定就可以了。
xxyping 2009-06-15
  • 打赏
  • 举报
回复
如果你的数据是绑定的话,增加一行于你的绑定数据源一模一样的行,然后在这行数据里加入你的统计。。。将这个行添加到datagridview里

110,546

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧