troubleshooting Question

Gridview date column not sorting chronologically

Avatar of Shade22
Shade22Flag for United States of America asked on
C#ASP.NETSQL
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.

Example:
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">

                                                <Columns>
                                                    <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}" />
                                                </Columns>
                                                <HeaderStyle BackColor="#1BA5DD" />
                                            </asp:GridView>

c#
    protected void MyDataGrid(string membershipNum)
    {
        try
        {
            MyDataSource.SelectParameters["membershipNum"].DefaultValue = membershipNum;
            MyDataSource.DataBind();
            MyDataGrid.DataSource = MyDataSource;
            MyDataGrid.DataBind();
           

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

            }
            else
            {
                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);
        }
    }
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
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