 |
| ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 21st, 2007, 01:42 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
The Totals Field doesn't change with Paging.
Hi,
I want to display Totals in a DataGrid. But the total doesn't change when AllowPaging is set to true. The total displayed for all the pages is the one that is displayed on the first page.
The Code Behind is:
* * * * * *
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Practice
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private double runningTotal = 0;
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection ();
con.ConnectionString="Server=localhost;uid=sa;pwd= sa;Database=Northwind;";
con.Open();
SqlDataAdapter dap=new SqlDataAdapter("select ProductName, UnitPrice from Products",con);
DataSet dst= new DataSet();
dap.Fill(dst);
DataGrid1.DataSource=dst.Tables[0];
DataGrid1.DataBind();
con.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CalcTotal(string _price)
{
try
{
runningTotal += Double.Parse(_price);
}
catch
{
//A value was null
}
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CalcTotal( e.Item.Cells[2].Text );
e.Item.Cells[2].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[2].Text));
}
else if(e.Item.ItemType == ListItemType.Footer )
{
e.Item.Cells[0].Text="Total";
e.Item.Cells[1].Text = string.Format("{0:c}", runningTotal);
}
}
//*Select a Row in DataGrid
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if( e.CommandName.ToUpper() == "SELECT")
{ // "Command edit" column clicked.
// (Column with SELECT command selected.)
// Fetch customer number out of zeroth bound column.
Label1.Text =e.Item.Cells[1].Text;
// Redirect to update page.
//Response.Redirect( "WebForm2.aspx" );
}
}
}
}
* * * * * *
The HTML code is"
* * * * * *
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="Practice.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 23px; POSITION: absolute; TOP: 44px" runat="server"
Width="275px" Height="249px" BorderColor="#DEBA84" BorderStyle="None" CellSpacing="2" BorderWidth="1px"
BackColor="#DEBA84" CellPadding="3" AllowPaging="True" ShowFooter="True">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#738A9C"></SelectedItemStyle>
<ItemStyle ForeColor="#8C4510" BackColor="#FFF7E7"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#A55129"></HeaderStyle>
<FooterStyle ForeColor="#8C4510" BackColor="#F7DFB5"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Click" HeaderText="Column" CommandName="SELECT"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#8C4510" Mode="NumericPages"></PagerStyle>
</asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 30px; POSITION: absolute; TOP: 12px" runat="server"
Width="266px" BackColor="#FFC080">Label</asp:Label>
</form>
</body>
</HTML>
* * * * * *
|
|

June 21st, 2007, 03:23 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Well
I couldn't get the idea behind the totals, please clarify the issues below:
1. Are you trying to display the total of each page in the footer of datagrid?
2. What is the purpose of click button on each row?
Moreover, if you want to set paging for a datagrid, you have to set pagesize in datagrid's property like
<asp:DataGrid AllowPaging="True" PageSize=10>
And then add the DataGrid1_PageIndexChanged event in the codebehind as below:
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//Rebind the datagrid
SqlConnection con=new SqlConnection ();
con.ConnectionString= ConfigurationSettings.AppSettings["ConnectionString"];
con.Open();
SqlDataAdapter dap=new SqlDataAdapter("select ProductName, UnitPrice from Products",con);
DataSet dst= new DataSet();
dap.Fill(dst);
DataGrid1.DataSource=dst.Tables[0];
DataGrid1.DataBind();
con.Close();
}
Regards
Mike
Don't expect too much, too soon.
|
|

June 21st, 2007, 04:07 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
I want to display the total of the unit price field. If AllowPaging is set to false, the total is correct.
I have already set Pagesize to 10.
Setting AllowPaging to True shows the total of the unit price field for each page and that was coming out same for all pages.
But I did not use the DataGrid1_PageIndexChanged event. After adding the piece of code specified by you in the DataGrid1_PageIndexChanged event, the total for each page varies but the computed value is incorrect.
Thanks
Sams
|
|

June 21st, 2007, 05:01 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Well, for that you have to do some manipulations, you have to calculate the total based on Datagrid's current page index. To understand the logic follow the steps below:
1. Declare a page level variable at the top of page
private int CurrentPageIndex = 0;
2. Add the following lines to the PageIndexChanged event before binding the grid:
CurrentPageIndex = DataGrid1.CurrentPageIndex;
runningTotal = 0;
3. Now in the ItemDataBound event, where you are calculating the total, replace the code as below:
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(DataGrid1.CurrentPageIndex == CurrentPageIndex)
CalcTotal( e.Item.Cells[2].Text);
e.Item.Cells[2].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[2].Text));
}
Do let us know if you face problems.
Regards
Mike
Don't expect too much, too soon.
|
|

June 22nd, 2007, 02:23 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
The total for each page is displayed correctly. The problem arises when the Button column containing the text 'CLICK' is clicked.
The Button column is used to select a row.
Suppose I click on Page 2, the page is shown and the total is also correct. But now if I select row(s) on the same page, the total for that page changes. This should not happen.
The code behind is shown below:
* * * *
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace Practice
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private double runningTotal = 0;
protected System.Web.UI.WebControls.Label Label2;
private int CurPgIndex = -1;
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection con=new SqlConnection ();
con.ConnectionString="Server=localhost;uid=sa;pwd= sa;Database=Northwind;";
con.Open();
SqlDataAdapter dap=new SqlDataAdapter("select ProductName, UnitPrice from Products",con);
DataSet dst= new DataSet();
dap.Fill(dst);
DataGrid1.DataSource=dst.Tables[0];
DataGrid1.DataBind();
con.Close();
}
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if(DataGrid1.CurrentPageIndex != CurPgIndex)
CalcTotal( e.Item.Cells[2].Text);
e.Item.Cells[2].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[2].Text));
}
else if(e.Item.ItemType == ListItemType.Footer )
{
e.Item.Cells[0].Text="Total";
e.Item.Cells[1].Text = string.Format("{0:c}", runningTotal);
}
// if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
// {
// CalcTotal( e.Item.Cells[2].Text );
// e.Item.Cells[2].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[2].Text));
// }
// else if(e.Item.ItemType == ListItemType.Footer )
// {
// e.Item.Cells[0].Text="Total";
// e.Item.Cells[1].Text = string.Format("{0:c}", runningTotal);
// }
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_ItemCommand);
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEvent Handler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler (this.DataGrid1_ItemDataBound);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void CalcTotal(string _price)
{
try
{
runningTotal += Double.Parse(_price);
}
catch
{
//A value was null
}
}
//*Select a Row in DataGrid
private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if( e.CommandName.ToUpper() == "SELECT")
{ // "Command edit" column clicked.
// (Column with SELECT command selected.)
// Fetch customer number out of zeroth bound column.
Label1.Text =e.Item.Cells[1].Text;
// Redirect to update page.
//Response.Redirect( "WebForm2.aspx" );
}
}
private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEvent Args e)
{
CurPgIndex = DataGrid1.CurrentPageIndex;
runningTotal = 0;
DataGrid1.CurrentPageIndex = e.NewPageIndex;
//Rebind the datagrid
SqlConnection con=new SqlConnection ();
con.ConnectionString="Server=localhost;uid=sa;pwd= sa;Database=Northwind;";
con.Open();
SqlDataAdapter dap=new SqlDataAdapter("select ProductName, UnitPrice from Products",con);
DataSet dst= new DataSet();
dap.Fill(dst);
DataGrid1.DataSource=dst.Tables[0];
DataGrid1.DataBind();
con.Close();
}
}
}
* * * *
The HTML code is same.
Thanks
Sams
|
|
 |