Hi all,
I'm using a custom type for cells values of the grid (say CustomCell). When the grid is rendered it calls the CustomCell.ToString() method. So, the only thing I need is to override the ToString method of the CustomCell class. The CustomCell objects represent only numeric values.
Now we have a requirement to show some cells of the same column in a currency format ("$nnn.nn") and some cells still in a "regular" format. That would be logically to set for some cells the Currency style. However, this will not work, because I'm not using numeric values as cells values.
Is there a simple way some cells of the same column can be in the currency format while other ones in "regular"? I would like to change the CustomCell only by implementing IFormattable.
Thanks,
Vitaly
There's really no simple way to format different cells in the same column with different formats.
One way you could do this is with a DrawFilter - this would allow you to draw the text in the cell yourself. I assume the cell is not updatable by the user.
Another optoion would be to use a DataFilter. The advantage to this approach is that you don't have to get involved in drawing text with GDI+. The disadvantage is that you need to convert the text both ways - formatting it and then unformatting it.
Still another option would be to use a CreationFilter and change the text of the TextUIElement in the cell.
And still another option would be to hide the real bound column in the grid, add an Unbound column, and then use the InitializeRow event to populate the unbound column with the formatted text you want, based on the value of the bound column in the same row. This last approach is probably the easiest.
Has anyone placed a feature request for this functionality? Specifically something like a Format property at the cell level? If not can I request it now?
I have a similar situation with a variables UltraGrid. Depending on the variable type, the value can be a date, amount, number etc. I started down the DataFilter approach but none of these solutions are ideal.
This seems like a rather common, reasonable request. It is great that we can control various aspects at cell level like so many appearence settings. Personally though I am more interested in functionality that helps with working with the data than having UX eye candy.
hudgeo said:Has anyone placed a feature request for this functionality? Specifically something like a Format property at the cell level? If not can I request it now?
You are certainly welcome to Submit a feature request to Infragistics.
Okay.. here's a way to do it without using DataFilters:
The Format property is on the grid column, but it's ultimately handled by the editor in the cell, not by the grid. So the editor gets the format from the grid via the editor owner. You can insert a default editor owner onto your editor and thus assign a different format to each cell in a column.
The first thing you need to do is define a new class which derives from Infragistics.Win.UltraWinEditors.DefaultEditorOwner and overrides the GetFormatInfo method. Something like this:
public class MyOwner : Infragistics.Win.UltraWinEditors.DefaultEditorOwner { private string format; public MyOwner(string format) { this.format = format; } public override void GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) { base.GetFormatInfo(ownerContext, out format, out provider); format = this.format; } }
As you can see, this class takes a format in the constructor and then uses that format in the GetFormatInfo method - overriding whatever the base class does (which is probably nothing).
Then you handle the InitializeRow event of the grid and assign an editor to the cells using this new owner class. Something like this:
private void ultraGrid1_InitializeRow(object sender, InitializeRowEventArgs e) { int x = (int)e.Row.GetCellValue(e.Row.Band.Columns["Int32 1"]); MyOwner myOwner; EditorWithText editor; switch (x) { case 1: myOwner = new MyOwner("d4"); break; case 2: myOwner = new MyOwner("e"); break; case 3: default: myOwner = new MyOwner("c"); break; } editor = new EditorWithText(myOwner); e.Row.Cells["double 1"].Editor = editor; }
This code examines the value in the "Int32 1" column and based on that value, it creates a new instance of MyOwner with a different format. Then it creates a new EditorWithText using the owner just created and assigns the editor to the "double 1" cell in the same row.
This is just a quick and dirty example, and it is, of course, horribly inefficient, because it will end up creating an editor and an owner for every cell. A better way to do it would be to re-use the same editor and the same owner for each cell where the format is the same string. But this demonstrates the basic idea, and I think this method is a bit cleaner and easier than using a DataFilter.
This looked like it would do the trick for us: we are representing multi-dimensional data such that in one column, we need some rows that display with no decimal data, and some with decimal parts displayed.
We're using UltraWinGrid from 8.2.20082.1000 (about to upgrade to 8.3.????), and I can't get it to work, though it throws no exceptions. I am using the shared editor and owner across all cells I am overriding.
Specific Questions:
1 - Do I need to instantiate a control for the editor in each cell I am overiding the column setting? I get the same result whether I do or don't, but noticed that during the GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) call, that deep inside the ownerContext object, it has recorded trapped exceptions if I do not create a control they are populated and look fine if I do create a control. (in ownerContext.base.Cell.Editor:
.IsValid = '((Infragistics.Win.UltraWinGrid.CellUIElementBase)(ownerContext)).Cell.Editor.IsValid' threw an exception of type 'System.ArgumentException'
.SelectedText = '((Infragistics.Win.UltraWinGrid.CellUIElementBase)(ownerContext)).Cell.Editor.SelectedText' threw an exception of type 'System.InvalidOperationException'
2 - Is there a different set of objects for rendering vs editing?
3 - Where are the various 'Format Strings' and 'Mask Strings' documented - I can't help but wonder if it's not working just because I've been guessing wrong about those strings.
Thanks!
rlsnyder said:1 - Do I need to instantiate a control for the editor in each cell I am overiding the column setting? I get the same result whether I do or don't, but noticed that during the GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) call, that deep inside the ownerContext object, it has recorded trapped exceptions if I do not create a control they are populated and look fine if I do create a control. (in ownerContext.base.Cell.Editor: .IsValid = '((Infragistics.Win.UltraWinGrid.CellUIElementBase)(ownerContext)).Cell.Editor.IsValid' threw an exception of type 'System.ArgumentException'
I'm not sure what you mean here. You don't need to create a control, but you do need to create an editor or each cell, or at least a different editor for each possible format. In my case, I used an EditorWithText, which is essentially the editor used by the UltraTextEditor and is used by the grid for most text-based cells.
rlsnyder said:2 - Is there a different set of objects for rendering vs editing?
The editor both renders and edits.
rlsnyder said:3 - Where are the various 'Format Strings' and 'Mask Strings' documented - I can't help but wonder if it's not working just because I've been guessing wrong about those strings.
The format is applied by calling the ToString method of the cell's value and passing in the format. So it's nothing to do with the grid or any of the Infragistics assemblies, it's handled entirely by the DotNet Framework. So to see the supported formats, you need to look at the Microsoft documentation on the ToString method of the particular data type you are using.