tags:

views:

251

answers:

1

In one of windows application in C# , I am using Dev Express Grid view control to bind some data and display it to user. I have custom business objects with properties defined for this purpose.Then I simple set the DataSource of the grid to the list of my custom business objects.

A while ago , there came a requirement which means that the columns to be displayed on to the grid will be dynamic. This means I cannot know at design time which fields I will need to display.

I was thinking of abandoning setting the DataSource and populating the grid manually by code. But I think this will cause many of the grid's features not to work properly, for example , grouping the data by drag n drop of fields to the header area etc. Is there any way to tell a grid at runtime to skip certain fields from a list of BO's when databinding to the grid ?

A: 

This is pretty simple, we do it all the time. You simply need to bind the grid to your datasource and it will do the rest for you.

Hiding fields is easy also, just set its VisibleIndex to -1

You could do something like this

C#

grid.FocusedView.Columns["Col1"].VisibleIndex = -1;

VB

grid.FocusedView.Columns("Col1").VisibleIndex = -1;
Alex DeLarge
VisibleIndex on what should be set to -1 ? Would appreciate if you put some details. Lets say I have a custom object which have 20 fields with properties ,and at runtime , I determine that only 5 fields need to be bound to the grid. What should I do so that when I set dataSource to the list , it shows the 5 appropriate fields only ? ?
Bhaskar
Apologies, the VisibleIndex property is set on the Column which can be found in the Columns collection of the active view. I've edited my answer and added an example.
Alex DeLarge
Alex , this would only serve to hide the column from displaying , but the fields remain bound to the grid. I was looking for a way to tell the grid not to even bind the fields that are not required. May be like create a field mapping that allows me to specify which fields from the custom business object to bind , what should be the position of the column , the caption of the column etc..
Bhaskar
Then simply do not select the fields from your business object?
Alex DeLarge