Hi,
What is the best way (if there is any) to show pound signs instead of a number if a cell is not wide enough to display the whole formatted number? This is one of features in MS Excel. In order to do this, we need to be able to do at least the following tasks:
1) get the formatted number
2) get the font
3) get Graphics instance to call MeasureString on the formatted number with the font
4) get the width of the cell and it margins
Thanks,
Shaolin
Hi Shaolin,
Measuring the test yourself will be extremely difficult. But there might be an easier way. The grid and it's editors already keep track of whether or not the text fits inside the cell. They do this so the grid knows whether or not it's appropriate to show a tooltip.
So you could probably make use of that and then use a CreationFilter in order to change the Text of the cell to whatever you want without affecting the cell's value.
I decided to try to whip up a quick sample and it turned out to be surprisingly easy. My sample is attached here.
Hi Mike,
Thanks for your quick response and solution!
I tested your solution. Method IsTextFullyVisible is not always accurate.
Regards,
Under what circumstances is it not accurate? It seems to be working fine in my sample. If I resize a column, I can see it showing the text when the column is wide enough to fit the text and showing # signs when it's not.
I specified a text format of "#,##0.00" on the one of columns of double data type:
band.Columns[7].Format = "#,##0.00";
Even the column was wide enough, pound signs were still displayed for some cells in this column. When I hovered mouse over the cell, the cell is repainted and pound signs were changed to the real number. I could even see pound signs in Key and string columns, especially when I played with horizontal scroll bar.
I tried this out with Column 7 - which is the "Double 1" column. But I am still not seeing any problem. I tried scrolling the grid, resizing the column and every combination of those that I can think of. I am attaching the updated sample here.
I'm sure you are probably right. This was just a quick sample, so there are likely to be edge cases that the sample does not account for. But I need to be able to see the problem in order to help you figure out why it's not working. Can you tell me exactly how to reproduce the issue?
I played with the test app a little more and it is quite easy to reproduce what I reported. You press and hold down your mouse on the scroll box of the horizontal scroll bar and drag the scroll box to the far right and then slowly back to the left, all the strings and numbers will be displayed as pound signs as shown in the attached image.
Ah, okay, I see it now. I took a new approach and used a DrawFilter instead of a CreationFilter and it works correctly now.
If you are interested in why the first problem occurred, it's because the UIElements for the cell are verified when the column is initially visible. When you scroll, the position of the UIElements changes, but the contents don't, so the cell essentially doesn't get into the CreationFilter when scrolling back to the left and so it just keeps the # signs.
So the cell doesn't have to go through the CreationFilter, but it does have to re-paint, so a CreationFilter works okay.
There are two caveats to this approach, though.
First, you have to turn off ScrollWindow (via the UseScrollWindow property). This property optimizes the grid scrolling, but it prevents the cell from painting every time when scrolling. So there's a slight performance cost here.
Second, we have to wait for the TextUIElement to draw the text and then erase the text and draw the # signs. You might notice that in my sample, the Color cells don't work correct. This is because the TextUIElement in those cells has a transparent background. So if you are using colors in your grid, you would have to work around this by either hard-coding the color for the background drawing or else getting the resolved background color of the cell and using that.
Thanks so much for your response!
I did exactly you said. Actually I never used a CreationFilter for this feature in the first place because I thought using a DrawFilter would be more efficient. The following is the code snippet:
case DrawPhase.BeforeDrawForeground: var cellElement = drawParams.Element as CellUIElement; if (cellElement != null) { string text = cellElement.Cell.Text; if (!string.IsNullOrEmpty(text) && text.Length > 1) { Size size = TextRenderer.MeasureText(text, drawParams.Font); if (size.Width > drawParams.ElementDrawingRectInsideBorders.Width) { text = new string('#', text.Length - 1); if(cellElement.Column.CellDisplayStyle == CellDisplayStyle.FormattedText) drawParams.DrawString(drawParams.ElementDrawingRectInsideBorders, text, false, false); else { var textElement = (TextUIElement)cellElement.GetDescendant(typeof(TextUIElement)); if (textElement != null) textElement.Text = text; } return true; } } } break;
Best Regards,Shaolin
CellDisplayStyle sacrifices functionality for performance. So in this case, you would lose the ability to change the text on the element.
I think what happens in this case is that the CellUIElement draws the text, so you might be able to acheive what you want using a DrawFilter to draw the text yourself. But this would be much more difficult, since you would also have to measure the text yourself, instead of just letting the UIElement do it.
This method does not work if an UltraGridColumn's CellDisplayStyle is set to CellDisplayStyle.FormattedText. There is no TextUIElement created for a CellUIElement, which would result in a better performance. I could see a tooltip showing when text is not fully visible inside the cell. Could you please give me some suggestions please?
Your help is greatly appreciated!
The way it works is that the more specific appearance takes precedence over the more general appearance. So the appearance on the Cell (cell.Appearance) would apply the BackColor Yellow in your example.
There are appearances at many different levels in the grid, and many of the other Infragistics Winforms controls do the same thing.
The cell appearances can be affected by many different appearances: the cell, the Column, the Row, the Override (which is on the band and the layout). So this allows you to set appearances at a very broad level and also override those appearances for more specific objects within the larger set.
What is the background color for a cell if the background color of its row's Appearance is Red, Yellow of row's CellAppearance and Green of column's Appearance? What UltraGridCell's Appearance and how I can change it?
I am in a process to choose a Grid control for our projects. I might have asked improper questions. Thanks for your patience!