none
Changing the font in dataGridView for specific cells.

    Question

  • I have read the MSDN library about how to format the fonts and cell styles for all the rows and columns of a dataGridView.

    What I need to be able to do is change the Font of a specific cell, or I could even get by with changing a whole row.

    What I can't understand (and this might be a result of me being tired) is how you change the Font property for one row, then use the default Font properties for the remaining rows.

    Basically, what I need to do is some of my cells are simply "headers" indicating a section in the data, and I need that cell (or row) to be in bold. Then, of course, return to the default properties so the rest of my data is in regular font.

    Wednesday, June 28, 2006 12:55 AM

Answers

  • This is how I'm doing it...

    DataGridViewCellStyle style = new DataGridViewCellStyle();

    style.Font = new Font(dataGridView.Font, FontStyle.Bold);

    dataGridView.Rows[0].DefaultCellStyle = style;

     

    Then I can just pick any row I want ( Rows[#] )and have it displayed the way I want.

    Thanks,

    MSJ

    Wednesday, June 28, 2006 6:21 PM

All replies

  • DataGridViewCellStyle style = new DataGridViewCellStyle();
    style.Font =
    new Font(DataGridView1.Font.FontFamily, ListMessages.Font.Size, FontStyle.Bold);
    foreach (DataGridViewRow row in DataGridView1.Rows)
    {
    if (!(bool)row.Cells["IsRead"].Value)
    {
    foreach (DataGridViewCell cell in row.Cells)
    cell.Style.ApplyStyle(style);
    }
    }

    I guess this is what you're looking for. In this example a row (you can see it's cell based) is put in bold in case the IsRead-column value is set to false.
    Basically you declare a style you like and bind it to specific cells.

    Wednesday, June 28, 2006 7:00 AM
  • The problem is I have an unbound dataGridView, so none of my columns or rows are named. My dataGridView has no column or row headers, simply cells.

    What I have is typical
     string[] row0 = {"some string, "some string", someValue}
    string[] row1 = ...
    etc..

    dataGridView.Rows.Add(row0);
    dataGridView.Rows.Add(row1);
    etc...

    I just need a way of setting row0 to bold, then row1 back to the default font parameters. The only way I can using your method is to search the contents of the cell and then set it to bold if I find what I want. There must be a way of picking a row, and setting it. Seems you can do it with columns easily enough.

    Wednesday, June 28, 2006 5:11 PM
  • This is how I'm doing it...

    DataGridViewCellStyle style = new DataGridViewCellStyle();

    style.Font = new Font(dataGridView.Font, FontStyle.Bold);

    dataGridView.Rows[0].DefaultCellStyle = style;

     

    Then I can just pick any row I want ( Rows[#] )and have it displayed the way I want.

    Thanks,

    MSJ

    Wednesday, June 28, 2006 6:21 PM
  • Hey Friends, Am pretty new to C# though I don't want that to discourage me. I have a situation where am using DataGridView and am having a column in my database table namely msg_status which has either a value of 0 or 1. The DataGridView should make the rows bold if msg_status value is 1 and rest of the rows with value 0 of msg_status should be normal. With that, I don't want msg_status to appear in DataGridView. My friends, I've searched a lot on internet and came up with these two ideas people had in mind about how to implement...here http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=63866 but it doesn't help in how to make connection with the database and a lot of unanswered issues. Please help with this, thank you in advance.
    Thursday, December 31, 2009 11:44 AM
  • there is a simple way to do this

    DataGridV.Rows(DataGridV.Rows.Count - 1).DefaultCellStyle.Font = New Font(DataGridV.DefaultCellStyle.Font, FontStyle.Bold)


    • Edited by Munibpk Saturday, October 20, 2012 7:58 AM
    • Proposed as answer by azemoten Wednesday, July 09, 2014 5:38 PM
    Saturday, October 20, 2012 7:53 AM