We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Alignment of negative values

If the format of a data bound grid control is set to display negative values (in Static cells) as "#,0;(#,0)", and horizontal alignment is Right, it seems that negative values do not exactly line up with positive values in the same column. That is, the least significant digit of each value is not aligned within a column. It looks like the ")" interferes with correct alignment. Excel seems to handle this properly (or at least what I consider properly). Is there a way to make the data bound grid behave like Excel in this regard? -Peter

3 Replies

PZ Peter Zaborski September 16, 2003 04:00 PM

Actually, I just noticed that even if the format string is set to "#,0;-#,0", it still fails to align the + and - values in a column, but the offset is much smaller than with "()" for - values.


AD Administrator Syncfusion Team September 16, 2003 05:19 PM

The minus problem is in .NET. If you line up several labels vertically, align them right, and put + and - values in them, the text does not line up in the labels either. I think this has been fixed in .NET 1.1. The paren problem is something else though. I think you can work around it with code like in a DrawCell event handler:
private void gridDataBoundGrid1_DrawCell(object sender, GridDrawCellEventArgs e)
{
	if(e.Style.CellValueType == typeof(double))
	{
		string s = e.Style.CellValue.ToString();
		if(s.Length != 0)
		{
			double d = double.Parse(s);
			if(d >= 0 )
			{
				int sizeParen = (int)  e.Graphics.MeasureString(" ", e.Style.GdipFont).Width;
			e.Style.TextMargins.Right += sizeParen;
		         }
	        }
        }
}


PZ Peter Zaborski September 17, 2003 02:32 PM

Clay, thanks for the response. Unfortunately, your suggestion does not work. I also tried const string testValue = "100,000.00"; int sizePos = (int) e.Graphics.MeasureString(testValue, e.Style.GdipFont).Width; int sizeNeg = (int) e.Graphics.MeasureString("(" + testValue + ")", e.Style.GdipFont).Width; sizeParen = (sizeNeg - sizePos) / 2; and that gets it closer but it's still off (by maybe 1 pixel). I'm not sure what XL uses to get it right no matter what the font, value, etc. FWIW, I use v1.1 of the framework and the problem with the "-" sign is still there. I tried your idea of vertically aligned labels and the text does not align properly. -Peter

Loader.
Live Chat Icon For mobile
Up arrow icon