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.
I am binding two columns named object_name and object_id in my grid and changing the column captions at runtime as Object Name and Ibject Id.can i change the property of the columns on the basis of their captions and not on the name through which they are binded in the grid ?
Thanks,
M
It seems I understand the question, but I'm not completely sure. Let's try ...
Suposing you have bound the grid to a custom data source (e.g a collection which implements either IList or IBindList), changing a property is automatically performed after a cell edition. This means you can change a property by basically two ways:
Via the column caption, I think this is what you want, or accessing the property directly.
C# (by custom column caption)
aRow.Cells["Object Name"].Value = "newName"; //perfoms a change into object_name property
aRow.Cells["Object Id"].Value = 123; //performs a change into object_id property
C# ( by accessing the object and property)
CustomObject obj = (CustomObject)aRow.ListObject;
obj.object_name = "newName";
obj.object_id = 123;
Has your question been answered?
I don't understand the question.