2011-08-31 49 views
2

我正在使用数据网格自动生成我的列。我怎样才能获得创建和赎回的专栏?我如何计算出我的色谱柱总数?

DbCommand dbCommand = db.GetStoredProcCommand("sel_TotalCount_p"); 
db.AddInParameter(dbCommand, "@pDateFrom", DbType.Date, datePicker1.Text); 
db.AddInParameter(dbCommand, "@pDateTo", DbType.Date, datepicker2.Text); 
ds = db.ExecuteDataSet(dbCommand); 

ds.Tables[0].Rows.Add("Total"); 

DataGrid2.DataSource = ds; 
DataGrid2.DataBind(); 

example datagrid

+0

什么是“创建”和“已兑换”列?我不明白你的意思。 – Oded

+0

这些是从SQL返回的列。 –

+0

如果您从存储过程获取值,为什么不让它返回总计呢? –

回答

3

能写 “的RowDataBound()” 事件,总结,高达seperately。像下面的东西。

public int totalCount = default(int); 

    protected void Test_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType.Equals(DataControlRowType.DataRow)) 
     { 
      int count = default(int); 
      string text = e.Row.Cells[0].Text; 
      int.TryParse(text, out count); 
      totalCount = totalCount + count; 
     } 
    } 

希望这是有用的!

+0

我必须在某个地方打电话吗?我如何将它实现到我的代码中?我将它作为我的totalcount代码下的新方法添加。 –

+0

如果它是GridView或“OnDataBound”(标签包含此事件),如果它是“.aspx页面”的数据网格并将此代码添加到相应的“”,则需要添加事件“RowDataBound”。 CS“文件,这会给你的计数。 – Praveen

0

如果你指的是列,而不是数量,并假设返回的DataSet的架构之和为你显示什么,然后尝试:

DataTable dt = ds.Tables[0]; 
int totalCreated = 0; 
int totalRedeemed = 0; 
foreach (DataRow dr in dt.Rows) 
{ 
    totalCreated += Convert.ToInt32(dr["Created"]); 
    totalRedeemed += Convert.ToInt32(dr["Redeemed"]); 
} 

DataRow r = dt.NewRow(); 
r["CreationDate"] = "Total"; 
r["Created"] = totalCreated; 
r["Redeemed"] = totalRedeemed; 
dt.Rows.Add(r); 

注意这增加了该行的数据源就DataGrid而言,它只是另一行数据。不要尝试排序或编辑DataGrid。