Hello.
I've a typed list (lets say employees) binded to an UltraGrid. The employee has a property of type Human (for example). Human has properties: Name, LastName and Document. How can I show columns for those three properties in my grid? There is no need of auto generating, I can add them by code or even in the designer. (Would rather by code at run-time).
Thanks,
Diego
dpaska90 said:I'd like to mark you (for the future viewers) that in the InitializeRow event the if are backwards. You should be asking e.Row.Cells.Exists("ColumnKey"), and not its denial.
Oops, sorry about that. I fixed it, now.
Thanks! I went through the simpler way.
I'd like to mark you (for the future viewers) that in the InitializeRow event the if are backwards. You should be asking e.Row.Cells.Exists("ColumnKey"), and not its denial.
Hi Diego,
There are a number of approaches you can take. If you are going to be binding the same objects to multiple controls, then it might be worthwhile for you to implement ITypedList on your typed list and return a custom PropertyDescriptor for each field you want to display. This is pretty complicated if you aren't familiar with how ITypedList and the PropertyDescriptor object work, but the advantage is that you will be able to bind your list to any control (not just WinGrid) and all of the properties will display the same.
If you are just binding to the grid and you want to do things the simpler way, then the easiest thing to do is to handle the InitializeLayout event of the grid and add some unbound columns to the root band. You will probably want to hide the "Human" column from the user, also. So the code would look something like this:
private void ultraGrid1_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { UltraGridLayout layout = e.Layout; UltraGridBand band = layout.Bands[0]; if (false == band.Columns.Exists("Name")) { UltraGridColumn column = band.Columns.Add("Name"); column.DataType = typeof(string); } if (false == band.Columns.Exists("LastName")) { UltraGridColumn column = band.Columns.Add("LastName"); column.DataType = typeof(string); } if (false == band.Columns.Exists("Document")) { UltraGridColumn column = band.Columns.Add("Document"); column.DataType = typeof(string); } band.Columns["Human"].Hidden = true; }
Then you would use the InitializeRow even to populate these unbound columns with data from the "Human" column.
private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) { if (false == e.ReInitialize) { Human human = e.Row.Cells["Human"].Value as Human; if (e.Row.Cells.Exists("Name")) { e.Row.Cells["Name"].Value = human.Name; } if (e.Row.Cells.Exists("LastName")) { e.Row.Cells["Name"].Value = human.LastName; } if (e.Row.Cells.Exists("Document")) { e.Row.Cells["Name"].Value = human.Document; } } }
If these fields are read-only, then you will add code to InitializeLayout to set the Activation property on each column to ActivateOnly, NoEdit, or Disabled.
If you need the user to be able to write to these fields, you can use the BeforeCellUpdate or BeforeRowUpdate event to read the value from the unbound cell and update the value(s) in the Human column.