Hi
Consider i have a student class like following
class Student{
public string Name {get; set;}
public int StudentID {get; set;}
public bool IsActive {get; set;}
}
Now i will be creating a datatable where some of the columns are of Student datatype. When i bind this to grid, i provide user with option to select what value out of these three properties he would like to see in grid, which will be editable too.
I am not able to find a way where i show a single property out of this custom object in a cell, and to update back it in case of modification.
I tried using FormatProvider for cell and able to show property selected by user, but not able to update the property back when edited. Rather i am getting exception related to datatype.
Please guide me to implement this functionality in the grid
Hi,
The grid doesn't know how to edit your custom objects, so there's nothing built-in to the grid to handle a case like this. But there are a couple of approaches you could take here.
In order to guide you further, though, I need a little clarification on your requirements. When you say the user can choose which of these values he will see in the grid, does that mean the user chooses one for the whole column and every cell in that column displays the same one? Or do you mean that the user might choose to see Name in one row and StudentId in some other row in the same column?
If it's the latter, then you will need to store that information somewhere - perhaps in another field on the Student class itself or in another column in the grid.
Hi Mike,
I am working with Aatish on the same problem.
When user choose any value, all the cells in the Grid should show the same value. Lets take user selects name, then the entire Grid should show Names.
Need your guidance.
Thanks,Rajesh
Okay, that will make things easier.
What you could do is design a control of your own that knows how to handle your custom type and also has the ability to show and edit one of the three properties. You could create a derived TextBox control, for example, and add a couple of properties to it - one for the Value (your custom type) and another property which is an enum indicating which property to display.
Then you could embed this control in the grid using the UltraControlContainerEditor component.
When the property to display changes, you would set the property on each of the instances of your custom editor control (you would need at least w: one for display and one for editing) and then just refresh the grid using grid.Rows.Refresh().
There are sample projects included with NetAdvantage that demonstrate how to use the ControlContainerEditor, but if you get stuck, let me know and I will try to point you in the right direction.
Another option, which is less elegant, but might be a bit simpler would be to hide the actual grid column that contains the data and add an unbound column to the grid of type string that the user can see. Then you could handle populating the text of the unbound column in the grid's InitializeRow event and you could populate it with whichever property you want based on the current user setting.
Again, when the setting changes, you call grid.Rows.Refresh(FireInitializeRow) to fore the grid to re-fire the event for each row and this re-copy the data.
Similarly, when the user updates the unbound field, you could use the AfterCellUpdate event to copy the new data into the custom object.