Click here to Skip to main content
15,905,420 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
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:

C#
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:
C#
/*to populate gridview*/
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();
         }
    }
Posted
Updated 4-Apr-13 22:51pm
v2

if ur DropDownList2 contains items like "2012-2013" then try following query
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(" + String.Join(",",DropDownList2.SelectedItem.Value.Split('-')) + ")", con); 
 
Share this answer
 
Comments
a2ulthakur 5-Apr-13 6:12am    
@pallavi what will this do ?? plus did you see the image link that i have posted.
protected void GridView1_DataBound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
row.Visible = row.RowIndex.Equals(1);
}

}
is this the right method to get only one row and if i take only one row will it take only one rows value from database table also ... currently its taking value of all the rows anyhow
Pallavi Waikar 5-Apr-13 6:17am    
if want one row use group by include other field related to amount in sum and u will get result....and if ur DropDownList2 contains values like "2012-2013"it will not going to work if y.fyyear value in database in like 2012
a2ulthakur 5-Apr-13 6:19am    
the value in my database is entered in 2011-2012 format only
a2ulthakur 5-Apr-13 6:23am    
the financial year starts from april and end in march of next year. is there any way possible for me to limit the entries from april to march..like any validation which prevents me from entering values based on my year selection. let me explain it i think m complicating it. if i select a year 2011-2012 from a dropdownlist can there be any way that in a textbox i can limit values from april to march of this year range.
Pallavi Waikar 5-Apr-13 6:29am    
ry this query
string str = string.Format(@"select m.pcode, m.fyyear ,sum(m.salary),sum(m.ta),sum(m.contigency),sum(m.nrc),
sum(m.institcharges),sum(m.others),sum(y.yearlyalloc),sum(y.salary) as sal1,sum(y.ta) as ta1,sum(y.contigency) as cont1,
sum(y.nrc) as nrc1,sum(y.institcharges) as inst1,sum(y.others) as other1 FROM monthly AS m , yearly as y
where m.pcode=y.pcode and m.pcode=('" + DropDownList1.SelectedItem.Value.ToString() + "' ) AND
y.fyyear like('" + DropDownList2.SelectedItem.Value.ToString() + "') and m.fyyear= y.fyyear
group by m.pcode, m.fyyear", con);
the following query did the trick


SQL
string str = string.Format(@"select m.pcode,sum( m.salary) as SalaryConsumed, y.salary,sum(m.ta) as taConsumed,y.ta,sum(m.contigency) as contiused, y.contigency,sum(m.nrc) as nrcconsumed,y.nrc,sum(m.institcharges) as institgiven,y.institcharges,sum(m.others) as miscused,y.others, m.fyyear,y.yearlyalloc from monthly m
        inner join yearly y on m.pcode = y.pcode and m.fyyear = y.fyyear where m.pcode=('" + DropDownList1.SelectedItem.ToString() + "' ) AND m.fyyear=('" + DropDownList2.SelectedItem.ToString() + "')group by m.fyyear, m.pcode, y.salary, y.ta,y.contigency,y.nrc,y.institcharges,y.others,y.yearlyalloc", con);
 
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