How to change cell font in a DataGridView control?

My task is to display a number of fonts in a list. This list will be scrollable, and each font name will be shown in its own font face.

On my form, I have two DataGridView controls. The first DataGridView gets a list of all system fonts and their filenames (well, just the font names for now, as I cannot find how to display the filenames! - if anyone knows, that would help!) and the second DataGridView displays the list of font names again, but formatted to display in their own typeface.

Well, that's the intention.

Here is some of my code:

----------

Dim i As FontFamily
For Each i In System.Drawing.FontFamily.Families

' Data Grid
Dim row As String() = {i.Name, i.Name}
DataGridView1.Rows.Add(row)

' Data Grid with font preview
Dim r As Integer
Dim c As String() = {i.Name}
r = DataGridView2.Rows.Add(c)
Try
*** DataGridView2.Cell.Font = New System.Drawing.Font(i, 10)
Catch
' Do nothing
End Try

Next i

----------

This is very simple test code that I quickly put together for this post. The real code is too long to post here.

The question is - how do I change the font of a cell (Marked above with ***) Obviously some co-ordinate must be specified... but I cannot find the appropriate properties.

If anyone can point me in the right direction, I would appreciate it :)


Answer this question

How to change cell font in a DataGridView control?

  • Eamon Nerbonne

    Hi there

    I didn't think you understood what I was trying to do - on my first read of your post, I thought you were referring to the control's properties - which change the font for the whole control.

    I had actually spent about 5 hours Googling and searching before posting here, and after finding no other solution, I managed to get part-way there by using a ListBox with DrawMode set to OwnerDrawFixed. I then used the following code to alter the font of each item in the ListBox:

    ----------

    Private Sub ListBox2_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox2.DrawItem

    e.DrawBackground()

    Dim myBrush As Brush
    Dim myBackColor As Color = Color.White
    Dim myForeColor As Color = Color.Black
    Dim myFont As Font = Me.Font

    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
    myBackColor = Color.CornflowerBlue
    myForeColor = Color.White
    End If

    If e.Index > 5 Then
    myFont = New Font("Arial", 10, FontStyle.Regular)
    myForeColor = Color.Maroon
    End If

    myBrush = New SolidBrush(myBackColor)
    e.Graphics.FillRectangle(myBrush, e.Bounds)
    myBrush = New SolidBrush(myForeColor)
    e.Graphics.DrawString(ListBox2.Items(e.Index), _
    myFont, myBrush, New RectangleF(e.Bounds.X, _
    e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
    e.DrawFocusRectangle()

    End Sub

    ----------

    I found that code at the following link: http://www.ramanamurthy.com/loan/2005/10/changing-colour-of-listbox-items-in.html

    As you can see, I had made a few simple changes to test if the method would work - which it does. My next step was going to be to set up an array and use that to store a list of the fonts, and then have the code above check that array obtain the correct index from the array and apply the appropriate font. After that, I was going to see if I could translate the method across to the DataGridView control - and if not, stick with a ListBox.

    I'm glad that instead, I checked back here to post my solution. When I read your post, I thought maybe you didn't understand what I was trying to do (quite often, people will read a post and say something very obvious, and also say "look at the Help", so I was initially a little dismissive )

    Anyway, I then figured I hadn't seen a Style property for the DataGridView itself, so I investigated. It only took me a few minutes to find that the method of addressing individual cells that I had missed was via Item(x, y) syntax... after which I could find the properties you mentioned.

    It was plain sailing from there.

    My code is now:

    DataGridView1.Item(1, index).Style.Font = New Font(i.Name, 10, FontStyle.Regular)

    This is to replace the line marked with *** in the code in my first post.

    I have a Try... Catch shell around the line because if I don't, I get errors about certain fonts not supporting a Regular style. Not sure how to overcome that one yet (any ideas ). Ignoring errors is certainly not a solution, but it helps me get past that bit for now.

    So, I now have two methods by which I can accomplish what I want - although yours is of course the method I want to use. Much nicer. However, the alternate method means I also know how to do this to ListBoxes now, and I learnt a little more about .NET in the process.

    I haven't properly used VB since VB4 until yesterday (only had minor fiddles with 5 and 6), so I'm finding there is a lot to learn afresh.

    Thanks for all your help

    Dan

  • David Szarenski

    Forgot to mention, this is with Visual Basic 2005, as a part of Visual Studio.

    Surely someone knows how to change the font of a cell I can't believe something that should be so simple is still eluding me!

  • RCD

    That sounds like a sensible way to go

    I have Visual Studio 2005, but my local help files don't seem all that great. I find the online ones are better, but I don't always know what to look for. Which reminds me, I have to find that Object Browser thing soon...

    Ok, the final unanswered thing from this thread (although the main thrust has been dealt with) is how to find the filename of an installed system font If you could point me in the right direction for that, that would be great... it doesn't seem to be part of the FontFamily object

  • codeiki

    thanks it's useful

    solved my problem


    with Best Regards
    M.S. Babaei


  • Jhan Zaib

    No problem. The 'help' thing is thrown around a lot because, well, it's in the Help . However, you actually need to read the help in quite a bit of detail - you will suffer a lot of going round in circles, and blind alleys, but you'll find it eventually. Specifically, the DataGridView is a rather complex control - you aren't going to be able to fully utilize it in just a couple of hours of reading (or even a few days of playing around with it).

    The more you read the help (I'm using VB2005 hand the MSDN2 help library is far superior, I think, than the origional one). I can't think of a time that I don't have help open! The more you read it, the easier it becomes to find things and you learn a whole lot about the .NET framework.

    Regarding your additional question, a Try Catch will work, but obviously, won't set your font. What you could do is create a FontFamily from the font name and check the IsStyleAvailable for the style you want. I would suspect that if Regular style is not available (which is the case for some fonts), then Italic would be, and visa versa.



  • Chikku

    Kinda what I figured... seeing as the filename doesn't seem to be a part of the object.

    It looks like I can make my own font "collection" - which can consist of fonts that have not been installed. Well, that's how I read it. I haven't tried yet.

    So, I guess I'd look through the C:\Windows\Fonts folder (I guess C:\Windows will be some environment variable such as $SystemPath$ in installers etc.) and add them to a custom collection, and then go from there... or something...

    Anyway, thanks once more for the help

  • n3hgn

    Try changing the cells Style.Font property.

    As ReneeC always says - The Object Browser is your friend . My favorate is the F1 key. I keep a few spare, because without it I'd be lost !



  • PGamblin

    Not sure how to get the filename from the font - and possibly it may not be so simple, because the font filename could be called anything. I'm not sure how fonts are 'registered' with the system.

    You could enumerate though all the fonts in the Fonts folder, and use some windows API (I'm guessing) to get the font properties in that file, but I have never done something like that.



  • How to change cell font in a DataGridView control?