I enabled filtering on my command column buy setting
.AllowFiltering =
true
I also have a cellformatting event that will change the button text based on a date but I no longer am able to ref the cell.commandbutton as the cell type has changed from "GridCommandCellElement" to
"GridFilterCellElement"
26 Answers, 1 is accepted
I don't understand exactly what you mean by this, but the CellFormatting event fires, for all the cells, and if to do some specific action if the cell element is GridCommandCellElement you can do something like:
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
var commandCell = e.CellElement
as
GridCommandCellElement;
if
(commandCell !=
null
)
{
// do something here
}
}
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
GridCommandCellElement cellSent = (GridCommandCellElement)e.CellElement;
package = (Package)cellSent.RowInfo.DataBoundItem;
cellSent.CommandButton.Text =
"Mark As Sent";
then when I Allowfiltering on this column the CellElement object type has changed to "GridFilterCellElement"
so now I need to cast the element as type "GridFilterCellElement" not "GridCommandCellElement" as above.
GridFilterCellElement cell = (GridFilterCellElement)e.CellElement;
by having to do this I no longer have access to the commandButton I hope this makes more sense
You shouldn't specify .AllowFiltering on the cell Element, you can specify to allow filtering on the column itself, something like this:
radGridView1.EnableFiltering =
true
;
radGridView1.Columns[0].AllowFiltering =
true
;
radGridView1.Columns[1].AllowFiltering =
false
;
and if possible you should set AllowFiltering to individual columns on DataBindingComplete event, to avoid any Object reference not set to an instance of an object exceptions.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
Best Regards,
Emanuel Varga
OK, let's assume filtering would be enabled, it should filter based on what? the Text of the button? Value?
Please tell me what you want to accomplish and i will try to help you achieve it.
Best Regards,
Emanuel Varga
if the record has been sent then the button is disabled but the display text would be "Sent On <datehere>"
I change the text of the button in the "CellFormatting" event buy checking a date in my dataobject from "
mycell.RowInfo.DataBoundItem"
the idea was to give the user a custom filter to show only records that have been sent, have not been sent, or no filter at all buy check the date in my dataobject of the call.value property.
I would think the cell value (bound datafield) or lets us pick if it is apply to the bound data, or the button text
public
class
CustomCommandColumn : GridViewCommandColumn
{
public
CustomCommandColumn()
:
base
()
{
}
public
CustomCommandColumn(
string
name)
:
base
(name)
{
}
public
override
bool
AllowFiltering
{
get
{
return
true
;
}
}
}
We just need this, because by default the AllowFiltering of the CommandColumn is set to false.
After that you should handle the editor Required Event, and the CellBeginEdit event:
void
radGridView1_CellBeginEdit(
object
sender, GridViewCellCancelEventArgs e)
{
if
(radGridView1.Columns[e.ColumnIndex].Name !=
"Command"
)
{
return
;
}
var editor = radGridView1.ActiveEditor
as
RadDropDownListEditor;
var dropDownElement = editor.EditorElement
as
RadDropDownListEditorElement;
dropDownElement.DataSource =
new
object
[] {
"Sent"
,
"Not Sent"
};
}
void
radGridView1_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
var editManager = sender
as
GridViewEditManager;
if
(editManager ==
null
|| radGridView1.CurrentColumn.Name !=
"Command"
)
{
return
;
}
e.Editor =
new
RadDropDownListEditor();
e.EditorType =
typeof
(RadDropDownListEditor);
}
This should get you started, please let me know how it is going, after this it should be just a matter of implementing a custom filtering event and showing the results.
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
this is what I currently have (below)
when I set ".EnableFiltering = true;" my cell type changes from "GridCommandCellElement" to "GridFilterCellElement"
when that happens I lose the ".CommadButton"
Thanks for your help on this
GridCommandCellElement cellSent = (GridCommandCellElement)e.CellElement;
package = (Package)cellSent.RowInfo.DataBoundItem;
//GridFilterCellElement cell = (GridFilterCellElement)e.CellElement;
//GridCommandCellElement cell = (GridCommandCellElement)cell2.ColumnInfo;
if
(package.DateSent != DateTime.MinValue)
{
cellSent.CommandButton.Text =
"Sent On: "
+ package.DateSent.ToString(CONST_DATE_FORMAT);
cellSent.CommandButton.ToolTipText =
"Package was Marked As Sent On "
+ package.DateSent.ToString(CONST_DATE_FORMAT) +
" by "
+ Thread.CurrentPrincipal.Identity.Name;
cellSent.CommandButton.Enabled =
false
;
}
else
{
cellSent.CommandButton.Text =
"Mark As Sent"
;
cellSent.CommandButton.ToolTipText =
"Mark Package As Sent To UWO"
;
cellSent.CommandButton.Enabled =
true
;
}
It cannot change the type to that, but because you also enabled filtering on that column, the first cell that is formatted is the GridFilterCellElement, please add a check in the beginning of the CellFormatting event something like:
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
var cellSent = e.CellElement
as
GridCommandCellElement;
if
(cellSent !=
null
)
{
var package = (Package)cellSent.RowInfo.DataBoundItem;
if
(package.DateSent != DateTime.MinValue)
{
cellSent.CommandButton.Text =
"Sent On: "
+ package.DateSent.ToString(CONST_DATE_FORMAT);
cellSent.CommandButton.ToolTipText =
"Package was Marked As Sent On "
+ package.DateSent.ToString(CONST_DATE_FORMAT) +
" by "
+ Thread.CurrentPrincipal.Identity.Name;
cellSent.CommandButton.Enabled =
false
;
}
else
{
cellSent.CommandButton.Text =
"Mark As Sent"
;
cellSent.CommandButton.ToolTipText =
"Mark Package As Sent To UWO"
;
cellSent.CommandButton.Enabled =
true
;
}
}
}
Please let me know if it's working like this for you.
One more thing, before I forget, are you using the latest version of telerik controls? Q2 2010 SP2, ...914?
Best Regards,
Emanuel Varga
yes we are using that version: 2010.2.10.914
1. I have now lost the abilty to sort my columns
2. Can I add my custom filters so they show when you click on the filter Image
- and limit the options to: no filter, sent, not sent
First i want to apologize for the late answer,
1. Sorting on all the columns, or just on the command column?, in my tests, sort is working normally.
2. This here is the problem, because, the context menu that is opening is called by the grid directly, and i've been struggling to get to the sender column, in order to override this behavior for just the custom command column, but so far i have found just this brute way of doing things:
using
System;
using
System.Collections.Generic;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
Telerik.WinControls.UI;
public
partial
class
Form1 : Form
{
private
RadGridView radGridView1;
public
Form1()
{
InitializeComponent();
Controls.Add(radGridView1 =
new
RadGridView());
radGridView1.CellFormatting +=
new
CellFormattingEventHandler(radGridView1_CellFormatting);
radGridView1.DataBindingComplete +=
new
GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
radGridView1.EditorRequired +=
new
EditorRequiredEventHandler(radGridView1_EditorRequired);
radGridView1.CellBeginEdit +=
new
GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
radGridView1.ReadOnly =
true
;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.Dock = DockStyle.Fill;
radGridView1.DataSource =
new
TestsCollection(10);
radGridView1.EnableFiltering =
true
;
}
void
radGridView1_ContextMenuOpening(
object
sender, ContextMenuOpeningEventArgs e)
{
RadDropDownMenu dtMenu =
new
RadDropDownMenu();
RadMenuItem r =
default
(RadMenuItem);
RadMenuSeparatorItem sepItm =
default
(RadMenuSeparatorItem);
//No Filter
r =
new
RadMenuItem();
r.Text =
"No Filter"
;
dtMenu.Items.Add(r);
//r.Click += NoFilter_Click;
//Separator
sepItm =
new
RadMenuSeparatorItem();
dtMenu.Items.Add(sepItm);
r =
new
RadMenuItem();
r.Text =
"OK"
;
dtMenu.Items.Add(r);
//r.Click += Status0_Click;
r =
new
RadMenuItem();
r.Text =
"Warning"
;
dtMenu.Items.Add(r);
//r.Click += Status1_Click
e.ContextMenu = dtMenu;
radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
}
void
radGridView1_CellBeginEdit(
object
sender, GridViewCellCancelEventArgs e)
{
if
(radGridView1.Columns[e.ColumnIndex].Name !=
"Command"
)
{
return
;
}
var editor = radGridView1.ActiveEditor
as
RadDropDownListEditor;
var dropDownElement = editor.EditorElement
as
RadDropDownListEditorElement;
dropDownElement.DataSource =
new
object
[] {
"Sent"
,
"Not Sent"
};
}
void
radGridView1_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
var editManager = sender
as
GridViewEditManager;
if
(editManager ==
null
|| radGridView1.CurrentColumn.Name !=
"Command"
)
{
return
;
}
e.Editor =
new
RadDropDownListEditor();
e.EditorType =
typeof
(RadDropDownListEditor);
}
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
var column =
new
CustomCommandColumn(
"Command"
);
column.CustomDataOperation = CustomDataOperation.Filtering;
this
.radGridView1.Columns.Add(column);
}
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.ColumnInfo.Name ==
"Command"
&& e.CellElement
as
GridFilterCellElement !=
null
)
{
var gridFilterElement = e.CellElement
as
GridFilterCellElement;
var filterButtonElement = gridFilterElement.Children[0]
as
GridFilterButtonElement;
filterButtonElement.MouseDown -=
new
MouseEventHandler(filterButtonElement_MouseDown);
filterButtonElement.MouseDown +=
new
MouseEventHandler(filterButtonElement_MouseDown);
}
var commandCell = e.CellElement
as
GridCommandCellElement;
if
(commandCell !=
null
)
{
var test = e.CellElement.RowInfo.DataBoundItem
as
Test;
if
(test.Id % 2 == 0)
{
commandCell.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
commandCell.CommandButton.TextElement.AutoSize =
true
;
commandCell.CommandButton.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
}
else
{
commandCell.Value =
"Not Sent"
;
commandCell.Text =
"Not Sent"
;
commandCell.CommandButton.Text =
"Not Sent"
;
}
}
}
void
filterButtonElement_MouseDown(
object
sender, MouseEventArgs e)
{
radGridView1.ContextMenuOpening +=
new
ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
}
}
#region Custom Command Column
public
class
CustomCommandColumn : GridViewCommandColumn
{
public
CustomCommandColumn()
:
base
()
{
}
public
CustomCommandColumn(
string
name)
:
base
(name)
{
}
public
override
bool
AllowFiltering
{
get
{
return
true
;
}
}
}
#endregion Custom Command Column
#region Business Objects
public
enum
MyEnum
{
Value1 = 0,
Value2,
Value3
}
public
class
Test
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
MyEnum Value
{
get
;
set
;
}
public
Test(
int
id,
string
name, MyEnum value)
{
this
.Id = id;
this
.Name = name;
this
.Value = value;
}
}
public
class
TestsCollection : List<Test>
{
public
TestsCollection(
int
noItems)
{
for
(
int
i = 0; i < noItems; i++)
{
this
.Add(
new
Test(i,
"test"
+ i, (MyEnum)(i % 3)));
}
}
}
#endregion Business Objects
Please let me know if this helps. Another way that would not involve always registering and unregistering to the ContextMenuOpening event would be to use a boolean flag, that's being set on filterButtonElement.MouseDown and being reset after setting the custom ContextMenu.
Best Regards,
Emanuel Varga
I've updated the previous example to show the selected filter and to apply filter descriptors based on the selection,
using
System;
using
System.Collections.Generic;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
Telerik.WinControls.Data;
using
Telerik.WinControls.UI;
public
partial
class
Form1 : Form
{
private
RadGridView radGridView1;
public
Form1()
{
InitializeComponent();
Controls.Add(radGridView1 =
new
RadGridView());
radGridView1.CellFormatting +=
new
CellFormattingEventHandler(radGridView1_CellFormatting);
radGridView1.DataBindingComplete +=
new
GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
radGridView1.EditorRequired +=
new
EditorRequiredEventHandler(radGridView1_EditorRequired);
radGridView1.CellBeginEdit +=
new
GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
radGridView1.ReadOnly =
true
;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.Dock = DockStyle.Fill;
radGridView1.DataSource =
new
TestsCollection(10);
radGridView1.EnableFiltering =
true
;
}
void
radGridView1_ContextMenuOpening(
object
sender, ContextMenuOpeningEventArgs e)
{
RadDropDownMenu dtMenu =
new
RadDropDownMenu();
var itemSelected =
false
;
//No Filter
var noFilterMenuItem =
new
RadMenuItem();
noFilterMenuItem.Text =
"No Filter"
;
noFilterMenuItem.Click +=
new
EventHandler(NoFilter_MenuItem_Click);
dtMenu.Items.Add(noFilterMenuItem);
//Separator
var separator =
new
RadMenuSeparatorItem();
dtMenu.Items.Add(separator);
var menuItem =
new
RadMenuItem();
menuItem.Click +=
new
EventHandler(SentFilter_MenuItem_Click);
menuItem.Text =
"Sent"
;
menuItem.IsChecked = IsFilterActive(
"Command"
,
"Sent"
);
dtMenu.Items.Add(menuItem);
itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
menuItem =
new
RadMenuItem();
menuItem.Text =
"Not Sent"
;
menuItem.IsChecked = IsFilterActive(
"Command"
,
"Not Sent"
);
menuItem.Click +=
new
EventHandler(NotSentFilter_MenuItem_Click);
itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
dtMenu.Items.Add(menuItem);
noFilterMenuItem.IsChecked = !itemSelected;
e.ContextMenu = dtMenu;
radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
}
private
bool
IsFilterActive(
string
propertyName,
string
filterString)
{
foreach
(var filter
in
radGridView1.FilterDescriptors)
{
if
(filter.PropertyName == propertyName && filter.Value == filterString)
{
return
true
;
}
}
return
false
;
}
void
NotSentFilter_MenuItem_Click(
object
sender, EventArgs e)
{
var filterFound =
false
;
for
(
int
i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
{
var filter = radGridView1.FilterDescriptors[i];
if
(filter.PropertyName ==
"Command"
)
{
filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
filter.Value =
"Not Sent"
;
filterFound =
true
;
break
;
}
}
if
(!filterFound)
{
radGridView1.FilterDescriptors.Add(
new
FilterDescriptor(
"Command"
, FilterOperator.StartsWith,
"Not Sent"
));
}
}
void
SentFilter_MenuItem_Click(
object
sender, EventArgs e)
{
var filterFound =
false
;
for
(
int
i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
{
var filter = radGridView1.FilterDescriptors[i];
if
(filter.PropertyName ==
"Command"
)
{
filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
filter.Value =
"Sent"
;
filterFound =
true
;
break
;
}
}
if
(!filterFound)
{
radGridView1.FilterDescriptors.Add(
new
FilterDescriptor(
"Command"
, FilterOperator.StartsWith,
"Sent"
));
}
}
void
NoFilter_MenuItem_Click(
object
sender, EventArgs e)
{
for
(
int
i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
{
var filter = radGridView1.FilterDescriptors[i];
if
(filter.PropertyName ==
"Command"
)
{
radGridView1.FilterDescriptors.RemoveAt(i);
break
;
}
}
}
void
radGridView1_CellBeginEdit(
object
sender, GridViewCellCancelEventArgs e)
{
if
(radGridView1.Columns[e.ColumnIndex].Name !=
"Command"
)
{
return
;
}
// because you are using a custom context menu you don't have to show the dropdown anymore
e.Cancel =
true
;
//var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
//var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
//dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
}
void
radGridView1_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
var editManager = sender
as
GridViewEditManager;
if
(editManager ==
null
|| radGridView1.CurrentColumn.Name !=
"Command"
)
{
return
;
}
e.Editor =
new
RadDropDownListEditor();
e.EditorType =
typeof
(RadDropDownListEditor);
}
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
var column =
new
CustomCommandColumn(
"Command"
);
column.FieldName =
"Command"
;
column.CustomDataOperation = CustomDataOperation.Filtering;
this
.radGridView1.Columns.Add(column);
}
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.ColumnInfo.Name ==
"Command"
&& e.CellElement
as
GridFilterCellElement !=
null
)
{
var gridFilterElement = e.CellElement
as
GridFilterCellElement;
var filterButtonElement = gridFilterElement.Children[0]
as
GridFilterButtonElement;
filterButtonElement.MouseDown -=
new
MouseEventHandler(filterButtonElement_MouseDown);
filterButtonElement.MouseDown +=
new
MouseEventHandler(filterButtonElement_MouseDown);
}
var commandCell = e.CellElement
as
GridCommandCellElement;
if
(commandCell !=
null
)
{
var test = e.CellElement.RowInfo.DataBoundItem
as
Test;
if
(test.Id % 2 == 0)
{
commandCell.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.Value =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
commandCell.CommandButton.TextElement.AutoSize =
true
;
commandCell.CommandButton.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
}
else
{
commandCell.Value =
"Not Sent"
;
commandCell.Text =
"Not Sent"
;
commandCell.CommandButton.Text =
"Not Sent"
;
}
}
}
void
filterButtonElement_MouseDown(
object
sender, MouseEventArgs e)
{
radGridView1.ContextMenuOpening +=
new
ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
}
}
#region Custom Command Column
public
class
CustomCommandColumn : GridViewCommandColumn
{
public
CustomCommandColumn()
:
base
()
{
}
public
CustomCommandColumn(
string
name)
:
base
(name)
{
}
public
override
bool
AllowFiltering
{
get
{
return
true
;
}
}
}
#endregion Custom Command Column
#region Business Objects
public
enum
MyEnum
{
Value1 = 0,
Value2,
Value3
}
public
class
Test
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
MyEnum Value
{
get
;
set
;
}
public
Test(
int
id,
string
name, MyEnum value)
{
this
.Id = id;
this
.Name = name;
this
.Value = value;
}
}
public
class
TestsCollection : List<Test>
{
public
TestsCollection(
int
noItems)
{
for
(
int
i = 0; i < noItems; i++)
{
this
.Add(
new
Test(i,
"test"
+ i, (MyEnum)(i % 3)));
}
}
}
#endregion Business Objects
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
just one other thing how can I get the current filter text to display in the column so the user can see what the current filter is with out clicking on the image
This is the updated code, I've added that filter text also in the GridFilterCellElement.
using
System;
using
System.Collections.Generic;
using
System.Windows.Forms;
using
Telerik.WinControls;
using
Telerik.WinControls.Data;
using
Telerik.WinControls.UI;
using
Telerik.WinControls.UI.Localization;
public
partial
class
Form1 : Form
{
private
RadGridView radGridView1;
public
Form1()
{
InitializeComponent();
Controls.Add(radGridView1 =
new
RadGridView());
radGridView1.CellFormatting +=
new
CellFormattingEventHandler(radGridView1_CellFormatting);
radGridView1.DataBindingComplete +=
new
GridViewBindingCompleteEventHandler(radGridView1_DataBindingComplete);
radGridView1.EditorRequired +=
new
EditorRequiredEventHandler(radGridView1_EditorRequired);
radGridView1.CellBeginEdit +=
new
GridViewCellCancelEventHandler(radGridView1_CellBeginEdit);
}
protected
override
void
OnLoad(EventArgs e)
{
base
.OnLoad(e);
radGridView1.ReadOnly =
true
;
radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
radGridView1.Dock = DockStyle.Fill;
radGridView1.DataSource =
new
TestsCollection(10);
radGridView1.EnableFiltering =
true
;
}
void
radGridView1_ContextMenuOpening(
object
sender, ContextMenuOpeningEventArgs e)
{
RadDropDownMenu dtMenu =
new
RadDropDownMenu();
var itemSelected =
false
;
//No Filter
var noFilterMenuItem =
new
RadMenuItem();
noFilterMenuItem.Text =
"No Filter"
;
noFilterMenuItem.Click +=
new
EventHandler(NoFilter_MenuItem_Click);
dtMenu.Items.Add(noFilterMenuItem);
//Separator
var separator =
new
RadMenuSeparatorItem();
dtMenu.Items.Add(separator);
var menuItem =
new
RadMenuItem();
menuItem.Click +=
new
EventHandler(SentFilter_MenuItem_Click);
menuItem.Text =
"Sent"
;
menuItem.IsChecked = IsFilterActive(
"Command"
,
"Sent"
);
dtMenu.Items.Add(menuItem);
itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
menuItem =
new
RadMenuItem();
menuItem.Text =
"Not Sent"
;
menuItem.IsChecked = IsFilterActive(
"Command"
,
"Not Sent"
);
menuItem.Click +=
new
EventHandler(NotSentFilter_MenuItem_Click);
itemSelected = !itemSelected ? menuItem.IsChecked : itemSelected;
dtMenu.Items.Add(menuItem);
noFilterMenuItem.IsChecked = !itemSelected;
e.ContextMenu = dtMenu;
radGridView1.ContextMenuOpening -= radGridView1_ContextMenuOpening;
}
private
bool
IsFilterActive(
string
propertyName,
string
filterString)
{
foreach
(var filter
in
radGridView1.FilterDescriptors)
{
if
(filter.PropertyName == propertyName && filter.Value == filterString)
{
return
true
;
}
}
return
false
;
}
void
NotSentFilter_MenuItem_Click(
object
sender, EventArgs e)
{
var filter = GetCurrentFilterForCommandColumn();
if
(filter !=
null
)
{
filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
filter.Value =
"Not Sent"
;
}
else
{
radGridView1.FilterDescriptors.Add(
new
FilterDescriptor(
"Command"
, FilterOperator.StartsWith,
"Not Sent"
));
}
}
void
SentFilter_MenuItem_Click(
object
sender, EventArgs e)
{
var filter = GetCurrentFilterForCommandColumn();
if
(filter !=
null
)
{
filter.Operator = Telerik.WinControls.Data.FilterOperator.StartsWith;
filter.Value =
"Sent"
;
}
else
{
radGridView1.FilterDescriptors.Add(
new
FilterDescriptor(
"Command"
, FilterOperator.StartsWith,
"Sent"
));
}
}
private
FilterDescriptor GetCurrentFilterForCommandColumn()
{
for
(
int
i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
{
var filter = radGridView1.FilterDescriptors[i];
if
(filter.PropertyName ==
"Command"
)
{
return
filter;
}
}
return
null
;
}
void
NoFilter_MenuItem_Click(
object
sender, EventArgs e)
{
for
(
int
i = radGridView1.FilterDescriptors.Count - 1; i >= 0; i--)
{
var filter = radGridView1.FilterDescriptors[i];
if
(filter.PropertyName ==
"Command"
)
{
radGridView1.FilterDescriptors.RemoveAt(i);
break
;
}
}
}
void
radGridView1_CellBeginEdit(
object
sender, GridViewCellCancelEventArgs e)
{
if
(radGridView1.Columns[e.ColumnIndex].Name !=
"Command"
)
{
return
;
}
// because you are using a custom context menu you don't have to show the dropdown anymore
e.Cancel =
true
;
//var editor = radGridView1.ActiveEditor as RadDropDownListEditor;
//var dropDownElement = editor.EditorElement as RadDropDownListEditorElement;
//dropDownElement.DataSource = new object[] { "Sent", "Not Sent" };
}
void
radGridView1_EditorRequired(
object
sender, EditorRequiredEventArgs e)
{
var editManager = sender
as
GridViewEditManager;
if
(editManager ==
null
|| radGridView1.CurrentColumn.Name !=
"Command"
)
{
return
;
}
e.Editor =
new
RadDropDownListEditor();
e.EditorType =
typeof
(RadDropDownListEditor);
}
void
radGridView1_DataBindingComplete(
object
sender, GridViewBindingCompleteEventArgs e)
{
var column =
new
CustomCommandColumn(
"Command"
);
column.FieldName =
"Command"
;
column.CustomDataOperation = CustomDataOperation.Filtering;
this
.radGridView1.Columns.Add(column);
}
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
if
(e.CellElement.ColumnInfo.Name ==
"Command"
&& e.CellElement
as
GridFilterCellElement !=
null
)
{
var gridFilterElement = e.CellElement
as
GridFilterCellElement;
var currentFilter = GetCurrentFilterForCommandColumn();
gridFilterElement.FilterOperatorText.Text = (currentFilter ==
null
?
RadGridLocalizationProvider.CurrentProvider.GetLocalizedString(RadGridStringId.FilterOperatorNoFilter) :
currentFilter.Value.ToString())+
":"
;
var filterButtonElement = gridFilterElement.Children[0]
as
GridFilterButtonElement;
filterButtonElement.MouseDown -=
new
MouseEventHandler(filterButtonElement_MouseDown);
filterButtonElement.MouseDown +=
new
MouseEventHandler(filterButtonElement_MouseDown);
}
var commandCell = e.CellElement
as
GridCommandCellElement;
if
(commandCell !=
null
)
{
var test = e.CellElement.RowInfo.DataBoundItem
as
Test;
if
(test.Id % 2 == 0)
{
commandCell.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.Value =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
commandCell.CommandButton.AutoSizeMode = RadAutoSizeMode.FitToAvailableSize;
commandCell.CommandButton.TextElement.AutoSize =
true
;
commandCell.CommandButton.Text =
"Sent on "
+ e.CellElement.RowIndex + DateTime.Now.AddDays(-1 * e.CellElement.RowIndex);
}
else
{
commandCell.Value =
"Not Sent"
;
commandCell.Text =
"Not Sent"
;
commandCell.CommandButton.Text =
"Not Sent"
;
}
}
}
void
filterButtonElement_MouseDown(
object
sender, MouseEventArgs e)
{
radGridView1.ContextMenuOpening +=
new
ContextMenuOpeningEventHandler(radGridView1_ContextMenuOpening);
}
}
#region Custom Command Column
public
class
CustomCommandColumn : GridViewCommandColumn
{
public
CustomCommandColumn()
:
base
()
{
}
public
CustomCommandColumn(
string
name)
:
base
(name)
{
}
public
override
bool
AllowFiltering
{
get
{
return
true
;
}
}
}
#endregion Custom Command Column
#region Business Objects
public
enum
MyEnum
{
Value1 = 0,
Value2,
Value3
}
public
class
Test
{
public
int
Id
{
get
;
set
;
}
public
string
Name
{
get
;
set
;
}
public
MyEnum Value
{
get
;
set
;
}
public
Test(
int
id,
string
name, MyEnum value)
{
this
.Id = id;
this
.Name = name;
this
.Value = value;
}
}
public
class
TestsCollection : List<Test>
{
public
TestsCollection(
int
noItems)
{
for
(
int
i = 0; i < noItems; i++)
{
this
.Add(
new
Test(i,
"test"
+ i, (MyEnum)(i % 3)));
}
}
}
#endregion Business Objects
And just to think you almost gave up on this....
Hope this helps, if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
and as always if you have any other questions or comments, please let me know,
Best Regards,
Emanuel Varga
I have a question adjacent to the topic discussed here.
I have a radgridview with several columns, the first of which is of "CheckBox" type.
The last column is a CommandColumn, which contains buttons with detalis to show for every row (= every company listed). Below there is a separate usual button which launches some actions to accomplish, after the completion of which I need to set the state of the button of the Commandcolumn in every row to "Enabled"/"Disabled" based on the state of the first cell of the same row according to the following rule:
if the CheckBox cell in the row is checked, then the button in the CommandColumn of this row has to be enabled,
if the CheckBox cell in the row is not checked, then the button in the CommandColumn of this row has to be disabled,
I have tried to set the state of the buttons using a routine in "CellFormatting" event I have found in one of the topics on this forum:
var commandcell = e.CellElement as GridCommandCellElement;
if (somecondition)
commandcell.CommandButton.Enabled = true;
However, I don't seem to be able to apply a condition based on the corresponding CheckBox cell state to modify the state of the button in the CommandColumn, using the same event handler and the same object "CellFormattingEvents e" for both.
Is there a way to do it, or maybe some other workaround?
Thank you very much in advance.
Best Regards,
Jeen B.
P.S.: If there is a topic already created for this issue, I am sorry. Please, be so kind to redirect me to it, since I have not found it on the forum.
Thank you for writing.
In the CellFormatting event you have access to the GridViewRowInfo from which you can extract the check box value for the specific row and compose the condition. Here is a sample code snippet:
public
Form1()
{
InitializeComponent();
DataTable dt =
new
DataTable();
dt.Columns.Add(
"Active"
,
typeof
(
bool
));
dt.Columns.Add(
"Id"
,
typeof
(
int
));
dt.Columns.Add(
"Name"
,
typeof
(
string
));
for
(
int
i = 0; i < 100; i++)
{
dt.Rows.Add(i % 2 == 0, i,
"Name"
+ i);
}
this
.radGridView1.DataSource = dt;
GridViewCommandColumn commandColumn =
new
GridViewCommandColumn(
"CommandColumn"
);
radGridView1.MasterTemplate.Columns.Add(commandColumn);
this
.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
this
.radGridView1.CellFormatting += radGridView1_CellFormatting;
}
private
void
radGridView1_CellFormatting(
object
sender, CellFormattingEventArgs e)
{
GridCommandCellElement commandcell = e.CellElement
as
GridCommandCellElement;
if
(commandcell !=
null
&& e.Row.Cells[
"Active"
].Value !=
null
)
{
bool
cbCellValue = (
bool
)e.Row.Cells[
"Active"
].Value;
if
(cbCellValue)
{
commandcell.CommandButton.Enabled =
true
;
}
else
{
commandcell.CommandButton.Enabled =
false
;
}
}
}
I hope this information helps. Should you have further questions, I would be glad to help.
Regards,
Desislava
Telerik
Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.
Thank you very much!
Your post has been of much help to me:)
Best Regards,
Jeen B.
Hi Emanuel,
I follow your code but when I filter and choose one but no showing data. please see pic.
Below my code,
Public Class CustomCommandColumn
Inherits GridViewCommandColumn
Private _count As Boolean
Public Sub New()
MyBase.New()
End Sub
Public Sub New(ByVal name As String)
MyBase.New(name)
End Sub
Public Overrides Property AllowFiltering As Boolean
Get
Return True
End Get
Set(ByVal value As Boolean)
_count = value
End Set
End Property
End Class
With GvFilingList.MasterTemplate
.Columns.Add(New GridViewCheckBoxColumn() With {.Name = "Select", .HeaderText = "Select", .FieldName = .Name, .Width = 60})
.Columns.Add(New CustomCommandColumn("View") With {.HeaderText = "Form", .MaxWidth = 65, .MinWidth = 65, .AllowFiltering = True})
.Columns.Add(New GridViewTextBoxColumn() With {.Name = "EntityDocumentID", .HeaderText = "EntityDocumentID", .FieldName = .Name, .IsVisible = False, .VisibleInColumnChooser = False})
.Columns.Add(New GridViewCommandColumn("EmailPreview") With {.HeaderText = "Email", .Image = My.Resources.View_16x16, .MaxWidth = 65, .MinWidth = 65, .ImageAlignment = ContentAlignment.MiddleCenter})
.Columns.Add(New GridViewTextBoxColumn() With {.Name = "ConfirmationEntityDocumentID", .HeaderText = "Confirmation Entity Document ID", .FieldName = .Name, .IsVisible = False, .VisibleInColumnChooser = False})
End With
Private Sub GvFilingList_CellFormatting(sender As Object, e As CellFormattingEventArgs) Handles GvFilingList.CellFormatting
Try
If TypeOf e.CellElement Is GridDataCellElement Then
If e.Column.Name = "View" Then
If TypeOf (e.CellElement) Is GridCommandCellElement Then
Dim commandCell As GridCommandCellElement = CType(e.CellElement, GridCommandCellElement)
commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Visible
If (e.Row.Cells("EntityFilingID").Value) Is Nothing OrElse CInt(e.Row.Cells("EntityFilingID").Value) = 0 Then
commandCell.CommandButton.Visibility = Telerik.WinControls.ElementVisibility.Hidden
Else
If (e.Row.Cells("EntityDocumentID").Value) Is Nothing Then
' commandCell.CommandButton.Image = My.Resources.Resources.Upload_16x16
commandCell.CommandButton.Text = "Upload"
Else
' commandCell.CommandButton.Image = My.Resources.Resources.View_16x16
commandCell.CommandButton.Text = "View"
End If
End If
'commandCell.CommandButton.MaxSize = New Size(55, 26)
'commandCell.CommandButton.MinSize = New Size(55, 26)
'commandCell.CommandButton.Alignment = ContentAlignment.MiddleCenter
'commandCell.CommandButton.ImageAlignment = ContentAlignment.MiddleCenter
End If
End If
Catch ex As Exception
ClientLog.Insert(ex, Me.Name & ".GvKYC_CellFormatting")
ClientLog.MessageToUser_ErrorProcessName("Cell Formatting")
End Try
End Sub
Private Sub radGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As GridViewCellCancelEventArgs) Handles GvFilingList.CellBeginEdit
If GvFilingList.Columns(e.ColumnIndex).Name <> "View" Then
Return
End If
Dim editor = TryCast(GvFilingList.ActiveEditor, RadDropDownListEditor)
Dim dropDownElement = TryCast(editor.EditorElement, RadDropDownListEditorElement)
dropDownElement.DataSource = New Object() {"View", "Upload"}
End Sub
Private Sub radGridView1_EditorRequired(ByVal sender As Object, ByVal e As EditorRequiredEventArgs) Handles GvFilingList.EditorRequired
Dim editManager = TryCast(sender, GridViewEditManager)
If editManager Is Nothing OrElse GvFilingList.CurrentColumn.Name <> "View" Then
Return
End If
e.Editor = New RadDropDownListEditor()
e.EditorType = GetType(RadDropDownListEditor)
End Sub
Please tell me . where place is wrong
Hello, Moe,
It seems that both of the provided screenshots are identical. Are they supposed to produce different look?
As to the provided solution by Emanuel, I would like to note this is quite an old forum post (back from 2010) and it may not be applicable for the latest version of the RadGridView.
If you want to filter the GridViewCommandColumn, please refer to the attached zip file which demonstrates a sample approach. The achieved result is illustrated below. Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.
Hi Dess,
Actually , my command button don't show text and show only image. So how to do filter for that.
Thanks
Moe
Hello, Moe,
You should have a value stored in the data cell as it is demonstrated in the provided sample project. RadGridView filters the rows considering the cell values. Otherwise, the applied FilterDesciptor wouldn't have any effect on the row.
Then, in the CellFormatting event you can apply any image to the button inside the cell.
I believe that it would help you to achieve the desired functionality.
Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.