Sort gridview using "From" and "To" dates.http://forums.asp.net/t/1796126.aspx/1?Sort+gridview+using+From+and+To+dates+Thu, 26 Apr 2012 08:14:00 -040017961264947432http://forums.asp.net/p/1796126/4947432.aspx/1?Sort+gridview+using+From+and+To+dates+Sort gridview using "From" and "To" dates. <pre class="prettyprint">Guys, I am trying to form report using gridview by enabling user to select their options (filters). Which is 2 dropdownlist and 2 textboxes.</pre> <pre class="prettyprint">The 2 dropdownlist works fine and producing the expected result on the gridview. For the 2 textboxes, it takes in values from calendar.</pre> <pre class="prettyprint">Example: From: |X (date1)| To:|Y (date2)|</pre> <pre class="prettyprint">I am finding it hard to form or sort the gridview by using the given dates by user. I guess the problem is with the "FilterExpression".</pre> <pre class="prettyprint">Please take a look at the code below and any help is much appreciated.</pre> <pre class="prettyprint">&nbsp;</pre> <pre class="prettyprint">&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="IncomeReport.aspx.cs" Inherits="IncomeReport" EnableEventValidation="false"%&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head runat="server"&gt; &lt;title&gt;&lt;/title&gt; &lt;script type="text/javascript" language="javascript"&gt; function CallPrint() { var grid_ID = 'GridView1'; var grid_obj = document.getElementById(grid_ID); if (grid_obj != null) { var new_window = window.open(print.html); new_window.document.write(grid_obj.outerHTML); new_window.print(); new_window.close(); } } function winopen() { window.open("cal.aspx", "mywindow", "left=20, top=20, width=250, height=250, toolbar=0, resizable=0"); } &lt;/script&gt; &lt;script src="Scripts/CalendarControl.js" language="javascript" type="text/javascript"&gt;&lt;/script&gt; &lt;link href="Styles/CalendarControl.css" rel="Stylesheet" type="text/css" /&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;table border="1"&gt; &lt;tr&gt; &lt;td&gt; Department &lt;/td&gt; &lt;td&gt; Cost Centre &lt;/td&gt; &lt;td&gt; Month &lt;/td&gt; &lt;td&gt; Amount &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt; &lt;asp:DropDownList ID="ddlDept" DataSourceID="PopulateDept" AutoPostBack="true" DataValueField="dept_ID" DataTextField="dept_name" runat="server" AppendDataBoundItems="true"&gt; &lt;asp:ListItem Text="---Select A Department---" Value="default"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:SqlDataSource ID="PopulateDept" runat="server" ConnectionString="&lt;%&#36; ConnectionStrings:MyConn %&gt;" ProviderName="&lt;%&#36; ConnectionStrings:MyConn.ProviderName %&gt;" SelectCommand="SELECT DISTINCT [dept_ID], [dept_name] FROM [department]"&gt;&lt;/asp:SqlDataSource&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:DropDownList ID="ddlCC" AutoPostBack="true" DataValueField="cc_ID" DataTextField="costCentre_name" runat="server" AppendDataBoundItems="true"&gt; &lt;asp:ListItem Text="---Select A Cost Centre---" Value="default"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="All" Value="%"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;/td&gt; &lt;td&gt;From: &lt;input id="from" name="from" onfocus="showCalendarControl(this)" type="text" /&gt; &amp;nbsp; To: &lt;input id="to" name="to" onfocus="showCalendarControl(this)" type="text" /&gt; &amp;nbsp; &lt;/td&gt; &lt;td&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="4"&gt; &lt;br /&gt; &lt;asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" AllowPaging="true" ShowFooter="true" AllowSorting="true" DataSourceID="dsGridView" PageSize="10" OnRowDataBound="GridView1_RowDataBound"&gt; &lt;Columns&gt; &lt;asp:BoundField DataField="dept_name" HeaderText="Department Name" SortExpression="dept_ID" /&gt; &lt;asp:BoundField DataField="costCentre_name" HeaderText="Cost Centre" SortExpression="cc_ID" /&gt; &lt;asp:BoundField DataField="month" HeaderText="Month" FooterText="&lt;b&gt;Total&lt;/b&gt;" SortExpression="month" /&gt; &lt;asp:TemplateField HeaderText="Amount" SortExpression="amount"&gt; &lt;ItemTemplate&gt; &lt;asp:Label ID="lblTotal" runat="server" Text='&lt;%# Eval("amount").ToString() %&gt;'&gt; &lt;/asp:Label&gt; &lt;/ItemTemplate&gt; &lt;FooterTemplate&gt; &lt;asp:Label ID="lblTotal2" runat="server"&gt;&lt;/asp:Label&gt; &lt;/FooterTemplate&gt; &lt;/asp:TemplateField&gt; &lt;/Columns&gt; &lt;/asp:GridView&gt; &lt;asp:SqlDataSource ID="dsGridView" runat="server" ConnectionString="&lt;%&#36; ConnectionStrings:MyConn %&gt;" ProviderName="&lt;%&#36; ConnectionStrings:MyConn.ProviderName %&gt;" SelectCommand="SELECT DISTINCT d.[dept_ID], d.[dept_name], c.[cc_ID], c.[costCentre_name], m.[month], m.[amount] FROM department d, costCentre c, monthlyIncome m WHERE d.dept_ID=c.dept_ID AND c.cc_ID=m.cc_ID" FilterExpression="Convert(dept_ID, 'System.String') like '{0}%' AND Convert(cc_ID, 'System.String') like '{1}%' AND ([month] Convert('from.Text', 'System.DateTime') AND Convert('to.Text', 'System.DateTime'))"&gt; &lt;FilterParameters&gt; &lt;asp:ControlParameter Name="dept_ID" ControlID="ddlDept" PropertyName="SelectedValue" /&gt; &lt;asp:ControlParameter Name="cc_ID" ControlID="ddlCC" PropertyName="SelectedValue" /&gt; &lt;asp:ControlParameter Name="month" ControlID="from" PropertyName="Text" /&gt; &lt;asp:ControlParameter Name="month" ControlID="to" PropertyName="Text" /&gt; &lt;/FilterParameters&gt; &lt;/asp:SqlDataSource&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;input type="button" value="Print" id="btnPrint" runat="server" onclick="javascript:CallPrint()" /&gt; OR Report Format:&lt;asp:DropDownList ID="ddlReportFormat" runat="server"&gt; &lt;asp:ListItem Text="Please Select A Format" Value="default"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="Excel" Value="1"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="Word" Value="2"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="PDF" Value="3"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:Button ID="repSel" runat="server" Text="Generate Report" OnClick="GenerateReport"/&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p>Thanks in advance!</p> 2012-04-24T01:40:11-04:004950673http://forums.asp.net/p/1796126/4950673.aspx/1?Re+Sort+gridview+using+From+and+To+dates+Re: Sort gridview using "From" and "To" dates. <p>What happens when you click on the header of a column to sort by?</p> 2012-04-25T11:47:16-04:004952309http://forums.asp.net/p/1796126/4952309.aspx/1?Re+Sort+gridview+using+From+and+To+dates+Re: Sort gridview using "From" and "To" dates. <p>Hi,</p> <p>Try the Calender control in server side. Or you can use Calender extender in Ajax.</p> <p>Thanks,</p> 2012-04-26T08:14:00-04:00