EPSupport2
asked on
Total in Gridview Footer
I am trying to calculate a row in my Gridview and have it show the total in the footer.
I have it working I think but I have a filter that needs to be applied, If someone could have a look and point me in the right direction.
Thanks
I have it working I think but I have a filter that needs to be applied, If someone could have a look and point me in the right direction.
Thanks
Aspx code:
<asp:GridView ID="GridView6" runat="server" AutoGenerateColumns="False"
ShowFooter="True" onrowdatabound="GridView6_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Work Hours">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("CompMinuteTotal").ToString()%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
Codebehind:
public partial class _Default : System.Web.UI.Page
{
private DataSet ds;
decimal grdTotal = 0;
protected void Page_Load(object sender, EventArgs e)
{
{
//DataSet ds = new DataSet();
ds = new DataSet();
string path = Server.MapPath("~/App_Data/");
string Path = path + "timesheet.xml";
ds.ReadXml(Path);
GridView1.DataMember = "TransmitBatches";
GridView2.DataMember = "Employees";
GridView3.DataMember = "TransmitTimeSheets";
GridView4.DataMember = "TransmitDay";
GridView5.DataMember = "TransmitAccountDistributions";
GridView6.DataMember = "TransmitDayCompensation";
GridView7.DataMember = "TransmitDayCompensation";
GridView1.DataSource = ds;
GridView2.DataSource = ds;
GridView3.DataSource = ds;
GridView4.DataSource = ds;
GridView5.DataSource = ds;
GridView6.DataSource = ds;
GridView7.DataSource = ds;
GridView1.DataBind();
GridView2.DataBind();
GridView3.DataBind();
GridView4.DataBind();
GridView5.DataBind();
GridView6.DataBind();
GridView7.DataBind();
}
}
protected void searchBtn_Click(object sender, EventArgs e)
{
if (ds == null) return;
string employeeName = txtClient.Text;
string employeeFilter = String.Format("EmployeeName LIKE '{0}%'", employeeName);
DataView dv2 = new DataView( ds.Tables["Employees"], employeeFilter,"EmployeeName", DataViewRowState.CurrentRows);
GridView2.DataSource = dv2;
GridView2.DataBind();
string employeeKeyFilter = "";
DataRow[] rows = ds.Tables["Employees"].Select(employeeFilter);
foreach (DataRow row in rows)
{
if (employeeKeyFilter != "") employeeKeyFilter += " AND ";
employeeKeyFilter += String.Format("EmployeeKey = {0}", row["EmployeeKey"]);
}
DataView dv3 = new DataView(ds.Tables["TransmitTimeSheets"], employeeKeyFilter, "EmployeeKey", DataViewRowState.CurrentRows);
GridView3.DataSource = dv3;
GridView3.DataBind();
//timesheet filter to filter by associated timesheet key
string timesheetsFilter = "";
rows = ds.Tables["TransmitTimeSheets"].Select(employeeKeyFilter);
foreach (DataRow row in rows)
{
if (timesheetsFilter != "") timesheetsFilter += " AND ";
timesheetsFilter += String.Format("TimesheetKey = {0}", row["TimesheetKey"]);
}
string basePayFilter = timesheetsFilter + " AND TransmitCompClassCode = 'BasePay'";
string workHoursFilter = timesheetsFilter + " AND TransmitCompClassCode = 'WorkHours'";
DataView dv4 = new DataView(ds.Tables["TransmitDay"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
GridView4.DataSource = dv4;
GridView4.DataBind();
DataView dv5 = new DataView(ds.Tables["TransmitAccountDistributions"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
GridView5.DataSource = dv5;
GridView5.DataBind();
DataView dv6 = new DataView(ds.Tables["TransmitDayCompensation"], workHoursFilter, "TimesheetKey", DataViewRowState.CurrentRows);
GridView6.DataSource = dv6;
GridView6.DataBind();
DataView dv7 = new DataView(ds.Tables["TransmitDayCompensation"], timesheetsFilter, "TimesheetKey", DataViewRowState.CurrentRows);
GridView7.DataSource = dv7;
GridView7.DataBind();
}
protected void clearbtn_Click(object sender, EventArgs e)
{
txtClient.Text = "";
}
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
decimal rowTotal = Convert.ToDecimal
(DataBinder.Eval(e.Row.DataItem, "CompMinuteTotal"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal.ToString("c");
}
}
sure, use this
decimal rowTotal = 0;
protected void GridView6_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
rowTotal = Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "CompMinuteTotal"));
grdTotal = grdTotal + rowTotal;
}
if (e.Row.RowType == DataControlRowType.Footer) {
Label lbl = (Label)e.Row.FindControl("lblTotal");
lbl.Text = grdTotal.ToString("c");
}
}
put this after line 21 (your original post)
decimal rowTotal = 0;
decimal rowTotal = 0;
sorry, ignore my posts ;)
to add a filter, do similar to this before binding to your grid...
datatable.DefaultView.RowF ilter = "active=1 and ctg <> '" & myCtg "'"
to add a filter, do similar to this before binding to your grid...
datatable.DefaultView.RowF
ASKER
Hain,
Thanks for the reply. I have it calculating the amount but the problem is it it gives the whole total from the xml file, How can I apply it to calculate just the info after I have filtered the data after clicking search button?
Thanks for the reply. I have it calculating the amount but the problem is it it gives the whole total from the xml file, How can I apply it to calculate just the info after I have filtered the data after clicking search button?
I could not get the problem exactly ;)
DataView dv6 = new DataView(ds.Tables["Transm itDayCompe nsation"], workHoursFilter, "TimesheetKey", DataViewRowState.CurrentRo ws);
--> add your filter here,dv6.rowfilter = "...", also set grdTotal = 0
GridView6.DataSource = dv6;
GridView6.DataBind();
and the code you already have will get the total based on your filtered data... you dont need to do anything else...
DataView dv6 = new DataView(ds.Tables["Transm
--> add your filter here,dv6.rowfilter = "...", also set grdTotal = 0
GridView6.DataSource = dv6;
GridView6.DataBind();
and the code you already have will get the total based on your filtered data... you dont need to do anything else...
GridView6_RowDataBound should run each time you call GridView6.Binddata()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
grdTotal is already set to 0 when it is declared @ Line 21
and rowTotal is used only in GridView6_RowDataBound, you dont need to set it to 0 ;)
anyways, good to know it is working fine now...
and rowTotal is used only in GridView6_RowDataBound, you dont need to set it to 0 ;)
anyways, good to know it is working fine now...
ASKER
Thanks for the help see above for code I need to add to make it work.
Bellow URL give some information
http://www.asp.net/(S(ywiyuluxr3qb2dfva1z5lgeg))/learn/data-access/tutorial-15-cs.aspx
http://msdn.microsoft.com/en-us/library/ms972833.aspx