Hi,
I've a wingrid with a column, type numeric. In InitializeLayout-event I write 'NumCol.Format = "N0"'. But in some rows of this column I need 'Format="N2"'. Is this possible ?
I'm using Infragistics 7.3
Thanks
Hansjörg Wintermantel
The following code sample demonstrates how to use an EmbeddableEditorOwnerBase-derived class which overrides the GetFormatInfo method to format cells differently in every other row:
DefaultEditorOwnerSettings settings = new DefaultEditorOwnerSettings();MyDefaultEditorOwner owner = new MyDefaultEditorOwner();this.ultraGrid1.DisplayLayout.Bands[0].Columns["whatever"].Editor = new EditorWithText( owner );
public class MyDefaultEditorOwner : DefaultEditorOwner{ public override void GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) { provider = null;
CellUIElement cellElement = ownerContext as CellUIElement; UltraGridColumn column = cellElement.Column; UltraGridRow row = cellElement.Row;
if ( (row.Index % 2) == 0 ) format = "N0"; else format = "N2"; }}
I am using this method to format various rows in a column differently. It works except for one thing: PerformAutoResize does not work, it resizes the column as if there was no formatting. Note, I am ignoring your line "DefaultEditorOwnerSettings settings = new DefaultEditorOwnerSettings();" since the settings variable is not used.
Below is my code:
private void ugResults_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e) { try { int DefaultWidth = 100; MyDefaultEditorOwner owner = new MyDefaultEditorOwner(); if (e.Layout.Bands[0].Columns.Exists("Format")) { foreach (Infragistics.Win.UltraWinGrid.UltraGridColumn col in e.Layout.Bands[0].Columns) { if (col.Key == "Format") { col.Hidden = true; } else if (col.DataType.Name == "Decimal") { //row.Cells[col.Index].Editor = new EditorWithText(owner); col.Editor = new EditorWithText(owner); col.Width = DefaultWidth; } col.PerformAutoResize(PerformAutoSizeType.AllRowsInBand); } } } catch (Exception ex) { _mainform.HandleException(ex); } } } public class MyDefaultEditorOwner : DefaultEditorOwner { public override void GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) { format = ""; provider = null; try { CellUIElement cellElement = ownerContext as CellUIElement; if (cellElement.IsNull() == false) { UltraGridColumn column = cellElement.Column; UltraGridRow row = cellElement.Row; switch (row.Cells["Format"].Value.ToString()) { case "Money": format = "#,##0"; break; case "Decimal": format = "#,##0.0000"; break; case "Percent": format = "0.00%"; break; default: format = ""; break; } } else { format = ""; } } catch (Exception) { //eat } } }
Note: ignore the set width lines in the initialize code, that was just added after the failed autosizing.
it should be
if (col.Key == "Format") { col.Hidden = true; } else if (col.DataType.Name == "Decimal") { col.Editor = new EditorWithText(owner); } col.PerformAutoResize(PerformAutoSizeType.AllRowsInBand);
I tried this out and I get the same results. The Autosizing appears to be using the Format of the entire column, but ignoring the format on a cell when provided by an editor. This seems to be a bug. I'm going to ask Infragistics Developer Support so create a case for you and write this up for developer review so we can get it fixed.
One thing you could do in the mean time is apply a format to the column. This isn't perfect, but if you use the longest of your three possible formats, it should produce better results than what you have now - which is that the column is way too wide.
OK, this works. Thanks.
OK, I was away and just saw this. Will look at and try to understand (!) and give it a try. Thanks.
I just wanted to let you know that we've looked into this in detail, and it's not a bug in the grid, it's an error in your GetFormatInfo code. You are making the assumption that the ownerContext will always be a CellUIElement. This is an understandable assumption on your part, but it's not correct. The ownerContext will be a CellUIElement only in the case where the grid is painting the cell on the screen. For other cases, like AutoSizing, the context might be something else - an UltraGridRow.
So you need to account for both possibilities.
public class MyDefaultEditorOwner : DefaultEditorOwner { public override void GetFormatInfo(object ownerContext, out string format, out IFormatProvider provider) { provider = null; // NOTE: If you change default format to "N2" everything will behave properly. // If you change it to something like "N7" the autosize behaves as though it needs 7 spaces after the decimal point string defaultFormat = "N0"; UltraGridRow row = ownerContext as UltraGridRow; if (null == row) { UIElement element = ownerContext as UIElement; if (null != element) row = element.GetContext(typeof(UltraGridRow)) as UltraGridRow; } //if (cellElement != null) //{ // UltraGridColumn column = cellElement.Column; // UltraGridRow row = cellElement.Row; // if ((row.Index % 2) == 0) // format = "N0"; // else // format = "N2"; //} if (null != row) { UltraGridColumn column = row.Band.Columns[0]; if ((row.Index % 2) == 0) format = "N0"; else format = "N2"; } else { Debug.Fail("Failed to get an UltraGridRow from the ownerContext. So we may want to account for the different ownerContext type."); format = defaultFormat; } } }
Note that I also added a Debug.Fail here, so that if the ownerContext returns something else that you are not accounting for, it will raise a message to notify you.
Hi sunibla,
I have logged this behavior in our internal tracking system with a Development ID of 143042. The next step will be for a developer to review my investigation and confirm my findings or to offer a fix or other resolution.
Thanks. Note I did try applying a format to the column, and resize did work properly, however the column format over-rode the texteditor format and all the cells then have the column format.