Hi,
I am using a Telerik Rad grid in my application from where I need to filter data from an expression which creates with selected multiple columns. In my grid there is a DateTimeColumn and other several columns to filter data. When I select each column separately filter is working but for selected multiple columns the filter expression is not working well. So I need to filter data from multiple columns with the filter expression.
Here is the code listing i have done with the grid.
protected void grdUserType_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
string Datakey = item.GetDataKeyValue("RegBy") == null ? "-1" : item.GetDataKeyValue("RegBy").ToString();
DataRowView DataItem = (DataRowView)e.Item.DataItem;
item["ActionType"].Text = string.Format("{0} ({1})", DataItem["ActionType"], DataItem["Description"]);
HyperLink hy = new HyperLink();
hy.Text = item.GetDataKeyValue("FullName").ToString();
hy.Attributes.Add("onclick", "javascript:calluserpopup('" + Datakey + "');return false;");
hy.NavigateUrl = "#";
hy.CssClass = "pagelinks";
item["RegBy"].Controls.Add(hy);
Label lblDt = (Label)item["RegDateOnly"].FindControl("lblDt");
lblDt.Text = string.Format("{0}", Convert.ToDateTime(DataItem["RegDate"]));
}
if (e.Item is GridFilteringItem)
{
Control ctl = (e.Item as GridFilteringItem)["RegDateOnly"].Controls[1];
RadDatePicker tbPattern = ctl as RadDatePicker;
if (Session["OldPattern"] != null )
tbPattern.SelectedDate = Convert.ToDateTime(Session["OldPattern"]);
}
}
protected void grdUserType_ItemCommand(object source, GridCommandEventArgs e)
{
switch (e.CommandName)
{
case RadGrid.FilterCommandName:
System.Web.UI.Pair pair = (System.Web.UI.Pair)e.CommandArgument;
if (pair.First.ToString() == "EqualTo" | pair.First.ToString() == "GreaterThan" | pair.First.ToString() == "LessThan" )
{
if (pair.Second.ToString() == "RegDateOnly" )
{
DatePatternfunction = pair.First.ToString();
Control ctl = (e.Item as GridFilteringItem)["RegDateOnly"].Controls[1];
RadDatePicker tbPattern = ctl as RadDatePicker;
DateTime dt2 = tbPattern.SelectedDate.Value;
// Convert the date to date type of the database
string dt = dt2.ToString("d", new System.Globalization.CultureInfo("en-Gb"));
GridFilteringItem filterItem = (GridFilteringItem)e.Item;
string filterPattern = string.Empty;
filterPattern = dt;
Session["filterPattern"] = filterPattern;
Session["OldPattern"] = string.Format("{0:d}", dt2.Date);
}
}
if (pair.First.ToString() == "NoFilter")
{
Session["filterPattern"] = null ;
Session["OldPattern"] = null;
}
break ;
}
}
protected void grdUserType_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
if (Session["filterPattern"] != null)
{
string Expression = grdUserType.MasterTableView.FilterExpression;
// To convert the date format which accepts the data base
Expression = Expression.Replace(Session["OldPattern"].ToString(), Session["filterPattern"].ToString());
grdUserType.MasterTableView.FilterExpression = Expression;
DatePatternfunction = string.Empty;
Session["filterPattern"] = null;
}
// To get the data source to the grid
grdUserType.DataSource = this.GetDataTable(1);
}
I am using a Telerik Rad grid in my application from where I need to filter data from an expression which creates with selected multiple columns. In my grid there is a DateTimeColumn and other several columns to filter data. When I select each column separately filter is working but for selected multiple columns the filter expression is not working well. So I need to filter data from multiple columns with the filter expression.
Here is the code listing i have done with the grid.
protected void grdUserType_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item is GridDataItem)
{
GridDataItem item = (GridDataItem)e.Item;
string Datakey = item.GetDataKeyValue("RegBy") == null ? "-1" : item.GetDataKeyValue("RegBy").ToString();
DataRowView DataItem = (DataRowView)e.Item.DataItem;
item["ActionType"].Text = string.Format("{0} ({1})", DataItem["ActionType"], DataItem["Description"]);
HyperLink hy = new HyperLink();
hy.Text = item.GetDataKeyValue("FullName").ToString();
hy.Attributes.Add("onclick", "javascript:calluserpopup('" + Datakey + "');return false;");
hy.NavigateUrl = "#";
hy.CssClass = "pagelinks";
item["RegBy"].Controls.Add(hy);
Label lblDt = (Label)item["RegDateOnly"].FindControl("lblDt");
lblDt.Text = string.Format("{0}", Convert.ToDateTime(DataItem["RegDate"]));
}
if (e.Item is GridFilteringItem)
{
Control ctl = (e.Item as GridFilteringItem)["RegDateOnly"].Controls[1];
RadDatePicker tbPattern = ctl as RadDatePicker;
if (Session["OldPattern"] != null )
tbPattern.SelectedDate = Convert.ToDateTime(Session["OldPattern"]);
}
}
protected void grdUserType_ItemCommand(object source, GridCommandEventArgs e)
{
switch (e.CommandName)
{
case RadGrid.FilterCommandName:
System.Web.UI.Pair pair = (System.Web.UI.Pair)e.CommandArgument;
if (pair.First.ToString() == "EqualTo" | pair.First.ToString() == "GreaterThan" | pair.First.ToString() == "LessThan" )
{
if (pair.Second.ToString() == "RegDateOnly" )
{
DatePatternfunction = pair.First.ToString();
Control ctl = (e.Item as GridFilteringItem)["RegDateOnly"].Controls[1];
RadDatePicker tbPattern = ctl as RadDatePicker;
DateTime dt2 = tbPattern.SelectedDate.Value;
// Convert the date to date type of the database
string dt = dt2.ToString("d", new System.Globalization.CultureInfo("en-Gb"));
GridFilteringItem filterItem = (GridFilteringItem)e.Item;
string filterPattern = string.Empty;
filterPattern = dt;
Session["filterPattern"] = filterPattern;
Session["OldPattern"] = string.Format("{0:d}", dt2.Date);
}
}
if (pair.First.ToString() == "NoFilter")
{
Session["filterPattern"] = null ;
Session["OldPattern"] = null;
}
break ;
}
}
protected void grdUserType_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
{
if (Session["filterPattern"] != null)
{
string Expression = grdUserType.MasterTableView.FilterExpression;
// To convert the date format which accepts the data base
Expression = Expression.Replace(Session["OldPattern"].ToString(), Session["filterPattern"].ToString());
grdUserType.MasterTableView.FilterExpression = Expression;
DatePatternfunction = string.Empty;
Session["filterPattern"] = null;
}
// To get the data source to the grid
grdUserType.DataSource = this.GetDataTable(1);
}