Shade22
asked on
Gridview date column not sorting chronologically
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
c#
"{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);
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I have recommended this question be closed as follows:
Accept: zephyr_hex (Megan) (https:#a42062436)
If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.
frankhelk
Experts-Exchange Cleanup Volunteer