0

I have a gridview with AllowSorting="True" but I would like to have a custom sort for just one column, so it can be sorted by date.

Currently that is sorted as a string so the sorting is incorrect.

The data is coming from SQL DB. Is there a way to maybe do a SQL sort for just the date column?

4

3 回答 3

1

you can mention Sort Expression property for that data column which you want to sort

eg:SortExpression="Date"

where Date is the name of the Data column which you want to sort

as you date is in string format you should convert it to DateTime then you would be able to sort the Date. If you are using a DataTable to bind the grid view first convert it to DataTime

dt.Columns.Add("DateTime", System.Type.GetType("System.DateTime"));
于 2013-06-07T10:24:47.230 回答
1

Hey for custom Sorting you need to Define Sort Expression on aspx page follwed by need to defind property on .cs page to hold current selecction like ASC/DESC and do Coding on OnSorting Event. I put it code one by one.

ASPX CODE .

  <asp:TemplateField HeaderText="DateTest" SortExpression="DateTest">

.CS Code.

Define the enum which holds the current selection

/// <summary>
/// Sort grid header.
/// </summary>
enum enmSortHeader
{
    Asc = 0,
    Desc = 1
}

Define Property to hold current selection.

 private enmSortHeader SortDateTest
    {
        get
        {
            if (ViewState["DateTest"] == null)
                return enmSortHeader.Asc;
            return
               (enmSortHeader)ViewState["DateTest"];
        }
        set
        {
            ViewState["DateTest"] = value;
        }
    }

On Sorting Event.

 protected void gvDetails_OnSorting(object sender, GridViewSortEventArgs e)
    {
     if (e.SortExpression == "DateTest")
        {
            if (SortDateTest== enmSortHeader.Asc)
            {
                var sorted = from m in SessionClass.BindDetailsGrid orderby  m.Date ascending select m;
                SessionClass.BindThoughtDetailsGrid = sorted.ToList();
                SortTheme = enmSortHeader.Desc;
            }
            else
            {
                var sorted = from m in SessionClass.BindThoughtDetailsGrid orderby m.Date descending select m;
                SessionClass.BindDetailsGrid = sorted.ToList();
                SortTheme = enmSortHeader.Asc;
            }
        }

    }

Hope it helps you.

于 2013-06-07T10:31:12.780 回答
1

Just want to correct the @Anuj answer, which works great for me. In the definition of the DataTable object, you need to specify the type of the object when you add column, like this:

DataTable dataTable = new DataTable("Log");
dataTable.Columns.Add("Start", typeof(DateTime));
dataTable.Columns.Add("End", typeof (DateTime));

And then, in the code behind of your ASP page, use this:

private string SortField
{
    get { return (string) ViewState["SortPropertyName"]; }
    set { ViewState["SortPropertyName"] = value; }
}

private string SortDirection
{
    get { return (string) ViewState["SortDirection"]; }
    set { ViewState["SortDirection"] = value; }
 }

And in your SortCommand method:

if (SortField.Equals(e_.SortExpression))
    SortDirection = SortDirection == "asc" ? "desc" : "asc";
else
{
    SortDirection = "asc";
    SortField = e_.SortExpression;
}
于 2013-10-02T09:48:12.160 回答