To filter the gridview's data you need to filter the data source the gridview is bound with for example if the gridview is bound with a dataset then apply filter on the dataset etc. then bind the gridview again with the data source
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
DataTable dt = (DataTable)Session["dt"];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
roxan
Member
116 Points
242 Posts
filter by date?
May 05, 2012 03:46 AM|LINK
hi how to filter gridview by date/month? thanks im using asp.net c#
shashankgwl
All-Star
18926 Points
3662 Posts
Re: filter by date?
May 05, 2012 06:56 AM|LINK
All is well if it runs well.
blog
chaaraan
Contributor
2170 Points
484 Posts
Re: filter by date?
May 05, 2012 06:57 AM|LINK
Hi,
Please check this sample code
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="sort._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:GridView ID="GridView1" runat="server" AllowSorting = "true" OnSorting = "grv_sorting" AutoGenerateColumns = "false" >
<Columns>
<asp:BoundField DataField = "id" HeaderText ="Name"/>
<asp:BoundField DataField = "Label" HeaderText = "Date" SortExpression = "Label" />
</Columns>
</asp:GridView>
</asp:Content>
in .cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace sort
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("label", typeof(DateTime));
dt.Rows.Add(9, "10/05/2012");
dt.Rows.Add(2, "06/05/2012");
dt.Rows.Add(6, "08/05/2012");
GridView1.DataSource = dt;
GridView1.DataBind();
Session["dt"] = (DataTable)dt;
}
}
protected void grv_sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, DESCENDING);
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, ASCENDING);
}
}
private void SortGridView(string sortExpression, string direction)
{
// You can cache the DataTable for improving performance
DataTable dt = (DataTable)Session["dt"];
DataView dv = new DataView(dt);
dv.Sort = sortExpression + direction;
GridView1.DataSource = dv;
GridView1.DataBind();
}
private const string ASCENDING = " ASC";
private const string DESCENDING = " DESC";
public SortDirection GridViewSortDirection
{
get
{
if (ViewState["sortDirection"] == null)
ViewState["sortDirection"] = SortDirection.Ascending;
return (SortDirection)ViewState["sortDirection"];
}
set { ViewState["sortDirection"] = value; }
}
}
}
Regards,
Charan
ramiramilu
All-Star
95493 Points
14106 Posts
Re: filter by date?
May 05, 2012 08:14 AM|LINK
If you are using any DataSources, then it should have FilterExpression property which you can set and filter your Gridview...
or else make a SqlConnnection and write a where clause based on what you want to filter and then bind GridView...
Thanks,
JumpStart
Mamba Dai - ...
All-Star
23531 Points
2683 Posts
Microsoft
Re: filter by date?
May 11, 2012 08:55 AM|LINK
Hi,
Check this sample code about FilterExpression:
<asp:DropDownList ID="ddlCity" runat="server" AppendDataBoundItems="true" AutoPostBack="true" DataSourceID="sqlDataSourceCity" DataTextField="City" DataValueField="City" Width="100px"> <asp:ListItem Value="%">All</asp:ListItem> </asp:DropDownList> <asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="true" DataSourceID="sqlDataSourceCountry" DataTextField="Country" DataValueField="Country" Width="100px" AutoPostBack="True"> <asp:ListItem Value="%">All</asp:ListItem> </asp:DropDownList> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="Horizontal" Width="650px" ShowHeader="False"> <Columns> <asp:BoundField DataField="CustomerID" HeaderText="CustomerID" /> <asp:BoundField DataField="CompanyName" HeaderText="CompanyName" /> <asp:BoundField DataField="ContactName" HeaderText="ContactName" /> <asp:BoundField DataField="City" HeaderText="City" /> <asp:BoundField DataField="Country" HeaderText="Country" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="sqlDataSourceGridView" runat="server" ConnectionString="<%$ ConnectionStrings:northWindConnectionString %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country] FROM [Customers]" FilterExpression="[City] like '{0}%' and [Country] like '{1}%'"> <FilterParameters> <asp:ControlParameter ControlID="ddlCity" Name="City" PropertyName="SelectedValue" Type="String" /> <asp:ControlParameter ControlID="ddlCountry" Name="Country" PropertyName="SelectedValue" Type="String" /> </FilterParameters> </asp:SqlDataSource>To retrieve more staff please go through here:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.filterexpression.aspx
Alternative, you can format the select statement dynamically based on the value user selects like below:
http://forums.asp.net/p/1239244/2258847.aspx
Feedback to us
Develop and promote your apps in Windows Store
sriramabi
Contributor
4351 Points
1277 Posts
Re: filter by date?
May 11, 2012 09:10 AM|LINK
Hai
use ORDER BY in u r select query..
for
Date:
Select * from Table ORDER BY Date DESC
Month:
Select * from Table ORDER BY Month DESC
thank u