转贴guoxhvip的包中的代码,以便有和我一样想看代码的学习:
private void Form1_Load(object sender, EventArgs e)
{
ds = new DataSet();
ds.Tables.Add("policeStation");
ds.Tables["policeStation"].Columns.Add("编号",typeof(string));
ds.Tables["policeStation"].Columns.Add("所属派出所",typeof(string));
ds.Tables["policeStation"].Columns.Add("汽车", typeof(int));
ds.Tables["policeStation"].Columns.Add("自行车", typeof(int));
ds.Tables["policeStation"].Columns.Add("计算机", typeof(int));
ds.Tables["policeStation"].Columns.Add("扫描仪", typeof(int));
ds.Tables["policeStation"].Rows.Add(new object[] {"1","龙头派出所",1,1,1,1});
ds.Tables["policeStation"].Rows.Add(new object[] {"2","新可镇派出所",12,12,12,3});
ds.Tables["policeStation"].Rows.Add(new object[] {"3","贤官镇派出所",12,21,0,0});
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
private void btnAdd_Click(object sender, EventArgs e)
{
object[] newRow = new object[6];
int[] sum = new int[4];
foreach (DataRow row in ds.Tables[0].Rows)
{
sum[0] += int.Parse(row["汽车"].ToString());
sum[1] += int.Parse(row["自行车"].ToString());
sum[2] += int.Parse(row["计算机"].ToString());
sum[3] += int.Parse(row["扫描仪"].ToString());
}
int i = 0;
newRow[i++] = "";
newRow[i++] = "统计";
newRow[i++] = sum[0];
newRow[i++] = sum[1];
newRow[i++] = sum[2];
newRow[i++] = sum[3];
this.ds.Tables[0].Rows.Add(newRow);
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
[此贴子已经被作者于2007-11-21 11:53:34编辑过]