in a gridview i am trying to show total of a project's expenditure during the year this has be in accordance to year (like 2011-2012, 2012-2013 etc.), i have following columns salary,T.A, contingency, NRC, institutional charges and others.I only want a single row in which i can show the summarized data, the the approach i am using to achieve this is:
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
row.Visible = row.RowIndex.Equals(1);
}
}
i want the result to be in this format [URL=http://www.image-share.com/ijpg-2143-192.html][IMG]http://www.image-share.com/upload/2143/192m.jpg[/IMG][/URL]
so what i am doing is in all the columns i am inserting labels and then i am trying to display the result on them. what basically is getting wrong is my query is not producing desired output according to years.
here is the code i am using:
protected void Button1_Click(object sender, EventArgs e)
{
string str = string.Format(@"select m.pcode, m.fyyear, m.salary, m.ta, m.contigency, m.nrc, m.institcharges, m.others,y.yearlyalloc,y.salary as sal1,y.ta as ta1,y.contigency as cont1,y.nrc as nrc1,y.institcharges as inst1,y.others as other1 FROM monthly AS m inner join yearly y on m.pcode=y.pcode where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND y.fyyear like('" + DropDownList2.SelectedItem.ToString() + "')", con);
SqlDataAdapter da = new SqlDataAdapter(str, con);
DataTable dtNew = new DataTable();
da.Fill(dtNew);
GridView1.DataSource = dtNew;
GridView1.DataBind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString);
if (e.Row.RowType == DataControlRowType.DataRow)
{
t1 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "salary"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbsal = (Label)e.Row.FindControl("lbsal");
lbsal.Text = t1.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t2 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "ta"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbta = (Label)e.Row.FindControl("lbta");
lbta.Text = t2.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t3 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "contigency"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbcont = (Label)e.Row.FindControl("lbcont");
lbcont.Text = t3.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t4 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "nrc"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbnrc = (Label)e.Row.FindControl("lbnrc");
lbnrc.Text = t4.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t5 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "institcharges"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbinst = (Label)e.Row.FindControl("lbinst");
lbinst.Text = t5.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t6 += Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "others"));
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbothers = (Label)e.Row.FindControl("lbothers");
lbothers.Text = t6.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t7 = t1 + t2 + t3 + t4 + t5 + t6;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label mtotal = (Label)e.Row.FindControl("mtotal");
mtotal.Text = t7.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t8 = Convert.ToDouble(((DataRowView)e.Row.DataItem)["yearlyalloc"].ToString());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label tyear = (Label)e.Row.FindControl("tyear");
tyear.Text = t8.ToString();
}
if (e.Row.RowType == DataControlRowType.DataRow)
{
t8 = Convert.ToDouble(((DataRowView)e.Row.DataItem)["yearlyalloc"].ToString());
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label tyear = (Label)e.Row.FindControl("tyear");
tyear.Text = t8.ToString();
}
}