Custom Browsable Attribute

1 Answer 110 Views
GridView
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Mark asked on 26 Jul 2023, 04:39 PM

We want to use a customer Browsable() attribute to Show/Hide columns from a grid, but we are having an issue just getting it to work.  Does telerk grids support customer browsable attribute?   I know it does for DisplayName.  

If so, I could use some help.

1 Answer, 1 is accepted

Sort by
0
Dinko | Tech Support Engineer
Telerik team
answered on 30 Jul 2023, 12:10 PM

Hello Mark,

The control supports various attributes and the Browsable attribute is one of them. To test this scenario, I have created a sample project. When I set this attribute to the property of the object which the control is populated with, the column does not appear. However, my implementation may defers from yours. May I ask you to check the attached project and let me know what I am missing here?

When you run the project, you can observe that the ID column is not visible.

public class MyObject
{
    public MyObject(string myString, int id )
    {

        MyString = myString;
        ID = id;
    }
    private int id;

    [Browsable(false)]
    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string _myString;
    public string MyString
    {
        get { return _myString; }
        set { _myString = value; }
    }
}

Regards,
Dinko | Tech Support Engineer
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
commented on 31 Jul 2023, 11:35 AM

Yes, we are using this attribute, but we want a custome browsable attribute.  Something we can pass to the attribute that is not boolean, is evaluated in the attributed and Shows/Hides the columns based on that evaluation of what is passed to that 

 


public MyObject
{
   [MyIsBrowsable("ABC")]
   public string myString
}
Based on the value to MyIsBrowsable attribute, I want to SHOW/HIDE the column. I have seen customer "Browasble" attributes on Stack Overflow, but the example they gave just doesn't work.   In fact, I bult a customer browsable attribute that should work idenically to the Browsable attribute and could not get that to work either.  
Dinko | Tech Support Engineer
Telerik team
commented on 03 Aug 2023, 07:08 AM

Thank you for the additional details.

Upon checking your scenario, custom attributes are not supported and it will not be respected. In our code, we are using CurrencyManager Class and its GetItemProperties() to read and get the properties of the object which the control will be populated with and return a collection of PropertyDescriptors. Based on the return properties we are creating columns depending on their type. For example, if you have an object with two properties, one of them has BrowsableAttribute set to false. When the value is set to false, the currencyManager.GetItemProperties() method returns only 1 PropertyDescriptor and the grid will add 1 column. 

This method supports the default attributes and can't read custom ones. In your case, the CurrencyManager Class does not know what to do with the custom attribute and which property of the PropertyDescriptor needs to be set. 

I have searched for a way to make this work with custom code but to no avail. What I can suggest as a workaround is to subscribe to the DataBindingComplete event of the RadGridView. Inside the event handler, you can hide a column depending on your condition by setting the IsVisible property to false. With a reflection, you can get the attributes set to the property.

public class MyObject
{
    public MyObject(string myString, int id)
    {

        MyString = myString;
        ID = id;
    }
    private int id;

    [MyAttribute("False")]
    public int ID
    {
        get { return id; }
        set { id = value; }
    }

    private string _myString;
    public string MyString
    {
        get { return _myString; }
        set { _myString = value; }
    }
}

private void RadGridView1_DataBindingComplete(object sender, GridViewBindingCompleteEventArgs e)
{
    var customAttribute = typeof(MyObject).GetProperty("ID").GetCustomAttributes(false).ToList();
    if ((customAttribute[0] as MyAttribute).Browsable == "False")
    {
        this.radGridView1.Columns[1].IsVisible = false;
    }         
}

The Browsable property is a custom string property that is set from the parameter of the constructor. Give this approach a try and let me know if it works for you. I hope it will work for you.

 

 

Tags
GridView
Asked by
Mark
Top achievements
Rank 2
Bronze
Bronze
Veteran
Answers by
Dinko | Tech Support Engineer
Telerik team
Share this question
or