Hi,
I'm binding a grid to a custom datasource. My problem is that columns don't appear exactly as i'd like.
I can't set order and header's caption to columns, as well as, to decide which columns to be shown and which not.
It seems property order declaration inside the object doesn't determine columns order. So how do I change it?
Wingrid Maps column names to property names. Is there a way of changing it?
I think WinGrid only shows public object properties. I do inherit all my business objects and i don't want to show base class public properties in the grid neither some of the object itself. Again, Does any body knows something about it?
Thanks
I found the solution.
Every public object property (get/set) becomes a WinGrid Column with caption equals to property name. In order to set a custom caption you must add System.ComponentModel.DisplayName's attribute to the property.
For instance,
Protected int? mCellNum = null
[System.ComponentModel.DisplayName("Cell phone")]
public int? CellNum { get { return mCellNum ; } set { mCellNum = value; }}
If you don't want to show a specific property, for example a business ID, add System.ComponentModel.Browsable's attribute to the property.
Protected int? mID = null
[System.ComponentModel.Browsable(false)]
public int? ID { get { return mID ; } set { mID = value; } }
About column visible order issue there're several posts showing how to achieve a custom order.
The Order of the columns is determined by the BindingManager. I beleive it gets these from the ITypedList interface's GetProperties method. This will essentially be random unless you implement this interface yourself.
The way you are setting the Caption and Visibilty of the columns is fine, but you can also do it through the grid's object model. The way you are doing it has the advantage that the grid will not create a column for the ones that are Browsable false and also if you bind multiple grids to your objects it will save you from repeating code.
But you could also use the Hidden property of the column to hide columns, the column.Header.Caption to change the caption, and the column.Header.VisiblePosition to change the order of the columns.
Mike Saltzman"]The Order of the columns is determined by the BindingManager. I beleive it gets these from the ITypedList interface's GetProperties method. This will essentially be random unless you implement this interface yourself.
I am using BindingList<T> of my custom class. On the custom class, do I need to implement some interface to control the column order. What are the other options.
No, the custom class in this case has nothing to do with it, it's the list that determines the order of the columns. You would have to implement ITypedList on your list, which means you can't just use a BindingList<T>, you would have to create your own list class- perhaps derived from BindingList<T> and implement ITypedList.
Personally, I think it's a lot simpler to just re-order the columns in the grid using the column.Header.Position property.
If you set the grid's DataSource, then yes - you should see the schema in the grid immediately. This is true whether you implement ITypedList or not. One has nothing to do with the other.
You might want to search the forums for know issues when using multiple threads with the grid, though. Getting your data on another thread can be a pretty dangerous and tricky thing to do and you have to be extremely careful.
Hi Mike,
Mike Saltzman"]You would have to implement ITypedList on your list, which means you can't just use a BindingList<T>, you would have to create your own list class- perhaps derived from BindingList<T> and implement ITypedList.
I have related query, I am implementing the ITypeList. Doesn't this mean the schema of the DataSource is available immediately. And I should be able to see the columns as soon I set the datasource. I am fetching the data on separate thread.
Mike Saltzman"] No, the custom class in this case has nothing to do with it, it's the list that determines the order of the columns. You would have to implement ITypedList on your list, which means you can't just use a BindingList<T>, you would have to create your own list class- perhaps derived from BindingList<T> and implement ITypedList. Personally, I think it's a lot simpler to just re-order the columns in the grid using the column.Header.Position property.
Thanks. I found similar implementation in the some sample code. As you mentioned in the earlier reply for this post. Doing it here means I don't have repeat it on all the grids which use my custom class as data source. Also it allows me to remain loosely coupled with the presenter.