troubleshooting Question

Gridview date column not sorting chronologically

Avatar of Shade22
Shade22Flag for United States of America asked on
2 Comments1 Solution104 ViewsLast Modified:
I have a sql datasource that is populating the gridview through c#.  However on the front end I used a datasourceformatstring of "
"{0:MMMM, yyyy}" to give me "March, 2016" ect.  However when it sorts it sorts alphabetically rather than by chronological order.

January, 2016
January, 2015
January, 2014

This is what I want:
January, 2016
December, 2015
November, 2014

.ASPX Side
 <asp:GridView ID="MyDataGrid" runat="server" AutoGenerateColumns="false" CellPadding="0" GridLines="None" HorizontalAlign="Left" Width="95%" AllowSorting="True">

                                                    <asp:BoundField DataField="workmonth" HeaderText="Work Month" ReadOnly="True" SortExpression="workmonth" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" DataFormatString="{0:MMMM, yyyy}" />
                                                    <asp:BoundField DataField="CompanyName" HeaderText="Employer" SortExpression="CompanyName" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
                                                    <asp:BoundField DataField="paiddate" HeaderText="Paid Date" ReadOnly="True" SortExpression="paiddate" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" DataFormatString="{0:MMMM, yyyy}" />
                                                    <asp:BoundField DataField="wrkhrs" HeaderText="Hours/Premiums Paid" SortExpression="wrkhrs" HeaderStyle-HorizontalAlign="center" ItemStyle-HorizontalAlign="center" />
                                                    <asp:BoundField DataField="Amount" HeaderText="Amount" ReadOnly="True" SortExpression="Amount" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left" DataFormatString="{0:c}" />
                                                <HeaderStyle BackColor="#1BA5DD" />

Open in new window

    protected void MyDataGrid(string membershipNum)
            MyDataSource.SelectParameters["membershipNum"].DefaultValue = membershipNum;
            MyDataGrid.DataSource = MyDataSource;

            if (MyDataGrid.Rows.Count > 0)
                MyDataGrid.HeaderRow.TableSection = TableRowSection.TableHeader;
                MyDataGrid.Visible = true;

                MyDataGrid.Visible = false;
                lb_tabContrib.Text = "data not found.";
                ddl_Export_Ctrb.Visible = false;
                btn_Export_Ctrb.Visible = false;

        catch (Exception ex)
            Error = ex.Message;
            dataMeth = "MyDataGrid";
            ContactAdministrator(Error, dataMeth, ex);

Open in new window

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros