datagrid sorting

  • Thread starter Thread starter Mike P
  • Start date Start date
M

Mike P

I have just written my first page with a datagrid that allows ASC and
DESC sorting. But I want to know how I can get it so that the header
shows the type of sorting that has just been done on it (i.e. Column1
DESC). Also I would need to be able to remove this from the last column
that was sorted on so that it now says just Column2.

<%@ Page Language="C#" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

private void Page_Load()
{
if (!(Page.IsPostBack))
{
ViewState["LastSortOrder"]="DESC";
ViewState["LastSortColumn"]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Convert.ToString(Session["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
}
}

//cache can't be used as is Application level
//this code keeps data up to date on Session level (user may enter
different ODN etc)
void BindData(string strCallLog)
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

DataTable objDataTable = new DataTable();
objDataAdapter.Fill(objDataSet, "tblCallLog");

Session["LogData"] = objDataSet.Tables["tblCallLog"].DefaultView;
dgLog.DataSource = Session["LogData"];
dgLog.DataBind();
}

void dgLog_Paged(object sender, DataGridPageChangedEventArgs e)
{
dgLog.CurrentPageIndex = e.NewPageIndex;
BindData(Convert.ToString(Session["CallLog"]));
}

void dgLog_Sort(object sender, DataGridSortCommandEventArgs e)
{
string newSortColumn= e.SortExpression.ToString();
string newSortOrder="ASC"; // default
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
if (newSortColumn.Equals(lastSortColumn) &&
lastSortOrder.Equals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder="ASC";}
ViewState["LastSortOrder"]= newSortOrder;
ViewState["LastSortColumn"]= newSortColumn;
dgLog.CurrentPageIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpression + " " + newSortOrder;

BindData(Convert.ToString(Session["CallLog"]));
}


</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form ID="Log" runat="server">
<div style="vertical-align
top;height:400px;width:570px;overflow:scroll;">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#00015E"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-Font-Bold="true"
BorderColor="black"
BackColor="#0099CC"
forecolor="#FFFFFF"
AllowPaging="true"
PageSize="10"
OnPageIndexChanged="dgLog_Paged"
AutoGenerateColumns="False"
AllowSorting="true"
OnSortCommand="dgLog_Sort">

<Columns>
<asp:BoundColumn DataField="LogNum" HeaderText="LogNum"
SortExpression="LogNum" />
<asp:BoundColumn DataField="InceptTime"
HeaderText="InceptTime" SortExpression="InceptTime" />
<asp:BoundColumn DataField="InSpan" HeaderText="InSpan" />
<asp:BoundColumn DataField="InChan" HeaderText="InChan"
SortExpression="InChan" />
<asp:BoundColumn DataField="OutSpan" HeaderText="OutSpan"
SortExpression="OutSpan" />
<asp:BoundColumn DataField="OutChan" HeaderText="OutChan" />
</Columns>

<PagerStyle
Mode="NumericPages"
HorizontalAlign="Left"
ForeColor="#00015E"
BackColor="#dcdcdc"
NextPageText="Next"
PrevPageText="Previous" />

</asp:DataGrid>
</div>
</form>
</body>
</html>


Any help would be really appreciated.

Cheers,

Mike
 
Mike,

DataGrid1.Columns(0).HeaderText = "Column 1 Desc"

Where: Columns([zero based column index])

Sincerely,

--
S. Justin Gengo, MCP
Web Developer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche


Mike P said:
I have just written my first page with a datagrid that allows ASC and
DESC sorting. But I want to know how I can get it so that the header
shows the type of sorting that has just been done on it (i.e. Column1
DESC). Also I would need to be able to remove this from the last column
that was sorted on so that it now says just Column2.

<%@ Page Language="C#" ContentType="text/html"
ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script language="C#" runat="server">

private void Page_Load()
{
if (!(Page.IsPostBack))
{
ViewState["LastSortOrder"]="DESC";
ViewState["LastSortColumn"]= "LogNum";
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY Lognum DESC";
BindData(Convert.ToString(Session["CallLog"]));
}
else
{
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
}
}

//cache can't be used as is Application level
//this code keeps data up to date on Session level (user may enter
different ODN etc)
void BindData(string strCallLog)
{
SqlConnection objConnection = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectTransitTest"]
);
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strCallLog,
objConnection);
DataSet objDataSet = new DataSet();

DataTable objDataTable = new DataTable();
objDataAdapter.Fill(objDataSet, "tblCallLog");

Session["LogData"] = objDataSet.Tables["tblCallLog"].DefaultView;
dgLog.DataSource = Session["LogData"];
dgLog.DataBind();
}

void dgLog_Paged(object sender, DataGridPageChangedEventArgs e)
{
dgLog.CurrentPageIndex = e.NewPageIndex;
BindData(Convert.ToString(Session["CallLog"]));
}

void dgLog_Sort(object sender, DataGridSortCommandEventArgs e)
{
string newSortColumn= e.SortExpression.ToString();
string newSortOrder="ASC"; // default
string lastSortColumn= (string)ViewState["LastSortColumn"];
string lastSortOrder= (string)ViewState["LastSortOrder"];
if (newSortColumn.Equals(lastSortColumn) &&
lastSortOrder.Equals("ASC"))
{
newSortOrder= "DESC";
} // else {newSortOrder="ASC";}
ViewState["LastSortOrder"]= newSortOrder;
ViewState["LastSortColumn"]= newSortColumn;
dgLog.CurrentPageIndex= 0; // goto first page
Session["CallLog"] = "SELECT * FROM CALL_LOG WHERE LogNum BETWEEN
'63493' AND '63545' ORDER BY " + e.SortExpression + " " + newSortOrder;

BindData(Convert.ToString(Session["CallLog"]));
}


</script>

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form ID="Log" runat="server">
<div style="vertical-align
top;height:400px;width:570px;overflow:scroll;">
<asp:DataGrid id="dgLog" runat="server"
CellPadding="3"
Font-Name="arial"
Font-Size="8pt"
HeaderStyle-ForeColor="#00015E"
HeaderStyle-BackColor="#dcdcdc"
HeaderStyle-Font-Bold="true"
BorderColor="black"
BackColor="#0099CC"
forecolor="#FFFFFF"
AllowPaging="true"
PageSize="10"
OnPageIndexChanged="dgLog_Paged"
AutoGenerateColumns="False"
AllowSorting="true"
OnSortCommand="dgLog_Sort">

<Columns>
<asp:BoundColumn DataField="LogNum" HeaderText="LogNum"
SortExpression="LogNum" />
<asp:BoundColumn DataField="InceptTime"
HeaderText="InceptTime" SortExpression="InceptTime" />
<asp:BoundColumn DataField="InSpan" HeaderText="InSpan" />
<asp:BoundColumn DataField="InChan" HeaderText="InChan"
SortExpression="InChan" />
<asp:BoundColumn DataField="OutSpan" HeaderText="OutSpan"
SortExpression="OutSpan" />
<asp:BoundColumn DataField="OutChan" HeaderText="OutChan" />
</Columns>

<PagerStyle
Mode="NumericPages"
HorizontalAlign="Left"
ForeColor="#00015E"
BackColor="#dcdcdc"
NextPageText="Next"
PrevPageText="Previous" />

</asp:DataGrid>
</div>
</form>
</body>
</html>


Any help would be really appreciated.

Cheers,

Mike
 
Thanks for your help Justin. I decided not to do it a slightly
different way as it's what my users would prefer, but it's really useful
to know there is another way to solve this problem as well.

Cheers,

Mike
 
Back
Top