This is a migrated thread and some comments may be shown as answers.

Filtering Issue

12 Answers 218 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Gohar
Top achievements
Rank 1
Gohar asked on 08 Dec 2015, 11:26 AM

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 Onlyso 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

Sort by
0
Dimitar
Telerik team
answered on 08 Dec 2015, 12:54 PM
Hi Gohar,

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. 
 
Regards,
Dimitar
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gohar
Top achievements
Rank 1
answered on 09 Dec 2015, 08:12 AM
Your answer didn't solved my answer. Actually the problem is that, when I declare some filters for radGridView by hand (without code), then the Value properties of FilterDescriptors are null.
0
Dimitar
Telerik team
answered on 10 Dec 2015, 08:46 AM
Hello Gohar,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gohar
Top achievements
Rank 1
answered on 13 Dec 2015, 07:14 AM
I found the problem, but I don't know how to solve it. When I select an item from the tree shown in the filtering box (green box in the attached image) then I have to select an item from Available Filters box (red box in the attached image) to get the Value of FilterDescriptors. On the other hand, if I select an item from the Available Filters (red box in the attached image) then I can get the Value of FilterDescriptors. The problem is that by default no items are selected in the Available Filters, so the Value of FilterDescriptor is null. How can I solve this?
0
Dimitar
Telerik team
answered on 14 Dec 2015, 01:33 PM
Hello Gohar,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gohar
Top achievements
Rank 1
answered on 15 Dec 2015, 08:43 AM
I captured my screen and attached its file as Gif. Please see that the radGridView.FilterDescriptors.First().Value is null after adding new filter.
0
Gohar
Top achievements
Rank 1
answered on 15 Dec 2015, 08:53 AM
As you can see in the attached screenshot, I added a new filter and the pressed the OK button. As you can see in the screenshot the Value property is null.
0
Dimitar
Telerik team
answered on 15 Dec 2015, 03:07 PM
Hello Gohar,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gohar
Top achievements
Rank 1
answered on 16 Dec 2015, 06:51 AM

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?

0
Dimitar
Telerik team
answered on 16 Dec 2015, 12:31 PM
Hello Gohar,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Gohar
Top achievements
Rank 1
answered on 17 Dec 2015, 10:26 AM

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.

0
Accepted
Dimitar
Telerik team
answered on 18 Dec 2015, 01:14 PM
Hello Gohar,

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
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
GridView
Asked by
Gohar
Top achievements
Rank 1
Answers by
Dimitar
Telerik team
Gohar
Top achievements
Rank 1
Share this question
or