在c#中以编程方式在gridview底部添加一行?

时间:2017-02-17 08:59:00

标签: c# asp.net gridview

我想在gridview的末尾添加一行,该行目前有5列。结束行将表示每列的值的总和。我怎样才能做到这一点 ?这是我到目前为止所尝试的...我的行将具有值nominatedquantity,allocatedquantity,volumne,provisionalusd,provisionalkes

using (SqlDataAdapter sda = new SqlDataAdapter())
{
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@Country", "KENYATEST");

    cmd.Connection = con;
    sda.SelectCommand = cmd;
    using (dt = new DataTable())
    {
      sda.Fill(dt);

      foreach (DataRow dr in dt.Rows)
      {
          allocatedsum += Convert.ToInt32(dr["AllocatedQuantity"]);
          nominatedsum += Convert.ToInt32(dr["NominatedQuantity"]);
          volume += Convert.ToDouble(dr["Volume"]);
          ProvisionalUSD += Convert.ToInt32(dr["ProvisionalUSD"]);
          ProvisionalKES += Convert.ToInt32(dr["ProvisionalKES"]);
      }                       

      GridView1.DataSource = dt;    
      GridView1.DataBind();
    }                                  
}

2 个答案:

答案 0 :(得分:1)

你快到了。您需要做的就是使用总值创建一个新行,并在使用它填充gridview之前将其添加到数据表中。

using (SqlDataAdapter sda = new SqlDataAdapter())
        {
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Country", "KENYATEST");

            cmd.Connection = con;
            sda.SelectCommand = cmd;
            using (dt = new DataTable())
            {

                sda.Fill(dt);

                foreach (DataRow dr in dt.Rows)
                {
                   allocatedsum += Convert.ToInt32(dr["AllocatedQuantity"]);
                   nominatedsum += Convert.ToInt32(dr["NominatedQuantity"]);
                   volume += Convert.ToDouble(dr["Volume"]);
                  ProvisionalUSD += Convert.ToInt32(dr["ProvisionalUSD"]);
                   ProvisionalKES += Convert.ToInt32(dr["ProvisionalKES"]);

                }
                //Create a row for totals over here
                DataRow totalRow = dt.NewRow();
                //Then set the total values inside this row
                totalRow["ColumnName"] = "Column Total"; //for each column
                dt.Rows.Add(totalRow); //finally add the new row to your dource datatable

                GridView1.DataSource = dt;

                GridView1.DataBind();
            }
} }

答案 1 :(得分:0)

在将dt分配给数据源之前,只需添加它......

 <ListView
    android:id="@+id/productListview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>