good examples are found here:
http://msdn.microsoft.com/en-us/library/aa479347.aspx[
^]
I use in the aspx:
<asp:GridView runat="server" ID="grdServiceMessage" AutoGenerateColumns="false" AllowSorting="true"
PagerSettings-Mode="NumericFirstLast" PagerSettings-Visible="true" PagerSettings-Position="TopAndBottom"
AllowPaging="true" PageSize="20"
OnPageIndexChanging="grdServiceMessage_OPIC"
OnSorting="grdServiceMessage_OS"
Caption="Messages"
EmptyDataText="No Valid information found in searching"
>
......
In code behind:
protected void grdServiceMessage_OS(Object sender, GridViewSortEventArgs e)
{
Session["Column"] = e.SortExpression;
if (rblSortOrder.SelectedValue.StartsWith(" ASC"))
e.SortDirection = SortDirection.Ascending;
else
e.SortDirection = SortDirection.Descending;
FillGridView(Session["Column"].ToString(),txtFilter.Text, grdServiceMessage.PageIndex);
}
the FillGridview method:
private void FillGridView(String ColumnNameToSortOn, String Filter, Int32 PageNr)
{
List<sqlparameter> parms = new List<sqlparameter>();
try
{
if (txtStartDate.Text.Trim().Equals(String.Empty))
txtStartDate.Text = DateTime.Now.AddDays(-DateTime.Now.Day).ToShortDateString();
if (txtEndDate.Text.Trim().Equals(String.Empty))
txtEndDate.Text = DateTime.Now.ToShortDateString();
parms.Add(new SqlParameter("@ServiceID", ddlService.SelectedValue));
parms.Add(new SqlParameter("@StartDate", Convert.ToDateTime(txtStartDate.Text + " 0:00:00")));
parms.Add(new SqlParameter("@EndDate", Convert.ToDateTime(txtEndDate.Text + " 23:59:59")));
DataView view = new DataView();
String ConCatFilter = String.Empty;
if (!Filter.Equals(String.Empty))
{
if (!ColumnNameToSortOn.Equals("lastModified", StringComparison.OrdinalIgnoreCase))
{
ConCatFilter = ColumnNameToSortOn + " Like '%" + Filter + "%'";
lblError.Text = String.Empty;
}
else
lblError.Text = "Filtering on field 'Occurred' is not possible";
}
SyConOSDBClass.WebControls.UpdateDataView(view, "SE_SelMessageData", parms, ColumnNameToSortOn + rblSortOrder.SelectedValue, ConCatFilter);
if (view.Count.Equals(0))
grdServiceMessage.EmptyDataText = "Search for column '" + ColumnNameToSortOn + "' with Filter '" + ConCatFilter + "' Lead to no results.";
grdServiceMessage.DataSource = view;
grdServiceMessage.PageIndex = PageNr;
grdServiceMessage.DataBind();
Sesame.StyleSortableGridView(grdServiceMessage);
Session["Column"] = ColumnNameToSortOn;
}
catch (Exception err)
{
lblError.Text = err.Message;
...
}
finally
{
....
}
}
</sqlparameter></sqlparameter>