I have a grid, DataBound with a DataSet/DataTable, with a number of columns in which I would like to make one column use a font family of "Consolas" because it puts a slash through zeroes. The column in question holds accounts with sometime strange values like A0O7382OU000. As you can see, discerning the zero from the letter O is not so obvious for our users. I went to grid InitializeLayout event and wrote the following expecting the column to format with the font.
grid1.DisplayLayout.Bands[0].Columns["ColumnA"].CellAppearance.FontData.Name = "Consolas"; Sadly this does not work. Then just to test the reality of this I tried the below code to see if I could make ANY change to the cell from this area and found that none of the following lines had any effect on the output of text. grid1.DisplayLayout.Bands[0].Columns["ColumnA"].CellAppearance.FontData.Bold = DefaultableBoolean.True;grid1.DisplayLayout.Bands[0].Columns["ColumnA"].CellAppearance.BackColor = Color.Plum;grid1.DisplayLayout.Bands[0].Columns["ColumnA"].CellAppearance.ForeColor = Color.Red;
None of these had any effect on the grid, but stepping through code I could see I was setting the CellAppearance properties. Obviously, these have nothing to do with the grid's actual display. So where am I going wrong?
Testing, I found that this works, but the font is ugly for anything but a number field. Since it modifies everything in the grid, I cannot use it as a solution. grid1.Font = new Font("Consolas", 9);
So how do you get to the Appearance to change the font family?
Hi,
The code you have here should work. There are two reasons I can think of why it might not:
1) Something else in your code is setting, or resetting the CellAppearance on the column after this code is executed. This should be very easy to detect. Simply check the "grid1.DisplayLayout.Bands[0].Columns["ColumnA"].CellAppearance.FontData.Name" at run-time (maybe in a button click) and see if it is still set to what you set it to.
If it's not, then something overwrote that property. Maybe you are setting this property somewhere else in your code. Maybe you are calling grid.DisplayLayout.Load and the layout you are loading is overwriting it.
2) If the property IS still set to the correct value and it's still not working, then the only other explanation is that something is overriding it. Maybe you are loading a style library into your application (StyleManager.Load). Or maybe you are applying an appearance to the Cell (which would override the column's more general setting).
I tried the suggestion to see what the values were after they were set. It turns out they were still set when I was viewing the screen. Well as it turns out, my problem was solved by pondering your #2 answer. Next, I tried changing the font of another column and low and behold it changed immediately. After recognizing that the column I was trying to change was a dynamically created column, in essence, I had the wrong column name.
If you recall an older post of mine trying to work out how to data mask account numbers, you told me I could add a new column and hide the original column. That is what I did in a class extension of the grid. (I completely forgot that I was not dealing with the proper column name.) This of course simplifies our font change because all the grids that mask numbers inherit from a single class that we modified. A quick change there and over 100 instances of the masked data were all fixed to the font that we desired... a small change in font size and Voila!, we're 90% through this edit.
Thanks for pointing out what should have been obvious. You were spot on.