I have a RADGrid that generates columns automatically by assigning the DataSource to a LIST (OF <Object>) array. (I generate the grid entirely through server-side code). My client now wants to be able to filter off any column. There are some columns that have very specific data, and I would like to show then a "DropDownList" as opposed to a textbox. I created an ITemplate object, and within the "InstantiateIn()" method I created my RADCombobox (I also tried using a regular ASP DropdownList). I also implemented code in Delegate Methods to handle both the DataBind() and the SelectedIndexChanged() events for the control.
I then added code to the Grid's "ColumnCreated()" method that replaced the FilterTemplate with my custom ITemplate if the column name matched the one that I want to replace. So far, everthing worked perfectly...my control appeared in place of the usual filter control, and it was populated with the correct data.
I had an extremely irritating problem with an out-of-sync ViewState error whenever any control on the same page tried to perform a
PostBack...after hours of searching, I ended up turning "EnableViewState" off on the DropDownList control.
Now, when I select an item in my DropDownList filter control, I see the AjaxPanel's rotating circle skin appear as usual, however the control's "SelectedIndexChanged()" event never fires. Any thoughts?
I then added code to the Grid's "ColumnCreated()" method that replaced the FilterTemplate with my custom ITemplate if the column name matched the one that I want to replace. So far, everthing worked perfectly...my control appeared in place of the usual filter control, and it was populated with the correct data.
I had an extremely irritating problem with an out-of-sync ViewState error whenever any control on the same page tried to perform a
PostBack...after hours of searching, I ended up turning "EnableViewState" off on the DropDownList control.
Now, when I select an item in my DropDownList filter control, I see the AjaxPanel's rotating circle skin appear as usual, however the control's "SelectedIndexChanged()" event never fires. Any thoughts?
10 Answers, 1 is accepted
0
Accepted

Princy
Top achievements
Rank 2
answered on 21 Nov 2013, 12:09 PM
Hi Ben,
Here is a sample code snippet that shows creating a FilterTemplate for a column. Please try it, if this doesn't help, provide your full code snippet.
C#:
Thanks,
Princy
Here is a sample code snippet that shows creating a FilterTemplate for a column. Please try it, if this doesn't help, provide your full code snippet.
C#:
RadGrid RadGrid1;
private
void
Page_Init(
object
sender, System.EventArgs e)
{
RadGrid1 =
new
RadGrid();
RadGrid1.ID =
"RadGrid1"
;
RadGrid1.NeedDataSource +=
new
GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
string
ColumnName =
"Country"
;
GridBoundColumn boundColumn;
boundColumn =
new
GridBoundColumn();
boundColumn.FilterTemplate =
new
MyTemplate(ColumnName, RadGrid1);
RadGrid1.MasterTableView.Columns.Add(boundColumn);
boundColumn.DataField = ColumnName;
boundColumn.HeaderText = ColumnName;
boundColumn.UniqueName = ColumnName;
PlaceHolder1.Controls.Add(RadGrid1);
}
private
class
MyTemplate : ITemplate
{
protected
RadComboBox combo;
private
string
colname;
RadGrid radgrid;
public
MyTemplate(
string
cName,RadGrid grid)
{
colname = cName;
radgrid = grid;
}
public
void
InstantiateIn(System.Web.UI.Control container)
{
combo =
new
RadComboBox();
combo.ID =
"RadComboBoxControl"
;
combo.DataBinding +=
new
EventHandler(RadComboBoxControl_DataBinding);
container.Controls.Add(combo);
combo.AutoPostBack =
true
;
combo.SelectedIndexChanged +=
new
RadComboBoxSelectedIndexChangedEventHandler(RadComboBoxControl_SelectedIndexChanged);
}
public
void
RadComboBoxControl_DataBinding(
object
sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
GridFilteringItem container = (GridFilteringItem)combo.NamingContainer;
combo.DataTextField = colname;
combo.DataValueField = colname;
combo.DefaultItem.Text =
"All"
;
combo.DataSource = dt;
}
void
RadComboBoxControl_SelectedIndexChanged(
object
sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
RadComboBox combo = sender
as
RadComboBox;
string
filterExpression;
filterExpression =
"([Country] LIKE '"
+ e.Value +
"')"
;
radgrid.MasterTableView.FilterExpression = filterExpression;
radgrid.MasterTableView.Rebind();
}
}
Thanks,
Princy
0

RB
Top achievements
Rank 1
answered on 10 Dec 2013, 06:33 PM
What is dt in the current line:
combo.DataSource = dt;
0

Princy
Top achievements
Rank 2
answered on 11 Dec 2013, 04:29 AM
Hi,
I'm setting the DataSource of the RadComboBox using a DataTable.
C#:
Thanks,
Princy
I'm setting the DataSource of the RadComboBox using a DataTable.
C#:
dt = GetDataTable(
"SELECT distinct Country FROM Customers"
);
combo.DataSource = dt;
. . . .
public
static
DataTable GetDataTable(
string
query)
{
string
ConnString = ConfigurationManager.ConnectionStrings[
"Northwind_newConnectionString3"
].ConnectionString;
SqlConnection conn =
new
SqlConnection(ConnString);
SqlDataAdapter adapter =
new
SqlDataAdapter();
adapter.SelectCommand =
new
SqlCommand(query, conn);
DataTable myDataTable =
new
DataTable();
conn.Open();
try
{
adapter.Fill(myDataTable);
}
finally
{
conn.Close();
}
return
myDataTable;
}
Thanks,
Princy
0

RB
Top achievements
Rank 1
answered on 11 Dec 2013, 07:53 PM
I implemented your example. How can I make the dropdown display only unique values.Also the filtering fails. Whenever an item is selected, is there a method which should be implemented to handle it?
<
span
style
=
"color:blue;"
>void</
span
> RadComboBoxControl_SelectedIndexChanged(<
span
style
=
"color:blue;"
>object</
span
> sender, <
span
style
=
"color:#2b91af;"
>RadComboBoxSelectedIndexChangedEventArgs</
span
> e)
{
<
span
style
=
"color:#2b91af;"
>RadComboBox</
span
> combo = sender <
span
style
=
"color:blue;"
>as</
span
> <
span
style
=
"color:#2b91af;"
>RadComboBox</
span
>;
<
span
style
=
"color:blue;"
>string</
span
> filterExpression;
filterExpression = <
span
style
=
"color:#a31515;"
>"([Reason] LIKE '"</
span
> + e.Value + <
span
style
=
"color:#a31515;"
>"')"</
span
>;
radgrid.MasterTableView.FilterExpression = filterExpression;
radgrid.MasterTableView.Rebind();
}
0

Princy
Top achievements
Rank 2
answered on 12 Dec 2013, 04:31 AM
Hi,
Its hard to identify the issue with so little information. The code works fine at my end, can you please provide your full code snippet.
You can have distinct values in the RadComboBox by querying it to contain values that you need. Like in my sample code, I have used a DataTable, in that I'm adding distinct country names, which is set as the DataSource to the RadComboBox. Make sure you have set RadcomboBox AutoPostBack property to true so as the SelectedIndexChanged and filter the values accordingly. Can you use breakpoints and find out if its taking the FilterExpression correct.
Thanks,
Princy
Its hard to identify the issue with so little information. The code works fine at my end, can you please provide your full code snippet.
You can have distinct values in the RadComboBox by querying it to contain values that you need. Like in my sample code, I have used a DataTable, in that I'm adding distinct country names, which is set as the DataSource to the RadComboBox. Make sure you have set RadcomboBox AutoPostBack property to true so as the SelectedIndexChanged and filter the values accordingly. Can you use breakpoints and find out if its taking the FilterExpression correct.
Thanks,
Princy
0

RB
Top achievements
Rank 1
answered on 13 Feb 2014, 10:49 PM
I tried your example above. But the default value "All" is not being displayed!
combo.DefaultItem.Text = "All";
I also tried this after DataBind()
combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "-1" });
combo.SelectedIndex = 0;
But even this doesnt display "All" on the top of the dropdown.
My DataBinding code is as follows:
public void RadComboBoxControl_DataBinding(object sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
GridFilteringItem container = (GridFilteringItem)combo.NamingContainer;
combo.DataTextField = colname;
combo.DataValueField = colname;
combo.EnableAutomaticLoadOnDemand = true;
combo.AllowCustomText = true;
try
{
if (this.popId > 0)
combo.DataSource = new PopCapabilityRule_ListByPopId().ExecuteTypedDataTable(popId).SelectDistinct(colname);
else
combo.DataSource = new PopCapabilityRule_List().ExecuteTypedDataTable().SelectDistinct(colname);
}
catch (Exception exception)
{
GlobalCrossing.Ucommand.Library.ErrorManager.LogCriticalError(this.GetType().Name, System.Environment.MachineName, SessionMatrix.Current.IFOPrincipal.UserId, "", "80", exception.Message, "-3836364", exception);
}
combo.DataBind();
combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "-1" });
combo.SelectedIndex = 0;
}
combo.DefaultItem.Text = "All";
I also tried this after DataBind()
combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "-1" });
combo.SelectedIndex = 0;
But even this doesnt display "All" on the top of the dropdown.
My DataBinding code is as follows:
public void RadComboBoxControl_DataBinding(object sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
GridFilteringItem container = (GridFilteringItem)combo.NamingContainer;
combo.DataTextField = colname;
combo.DataValueField = colname;
combo.EnableAutomaticLoadOnDemand = true;
combo.AllowCustomText = true;
try
{
if (this.popId > 0)
combo.DataSource = new PopCapabilityRule_ListByPopId().ExecuteTypedDataTable(popId).SelectDistinct(colname);
else
combo.DataSource = new PopCapabilityRule_List().ExecuteTypedDataTable().SelectDistinct(colname);
}
catch (Exception exception)
{
GlobalCrossing.Ucommand.Library.ErrorManager.LogCriticalError(this.GetType().Name, System.Environment.MachineName, SessionMatrix.Current.IFOPrincipal.UserId, "", "80", exception.Message, "-3836364", exception);
}
combo.DataBind();
combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "-1" });
combo.SelectedIndex = 0;
}
0

RB
Top achievements
Rank 1
answered on 13 Feb 2014, 10:50 PM
public void RadComboBoxControl_DataBinding(object sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
GridFilteringItem container = (GridFilteringItem)combo.NamingContainer;
combo.DataTextField = colname;
combo.DataValueField = colname;
combo.EnableAutomaticLoadOnDemand = true;
combo.AllowCustomText = true;
try
{
if (this.popId > 0)
combo.DataSource = new PopCapabilityRule_ListByPopId().ExecuteTypedDataTable(popId).SelectDistinct(colname);
else
combo.DataSource = new PopCapabilityRule_List().ExecuteTypedDataTable().SelectDistinct(colname);
}
catch (Exception exception)
{
GlobalCrossing.Ucommand.Library.ErrorManager.LogCriticalError(this.GetType().Name, System.Environment.MachineName, SessionMatrix.Current.IFOPrincipal.UserId, "", "80", exception.Message, "-3836364", exception);
}
combo.DataBind();
combo.Items.Insert(0, new Telerik.Web.UI.RadComboBoxItem { Text = "All", Value = "-1" });
combo.SelectedIndex = 0;
}
0

Princy
Top achievements
Rank 2
answered on 14 Feb 2014, 05:02 AM
Hi,
Please try setting the AppendDataBoundItems property to true for the RadComboBox.
C#:
Thanks,
Princy
Please try setting the AppendDataBoundItems property to true for the RadComboBox.
C#:
RadComboBox combo = (RadComboBox)sender;
. . .
combo.AllowCustomText =
true
;
combo.AppendDataBoundItems =
true
;
. . .
Thanks,
Princy
0

RB
Top achievements
Rank 1
answered on 18 Feb 2014, 11:29 PM
Thanks. It worked. I am trying to implement "All" option. On click of "All" the event RadComboBoxControl_SelectedIndexChanged is not triggered. Which event will handle this?
0

Princy
Top achievements
Rank 2
answered on 19 Feb 2014, 03:56 AM
Hi,
Please try the below sample code snippet, I have modified the code and "All" option works fine now.
ASPX:
C#:
Thanks,
Princy
Please try the below sample code snippet, I have modified the code and "All" option works fine now.
ASPX:
<
asp:Panel
ID
=
"Panel1"
runat
=
"server"
></
asp:Panel
>
C#:
{
protected
void
Page_Init(
object
sender, EventArgs e)
{
RadGrid grid =
new
RadGrid();
grid.ID =
"RadGrid1"
;
grid.AllowFilteringByColumn =
true
;
grid.AutoGenerateColumns =
false
;
grid.NeedDataSource +=
new
GridNeedDataSourceEventHandler(grid_NeedDataSource);
GridBoundColumn boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"CustomerID"
;
boundColumn.HeaderText =
"CustomerID"
;
boundColumn.UniqueName =
"CustomerID"
;
boundColumn.AllowFiltering =
false
;
grid.MasterTableView.Columns.Add(boundColumn);
boundColumn =
new
GridBoundColumn();
boundColumn.DataField =
"Country"
;
boundColumn.HeaderText =
"Country"
;
boundColumn.UniqueName =
"Country"
;
boundColumn.FilterTemplate =
new
ComboFilterTemplate(Page,
"Country"
,
"Country"
);
grid.MasterTableView.Columns.Add(boundColumn);
Panel1.Controls.Add(grid);
}
void
grid_NeedDataSource(
object
source, GridNeedDataSourceEventArgs e)
{
DataTable dt = GetDataTable(
"SELECT CustomerID, Country FROM Customers"
);
(source
as
RadGrid).DataSource = dt;
}
}
public
class
ComboFilterTemplate : ITemplate
{
private
RadComboBox combo;
private
string
columnName;
protected
string
field;
protected
String connectionString;
protected
Page page;
public
ComboFilterTemplate(Page Page,
string
ColumnName,
string
Field)
{
field = Field;
columnName = ColumnName;
page = Page;
}
public
void
InstantiateIn(Control container)
{
combo =
new
RadComboBox();
combo.ID = String.Format(
"RadComboBox{0}"
, columnName);
combo.AppendDataBoundItems =
true
;
if
(field.Length > 0)
combo.DataTextField = field;
combo.DataValueField = field;
combo.EmptyMessage =
"Select"
;
combo.Items.Insert(0,
new
RadComboBoxItem(
"All"
));
combo.DataBound += combo_DataBound;
combo.DataBinding +=
new
EventHandler(combo_DataBinding);
combo.OnClientSelectedIndexChanged = String.Format(
"ClientComboFilterSelected_{0}"
, columnName);
container.Controls.Add(combo);
}
void
combo_DataBinding(
object
sender, EventArgs e)
{
DataTable dt = GetDataTable(
"SELECT distinct Country FROM Customers"
);
combo.DataSource = dt;
combo.DataTextField = columnName;
combo.DataValueField = columnName;
}
void
combo_DataBound(
object
sender, EventArgs e)
{
RadComboBox combo = (RadComboBox)sender;
GridItem container = (GridItem)combo.NamingContainer;
string
script =
"function ClientComboFilterSelected_"
+ columnName +
"(sender,args){var tableView=$find(\""
+ ((GridItem)container).OwnerTableView.ClientID +
"\");tableView.filter(\""
+ field +
"\",args.get_item().get_value(),\"EqualTo\");}"
;
ScriptManager.RegisterStartupScript(page, page.GetType(), String.Format(
"ClientComboFilterSelected_{0}"
, field), script,
true
);
combo.SelectedValue = container.OwnerTableView.GetColumn(columnName).CurrentFilterValue;
}
}
Thanks,
Princy