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.
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.
publicclassMyObject
{
publicMyObject(string myString, int id )
{
MyString = myString;
ID = id;
}
privateint id;
[Browsable(false)]publicint ID
{
get { return id; }
set { id = value; }
}
privatestring _myString;
publicstring MyString
{
get { return _myString; }
set { _myString = value; }
}
}
Regards,
Dinko | Tech Support Engineer
Progress Telerik
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")]
publicstring 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.
publicclassMyObject
{
publicMyObject(string myString, int id)
{
MyString = myString;
ID = id;
}
privateint id;
[MyAttribute("False")]
publicint ID
{
get { return id; }
set { id = value; }
}
privatestring _myString;
publicstring MyString
{
get { return _myString; }
set { _myString = value; }
}
}
privatevoidRadGridView1_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.