Click here to Skip to main content
16,017,707 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to summing value column in the last column table from this code

C#
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;
	}
}
Posted
Updated 26-Mar-13 16:36pm
v2

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
 
Share this answer
 
Comments
Gun_And_Ar 31-Mar-13 21:58pm    
can you give me sample code Chamika?
well i finish it with this code below put in datarowbound.
C#
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;
                    }
                }
            }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900