简体   繁体   English

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

[英]Adding a row at the bottom of the gridview programmatically in c#?

I would like to add a row at the end of my gridview which has 5 columns as of now. 我想在我的gridview的末尾添加一行,到目前为止,它有5列。 The end row will represent the total of the values of each column. 最后一行将代表每列值的总和。 How can i do that ? 我怎样才能做到这一点 ? This is what I tried so far..My row will have the values 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();
    }                                  
}

You are almost there. 你快到了。 All you need to do is create a new row with the total values and add it to the data table, before you populate the gridview with it. 您需要做的就是用总值创建一个新行并将其添加到数据表中,然后再用它填充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();
            }
} }

Just add this before you assign dt to your datasource... 只需在添加dt到数据源之前添加它即可。

 DataRow totalRow = dt.NewRow();
                totalRow["ColumnName"] = "Column Total"; 
                dt.Rows.Add(totalRow);

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

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