private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = GetData();
}
private void button1_Click(object sender, EventArgs e)
{
DataGridView gw =dataGridView1;
Total(gw);
}
//合计行
public void Total(DataGridView dg)
{
DataGridViewRow dgr = dg.Rows[dg.Rows.Count - 1];
dgr.ReadOnly = true;
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);
}
}
private System.Data.DataTable GetData()
{
System.Data.DataTable tableInfo = new System.Data.DataTable();
DataColumn dctime = new DataColumn("Time", Type.GetType("System.String"));
DataColumn dcCity = new DataColumn("City", Type.GetType("System.String"));
DataColumn dcCount = new DataColumn("Count", Type.GetType("System.Int32"));
tableInfo.Columns.Add(dctime);
tableInfo.Columns.Add(dcCity);
tableInfo.Columns.Add(dcCount);
DataRow dr = tableInfo.NewRow();
dr["Time"] = "1:00";
dr["City"] = "10";
dr["Count"] = "15";
tableInfo.Rows.Add(dr);
DataRow dr1 = tableInfo.NewRow();
dr1["Time"] = "2:00";
dr1["City"] = "12";
dr1["Count"] = "19";
tableInfo.Rows.Add(dr1);
DataRow dr2 = tableInfo.NewRow();
dr2["Time"] = "3:00";
dr2["City"] = "13";
dr2["Count"] = "25";
tableInfo.Rows.Add(dr2);
DataRow dr3 = tableInfo.NewRow();
dr3["Time"] = "4:00";
dr3["City"] = "14";
dr3["Count"] = "10";
tableInfo.Rows.Add(dr3);
DataRow dr4 = tableInfo.NewRow();
dr4["Time"] = "5:00";
dr4["City"] = "15";
dr4["Count"] = "11";
tableInfo.Rows.Add(dr4);
DataRow dr5 = tableInfo.NewRow();
dr5["Time"] = "6:00";
dr5["City"] = "16";
dr5["Count"] = "17";
tableInfo.Rows.Add(dr5);
DataRow dr6 = tableInfo.NewRow();
dr6["Time"] = "7:00";
dr6["City"] = "17";
dr6["Count"] = "20";
tableInfo.Rows.Add(dr6);
DataRow dr7 = tableInfo.NewRow();
dr7["Time"] = "8:00";
dr7["City"] = "12";
dr7["Count"] = "13";
tableInfo.Rows.Add(dr7);
return tableInfo;
}c# DataGridView添加合计行的例子
最新推荐文章于 2025-09-20 13:49:54 发布
本文演示了如何使用C#编程语言实现数据表格的加载、读取和合计计算功能,包括创建数据集、填充数据行及计算合计行。
部署运行你感兴趣的模型镜像
您可能感兴趣的与本文相关的镜像
Llama Factory
模型微调
LLama-Factory
LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

936

被折叠的 条评论
为什么被折叠?



