GRID View footer column calculation [Resolved]

Posted by Vijetha under C# on 3/12/2012 | Points: 10 | Views : 25916 | Status : [Member] | Replies : 18
Hi,
I have a grid view which consists of 8 columns namely SKU,qty(textbox),item name,UPC,Price,total,Update button,Remove button.
On click of update btn the total will be calculated which is working fine. And i need the grand total in the footer of my grid which sum ups the total.
Im very new to C#.So i need the aspx coding and .cs coding help on this.
Waiting for the reply.

Regards,
Vijetha.M.M




Responses

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 50

Up
0
Down

Resolved
Hi vijetha here is solution:

protected void btncalculate_Click(object sender, EventArgs e)
{

foreach (GridViewRow dr in grd5.Rows)
{
double price = Convert.ToDouble(((Label)dr.FindControl("lblRPrice")).Text);
int Qty = int.Parse(((TextBox)dr.FindControl("txtQty")).Text);

double total = price * Qty;
((Label)dr.FindControl("lbltotal")).Text = Convert.ToString(total);
grandtotal = grandtotal + total;

}
GridViewRow row = grd5.FooterRow;
((Label)row.FindControl("lblgrandtotal")).Text = Convert.ToString(grandtotal);
}




Mark this as answer, if it helps you................

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/16/2012 [Member] Silver | Points: 50

Up
0
Down

Resolved

When you are using RowDataBound every row gets processed, if you maintain a status with 0 and 1 in invisible level or Hidden field it is easier to check the value of that row only instead of going to check the entire data-source or List.
It will definitely hamper performance.

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/12/2012 [Member] Silver | Points: 25

Up
0
Down

Hope this link will help you.
http://www.dotnetfunda.com/forums/thread7191-have-a-problem-gridview-total.aspx

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/12/2012 [Member] Silver | Points: 25

Up
0
Down
You can refer to this link as well.
http://www.dotnetfunda.com/articles/article379-how-to-display-total-at-the-bottom-of-the-gridview.aspx

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/12/2012 [Member] Silver | Points: 25

Up
0
Down
Refer to this link as well.
http://www.csharpaspnetarticles.com/2009/07/display-total-in-gridview-footer.html

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Denny on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
Try this code


doublr grdTotal=0.0;
protected void grdCart_RowDataBound(object sender, GridViewRowEventArgs e) //Event RowDataBound fired in Grid.
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
double rowTotal = Convert.ToDouble(DataBinder.Eval(e.Row.DataItem, "TotalPrice"));
grdTotal = grdTotal + rowTotal;
}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lbl = (Label)e.Row.FindControl("lblCalculatedTotal");
lbl.Text = grdTotal.ToString();
}
}

Regards,
Denny Kurian

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/13/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi plz check this attachment.
 Download source file

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijetha on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
The requirement slightly changed now.
I do have a update button at the end of the page.on click of the button the total amt(Price*qty) for all items has to be calculated and grand total too.The grandtotal has to be displayed at the footer.
in my updatebnt click the grandtotal is calculated fine.
But not getting a way how to assign the grandtotal to footer label.
here is the .cs code::
protected void btncalculate_Click(object sender, EventArgs e)
{

foreach (GridViewRow dr in grd5.Rows )
{
double price = Convert.ToDouble(((Label)dr.FindControl("lblRPrice")).Text);
int Qty=int.Parse(((TextBox)dr.FindControl("txtQty")).Text);

double total=price*Qty;
((Label)dr.FindControl("lbltotal")).Text = Convert.ToString(total);
grandtotal=grandtotal+total;

}

}
.aspx
<asp:GridView ID="grd5" runat="server" AutoGenerateColumns="false"
Width="97%" Height="51px"
onrowdeleting="grd5_RowDeleting"
ShowFooter="True" AllowPaging="True" >
<Columns>

<asp:TemplateField HeaderText="SKU">
<ItemTemplate>
<asp:Label ID="lblRsku" runat="server" Text='<%# Eval("SKU")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="ItemName">
<ItemTemplate>
<asp:Label ID="lblRItemName" runat="server" Text='<%# Eval("ItemName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="UPC">
<ItemTemplate>
<asp:Label ID="lblRupc" runat="server" Text='<%# Eval("UPC") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<asp:Label ID="lblRprice" runat="server" Text='<%# Eval("Price" )%>'> </asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="QTY">
<ItemTemplate>
<asp:TextBox ID="txtQty" runat="server" visible="true" ReadOnly="false" Text="1" ></asp:TextBox>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Enter a valid qty"
ValidationExpression="^[1-9]+$" ControlToValidate="txtQty" />

</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TOTAL">
<ItemTemplate>
<asp:Label ID="lbltotal" runat="server" Text='<%# Eval("Price" )%>' visible="true" ></asp:Label>

</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblgrndtotal" runat="server"></asp:Label>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Remove">
<ItemTemplate>

<asp:Button ID="btnRemove" runat="server" Text="DELETE" CommandName ="Delete" CommandArgument ="<%# Container.DataItemIndex %>" />
</ItemTemplate>
</asp:TemplateField>


</Columns>
</asp:GridView>
<div>
<table align="center">
<tr>
<td><asp:Button ID="btnOrder" runat="server" Text="PROCESS MY ORDER"

onclick="btnOrder_Click"/></td>
<td><asp:Button ID="btncalculate" runat="server" Text="Update"
onclick="btncalculate_Click" /></td>
</tr></table>

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/13/2012 [Member] Silver | Points: 25

Up
0
Down
Hi Vijeta ,
You can use this:
grd5.FooterRow.FindControl("YourControl").Text=value;

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/13/2012 [Member] Silver | Points: 25

Up
0
Down
Try this also:
Label lblGrandTot = grd5.FooterRow.FindControl("lblgrndtotal") as Label;
lblGrandTot .Text="your calculated value";

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijetha on: 3/13/2012 [Member] Starter | Points: 25

Up
0
Down
Yes its working fine now :)Thank you...

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sabarimahesh on: 3/15/2012 [Member] Bronze | Points: 25

Up
0
Down




The Page_Load event of the page binds data with the GridView control as follows:

protected void Page_Load(object sender, EventArgs e) 

{
if (!Page.IsPostBack)
{
BindData();
}
}

private void BindData()
{
string constr = "Server=TestServer;Database=TestDatabase;uid=test;pwd=test;";
string query = "SELECT ProductID, ProductName, UnitPrice, UnitsInStock, OnSale FROM Products";

SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();

da.Fill(table);

GridView1.DataSource = table;
GridView1.DataBind();
}


GridView1_RowDataBound

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lblPrice = (Label)e.Row.FindControl("lblPrice");
Label lblUnitsInStock = (Label)e.Row.FindControl("lblUnitsInStock");

decimal price = Decimal.Parse(lblPrice.Text);
decimal stock = Decimal.Parse(lblUnitsInStock.Text);

totalPrice += price;
totalStock += stock;

totalItems += 1;
}

if (e.Row.RowType == DataControlRowType.Footer)
{
Label lblTotalPrice = (Label)e.Row.FindControl("lblTotalPrice");
Label lblTotalUnitsInStock = (Label)e.Row.FindControl("lblTotalUnitsInStock");

lblTotalPrice.Text = totalPrice.ToString();
lblTotalUnitsInStock.Text = totalStock.ToString();

lblAveragePrice.Text = (totalPrice / totalItems).ToString("F");
}
}




Life is a Race
Thanks & Regards
By
Sabari Mahesh P M

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijetha on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
i do have set of items,and if i have selected it already i should not be able to select it again.How do i check whether my list consists the item??

protected void grd4_RowCommand(object sender, GridViewCommandEventArgs e)
{
int i = Convert.ToInt32(e.CommandArgument);
int index = i % grd4.PageSize;


List<ItemadditionItems> objItemAddtionList;

objItemAddtionList = (List<ItemadditionItems>)Session["ObjectItemData"];
if (e.CommandName == "select")
{


if (objItemAddtionList != null)(ThIS IS THE LIST WHICH CAPTURES ALL MY DATA)
{


ItemadditionItems objItemData = new ItemadditionItems();
objItemData.SKU = ((Label)grd4.Rows[index].Cells[0].FindControl("lblRsku")).Text.Trim();

objItemData.ItemName = ((Label)grd4.Rows[index].Cells[1].FindControl("lblRItemName")).Text.Trim();
objItemData.UPC = ((Label)grd4.Rows[index].Cells[2].FindControl("lblRupc")).Text.Trim();
objItemData.Price = ((Label)grd4.Rows[index].Cells[3].FindControl("lblRPrice")).Text.Trim();
objItemAddtionList.Add(objItemData);

Session["ObjectItemData"] = objItemAddtionList;
BindGrid5();

}
else
{
objItemAddtionList = new List<ItemadditionItems>();
ItemadditionItems objItemData = new ItemadditionItems();
objItemData.SKU = ((Label)grd4.Rows[index].Cells[0].FindControl("lblRsku")).Text.Trim();
objItemData.ItemName = ((Label)grd4.Rows[index].Cells[1].FindControl("lblRItemName")).Text.Trim();
objItemData.UPC = ((Label)grd4.Rows[index].Cells[2].FindControl("lblRupc")).Text.Trim();
objItemData.Price = ((Label)grd4.Rows[index].Cells[3].FindControl("lblRPrice")).Text.Trim();
objItemAddtionList.Add(objItemData);

Session["ObjectItemData"] = objItemAddtionList;
BindGrid5();
}

div7.Visible = false;
}

}

REGARDS,
VIJETHA.M.M

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Hi vijetha,

1st of all dont add new question in resolved post, for that create new post.
and for this problem, write Gridview_RowDataBound event
in that event check wether you binding row is there in your selected object list if so then just disabled "select" button for that row.



Mark this as answer if it helps you...................

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksamantaray on: 3/16/2012 [Member] Silver | Points: 25

Up
0
Down

Hi Vijeta,
You can have another column for status(0,1), then in Rowdatabound check if it 0 or 1.
For 0 keep 'Select" button enabled , and for 1 disable it.

Thanks,
Sanjay

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijetha on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi Sanjay,
Want to make it simple just by using list.contains() method.But it is not happening though.

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hmanjarawala on: 3/16/2012 [Member] Bronze | Points: 25

Up
0
Down
Ok,

Do one thing before binding data to GridView, just filterout these data from your DataSource and then assign it to GridView.



Mark this as answer if it helps you...................

Himanshu Manjarawala
Sr. Software Engineer@AutomationAnywhere
http://fieredotnet.wordpress.com/

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vijetha on: 3/16/2012 [Member] Starter | Points: 25

Up
0
Down
Hi,
Im not clear with your answer.Can u modify the code that i have posted above?Please

Vijetha, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response