简体   繁体   English

ASP.NET C#将行添加到数据集

[英]ASP.NET c# adding row to dataset

i have a dataset which im using to calculate some sales figures, this data set has about 15 columns i want to add a new row to the end of the dataset to calculate the total of each column. 我有一个数据集,我正在使用该数据集来计算一些销售数字,该数据集大约有15列,我想在数据集的末尾添加新行以计算每一列的总数。 heres an example of what the dataset looks like 这是数据集的示例

NAME | GP   | ORD_GP | EXP   | TOTAL GP
a      206     48      -239     15 
b      0       27       0        27

so what i want to be able to do is add another row at the end which will calulate the sum of each row so it would look something like 所以我想要做的是在末尾添加另一行,这将计算每一行的总和,因此它看起来像

NAME | GP   | ORD_GP | EXP   | TOTAL GP
a      206     48      -239     15 
b      0       27       0       27
TOTAL  206     75       -239    42

so far i have 到目前为止,我有

    ds.Tables[0].Rows.Add("TOTAL");
    foreach (DataColumn dc in ds.Tables[0].Columns)
    {
      // add upp column data and put into toal field
    }

Have a look at the DataTable.Compute method: 看一下DataTable.Compute方法:

private void AddTotalRow(DataTable dt)
{
    DataRow dr = dt.NewRow();
    dr["NAME"] = "TOTAL";
    dr["GP"] = dt.Compute("Sum(GP)", null);
    dr["ORD_GP"] = dt.Compute("Sum(ORD_GP)", null);
    dr["EXP"] = dt.Compute("Sum(EXP)", null);
    dr["TOTAL_GP"] = dt.Compute("Sum(TOTAL_GP)", null);
    dt.Rows.Add(dr);
}

You would call this function only once, for example: 您只需调用一次此函数,例如:

AddTotalRow(ds.Tables[0]);
//Now the first DataTable in your DataSet has an additonal record with the total values

Edited according to your new informations 根据您的新信息进行编辑

How to: Add Rows to a DataTable 如何:将行添加到数据表

DataRow newCustomersRow = dataSet1.Tables["Customers"].NewRow();

newCustomersRow["CustomerID"] = "ALFKI";
newCustomersRow["CompanyName"] = "Alfreds Futterkiste";

dataSet1.Tables["Customers"].Rows.Add(newCustomersRow);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM