Google Brother Up

2008/05/07

An Example of Column Sorting

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace CartTest
{
public partial class RevenueReport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblGrandTotal.Visible = false;
}
}
public void populateGridView()
{
FreshCart fc = new FreshCart();
fc.RevenueReportFromDate = Convert.ToDateTime(txtFromDate.Text.Trim());
fc.RevenueReportToDate = Convert.ToDateTime(txtToDate.Text.Trim());
DataSet ds = new DataSet();
ds = fc.populategvRevenueReport();
ViewState["SortExpression"] = "OrderDate";
ViewState["SortDirection"] = "DESC";
// check whether the date is present or not
string returnValue;
returnValue = ds.Tables[0].Rows[0][("ReturnValue")].ToString().Trim();
if (returnValue == "0")
{
//no matching date
gvRevenueReport.Visible = false;
lblGrandTotal.Visible = false;
lblQtyTotal.Visible = false;
lblRevenueTotal.Visible = false;
lblMessage.Visible = true;
lblMessage.Text = "No Matching Date";
}
else if (returnValue == "1")
{
lblMessage.Visible = false;
gvRevenueReport.Visible = true;
gvRevenueReport.DataSource = ds.Tables[1].DefaultView;
ViewState["DataSource"] = ds.Tables[1];
lblGrandTotal.Visible = true;
lblQtyTotal.Visible = true;
lblQtyTotal.Text = ds.Tables[2].Rows[0][("Qty_Total")].ToString().Trim();
lblRevenueTotal.Visible = true;
lblRevenueTotal.Text = ds.Tables[3].Rows[0][("Revenue_Total")].ToString().Trim();
gvRevenueReport.DataBind();
PopulateDetails(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString());
}
}
protected void btnGo_Click(object sender, EventArgs e)
{
populateGridView();
}
public void PopulateDetails(string strSortExp, string strSortDir)
{
DataView dv = ((DataTable)ViewState["DataSource"]).DefaultView;
DataTable NewsTable = dv.ToTable();
DataView NewsView = NewsTable.DefaultView;
if (NewsView != null)
{
NewsView.Sort = strSortExp + " " + strSortDir;
gvRevenueReport.DataSource = NewsView;
gvRevenueReport.DataBind();
}
}
protected void gvRevenueReport_Sorting(object sender, GridViewSortEventArgs e)
{
string SortExpression = e.SortExpression;
ViewState["SortExpression"] = SortExpression;
if (ViewState["SortDirection"].ToString() == "DESC")
{
ViewState["SortDirection"] = "ASC";
}
else
{
ViewState["SortDirection"] = "DESC";
}
PopulateDetails(ViewState["SortExpression"].ToString(), ViewState["SortDirection"].ToString());
}
}
}

No comments:

Drop Down List

1. http://www.janetsystems.co.uk/Articles/NetArticles/tabid/74/itemid/161/modid/449/Default.aspx