如何对最后一列中的列值求和 [英] How to sum column value in last column

查看:170
本文介绍了如何对最后一列中的列值求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从此代码中汇总最后一列表中的值列



How to summing value column in the last column table from this code

for (int g = 0; g <= cat.Length - 1; g++)
{
    string Category = cat[g];
    DataRow dr1 = dt.NewRow();
    dr1.SetField("Name", dr2["Name"]);
    dr1.SetField("Class", "Class " + Category);
    var i = 1;
    while (i <= days)
    {
        string day = year + "-" + month + "-" + i;
        DateTime date = Convert.ToDateTime(day);
        day = date.ToString("yyyy-MM-dd");
        SqlConnection conView = new SqlConnection(strConView);
        SqlCommand cmdView = new SqlCommand();
	cmdView.CommandText = "SELECT  * FROM Student WHERE Name = '" + dr2["Name"] + "' AND YEAR(Date)=" + year + " AND MONTH(Date)=" + month + "";
        cmdView.Connection = conView;
		SqlDataAdapter adapter = new SqlDataAdapter(cmdView);
		DataTable dt1 = new DataTable();
		adapter.Fill(dt1);
		for (int x = 0; x <= dt1.Rows.Count - 1; x++)
		{
			DataRow dr3 = dt1.Rows[x];
			if (Convert.ToDateTime(drQUnit["Date"]).ToString("yyyy-MM-dd") == day)
			{
				dr1.SetField("Date " + i, dr3[Category]);
			}
		}
		i += 1;
	}
}

推荐答案

您可以只使用数据网格列表视图或控件的行数据绑定事件你的使用。并在那里形成你可以访问整行。然后计算这些值并将其显示在最后一列
You can just use row data bound event of the datagrid list view or the control your using. and form there you can access entire row. then calculate the values and display it in the last column


我完成它,下面的代码放在datarowbound中。

well i finish it with this code below put in datarowbound.
protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
            {
                total = 0;
                int intex = GetColumnIndexByName(e.Row, "Category");
                if (e.Row.Cells[intex].Text == "Class")
                {
                    var jo = 1;
                    var ro = 2;
                    while (jo <= days)
                    {
                        if (e.Row.Cells[ro].Text == " ")
                        {
                            e.Row.Cells[ro].Text = "0";
                        }
                        if (!Convert.IsDBNull(e.Row.Cells[ro].Text))
                        {
                            _total[jo] += Convert.ToDecimal(e.Row.Cells[ro].Text);
                            total = total + Convert.ToDecimal(e.Row.Cells[ro].Text);
                            if (jo == days)
                            {
                                e.Row.Cells[ro + 1].Text = Convert.ToString(total);
                            }
                        }
                        ro += 1;
                        jo += 1;
                    }
                }
            }
}


这篇关于如何对最后一列中的列值求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆