Binding objects to a grid
Hi,
I have a class called let's say "Customer" which has some public properties like ID, FirstName, LastName, DisplayName etc. etc.
So what I normally do is create a generic list (of Customer) type of object and fill it up and then assign the whole thing to a grid.
Obviously the grid shows me ID, FirstName, LastName, DisplayName and remaining public properties it can bind.
What I want to achieve is make a particular property non-bindable (using some sort of attribute). Is this possible?
At the moment I am using Grid's on column added event to make those columns invisible but I would like to make it neat.
Also is it possible to make it bindable but hidden using attributes?
Cheers :)
Re: Binding objects to a grid
Quote:
What I want to achieve is make a particular property non-bindable (using some sort of attribute). Is this possible?
There's a Bindable attribute but it doesn't actually prevent the property being bound. I'm not sure whether the grid will automatically create a column or not.
Quote:
Also is it possible to make it bindable but hidden using attributes?
What exactly does "hidden" mean?
Re: Binding objects to a grid
I tried to find the "Bindable" attribute but it says attribute is not defined..! Do I have to add anything special to the references or IMPORT any library?
What I meant with "Hidden" was "Bindable" but treated as a "Hidden" column in the grid.
Re: Binding objects to a grid
OK
Code:
Imports System.ComponentModel
<Bindable(False)> _
Public Property MyProperty() As Integer
Get
' Insert code here.
Return 0
End Get
Set
' Insert code here.
End Set
End Property
took care of the binding problem. I am trying to find the solution to problem #2.
Re: Binding objects to a grid
Quote:
Originally Posted by wrack
I tried to find the "Bindable" attribute but it says attribute is not defined..! Do I have to add anything special to the references or IMPORT any library?
Almost certainly. What did MSDN say when you looked it up?
Quote:
Originally Posted by wrack
What I meant with "Hidden" was "Bindable" but treated as a "Hidden" column in the grid.
Hiding a column in a grid has nothing to do with binding. If you want to hide the grid column then hide the grid column.
Re: Binding objects to a grid
Yes at the moment I am hiding the grid column but I have to do it on the grid's column added event and look for a given column name to hide it.
For example, I normally bind an object to a grid. Now all the object's have ID property which relates to lets CustomerId field in the database table.
So for the purpose of UI, I don't need to display that column, just use the ID value when a row is selected so I just make the column visible in the event I am talking about.
If I have access to at the design time then I would not have to ask this question since I can make it hidden right there. But I need to reduce the amount of coding and also make the code more managable so if I can achieve this using attributes then all honky dory otherwise I will drop the idea.
Cheers :)
Re: Binding objects to a grid
You are presumably allowing the grid to create the columns for you. That's fine if you want to accept all the defaults but, as you don't want to, you should be creating the columns yourself in the designer. Create your columns at design time and set their DataPropertyName appropriately. You can then set the grid's AutoGenerateColumns property to False in code if appropriate. Note that you can create some columns at design time and have others auto-generated if you want.
Re: Binding objects to a grid
Ahh I didn't knew about creating some columns at design time and rest autogenerate.