I have two problems with Filtering.
1) I need to set grid.Columns[i].FilterDescriptor.Expression to a string value, but as the Expression is Read Only, so I cannot do this. Note: I don't want to use grid.FilterDescriptors.Add(.....).
2) Why does the grid.FilterDescriptors.First(.....).Value return null? The PropertyName and Operand is OK, but Value is null.
12 Answers, 1 is accepted
Thank you for writing.
1. The descriptor is building the expression according to its Operator, Value and PropertyName. The expression cannot be set manually. However you can assign the descriptor to the column directly:
private
void
radButton1_Click(
object
sender, EventArgs e)
{
FilterDescriptor desc =
new
FilterDescriptor(
"Name"
, FilterOperator.Contains,
"Sam"
);
radGridView1.Columns[
"Name"
].FilterDescriptor = desc;
var value = radGridView1.FilterDescriptors.First().Value;
Console.WriteLine(value);
}
2. The Value property represents the currently used filter value. In your case, it may not be set. You can test this with the above code.
Please let me know if there is something else I can help you with.
Dimitar
Telerik

Thank you for writing back.
I tested this as well, but the value is correct. I have recorded a small video that shows what I am doing. Could please check it and let me know what else I need to do in order to reproduce this? In addition, I have attached my test project so you can see if it differs your real setup.
I am looking forward to your reply.
Regards,
Dimitar
Telerik

Thank you for writing back.
The filter is applied when the drop down is closed and you can get the default filter after that. For example, you can use the FilterChanged event.
I hope this helps.
Regards,
Dimitar
Telerik


Thank you for writing back.
Please note that the value depends on the actual filter and usually the property is not sufficient to represent the entire expression. For example, if you uncheck the a single item from the tree view the expression will look like this: "{(Name <> 'David')}". Perhaps you can use the expression to get the value that you need:
void
radGridView1_FilterChanged(
object
sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
Console.WriteLine(radGridView1.FilterDescriptors.FirstOrDefault().Expression);
}
Is this suitable for your case?
I hope this helps.
Regards,
Dimitar
Telerik

I feel I should explain a little more about the scenario I'm going to implement to you to make sure you understand exactly what the problem is.
My scenario is that I want the user can save the current state of GridView and when he/she opens the form later then the same settings are opened for him/her. I mean the current state of GridView is the filters that have been applied on the columns of GridView.
Unfortunately, since the Expression is read-only, so I cannot save this value and later assign the GridView's columns' Expression with the saved value; so I have to use PropertyName, Operator, and Value of FilterDescriptors. In addition, if complex filters are applied to the columns of GridView, then I cannot easily extract the equivalent amounts of PropertyName, Operator, and Value from Expressions and this is the reason of why I would like to use PropertyName, Operator and Value to save the current state of GridView and load the state later.
I don't know, but maybe you have an algorithm to extract all
ProperyNames, Operators and Values from GridView's columns' Expressions instead
of using PropertyName, Operator and Value of FilterDescriptors.
And my another important question is that what method do you prefer to
save the current state of a GridView and load it later when the user gets back
to the form?
Thank you for writing back.
This is supported out of the box and you can save the layout along with filters by just calling a single method:
private
void
radButton1_Click(
object
sender, EventArgs e)
{
radGridView1.SaveLayout(
"test.xml"
);
}
private
void
radButton2_Click(
object
sender, EventArgs e)
{
radGridView1.LoadLayout(
"test.xml"
);
}
Detailed information about this is available in the following articles:
Should you have any other questions do not hesitate to ask.
Regards,
Dimitar
Telerik

I cannot use the methods you mentioned, because the query that I use to fill the GridView may return different number of columns each time. Do you have any idea about this?
I apologize that I ask this question => what is your country time Zone, because you are so late to answer my questions.
Thank you for writing back.
I have further investigated this case and this is how you can get the value when the excel-like filtering is used:
void
radGridView1_FilterChanged(
object
sender, Telerik.WinControls.UI.GridViewCollectionChangedEventArgs e)
{
CompositeFilterDescriptor des = e.NewItems[0]
as
CompositeFilterDescriptor;
if
(des !=
null
)
{
Console.WriteLine(des.FilterDescriptors[0].Value);
}
}
In addition, you can serialize only the filters by using the .NET XmlSerializer . For example here is how you can serialize/deserialize the first descriptor:
private
void
radButton1_Click(
object
sender, EventArgs e)
{
SerializeObject<CompositeFilterDescriptor>(radGridView1.FilterDescriptors[0]
as
CompositeFilterDescriptor,
"test.xml"
);
radGridView1.FilterDescriptors.Clear();
}
private
void
radButton2_Click(
object
sender, EventArgs e)
{
CompositeFilterDescriptor f = DeSerializeObject<CompositeFilterDescriptor>(
"test.xml"
);
radGridView1.FilterDescriptors.Add(f);
}
public
void
SerializeObject<T>(T serializableObject,
string
fileName)
{
if
(serializableObject ==
null
)
{
return
;
}
try
{
XmlDocument xmlDocument =
new
XmlDocument();
XmlSerializer serializer =
new
XmlSerializer(serializableObject.GetType());
using
(MemoryStream stream =
new
MemoryStream())
{
serializer.Serialize(stream, serializableObject);
stream.Position = 0;
xmlDocument.Load(stream);
xmlDocument.Save(fileName);
stream.Close();
}
}
catch
(Exception ex)
{
//Log exception here
}
}
public
T DeSerializeObject<T>(
string
fileName)
{
if
(
string
.IsNullOrEmpty(fileName))
{
return
default
(T);
}
T objectOut =
default
(T);
try
{
string
attributeXml =
string
.Empty;
XmlDocument xmlDocument =
new
XmlDocument();
xmlDocument.Load(fileName);
string
xmlString = xmlDocument.OuterXml;
using
(StringReader read =
new
StringReader(xmlString))
{
Type outType =
typeof
(T);
XmlSerializer serializer =
new
XmlSerializer(outType);
using
(XmlReader reader =
new
XmlTextReader(read))
{
objectOut = (T)serializer.Deserialize(reader);
reader.Close();
}
read.Close();
}
}
catch
(Exception ex)
{
//Log exception here
}
return
objectOut;
}
Our time zone is GMT+2, please note that we do not guarantee an answer to a forum thread within a specific timeframe. Detailed information is available here: IMPORTANT INFORMATION ON USING THE TELERIK FORUMS
I hope this helps.
Regards,
Dimitar
Telerik