locked
How To Programatically Set A DataGridView Cell Text To Bold RRS feed

  • Question

  • Hello community,

    Thank you for help with this...

    I am trying to find the correct syntax to set a referenced data grid view cells text to bold.
    Or failing that a referenced rows text to bold

    I was trying to using the current row and cell values as a reference of which cells set.
    I Cant seem to find the correct command to get it to work however. DefaultCellyStyle appears not to be correct, or I'm not implementing it correctly.

    I was trying something along the lines of pseudo code...

    DataGridView.CurrentCell.DefaultCellStyle.Font.Bold = true

    Or failing that the whole rows text set to bold would do at a push...

    DataGridView.CurrentRow.DefaultCellStyle.Font.Bold = True

    Any help would be appreciated.

    Friday, August 25, 2017 4:49 AM

Answers

  • Hello community,

    Thank you for help with this...

    I am trying to find the correct syntax to set a referenced data grid view cells text to bold.
    Or failing that a referenced rows text to bold

    I was trying to using the current row and cell values as a reference of which cells set.
    I Cant seem to find the correct command to get it to work however. DefaultCellyStyle appears not to be correct, or I'm not implementing it correctly.

    I was trying something along the lines of pseudo code...

    DataGridView.CurrentCell.DefaultCellStyle.Font.Bold = true

    Or failing that the whole rows text set to bold would do at a push...

    DataGridView.CurrentRow.DefaultCellStyle.Font.Bold = True

    Any help would be appreciated.

    It sounds like you want to be specific for a particular column (or group of columns) to have a specific format.

    For that, look at the .Column properties:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn_properties(v=vs.110).aspx

    In that list you'll see the .DefaultCellStyle:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.defaultcellstyle(v=vs.110).aspx

    Keep in mind that's the cell style for this particular column. As an example (an excerpt):

    With .Columns(1)
        .Width = 210
        
        With .DefaultCellStyle
            .WrapMode = DataGridViewTriState.False
            .Alignment = DataGridViewContentAlignment.MiddleLeft
            .Font = New Font("Tahoma", 11, FontStyle.Bold)
        End With
    End With

    ***** EDIT *****

    You can also access a given row's default cell style properties:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsdefaultcellstyle(v=vs.110).aspx

    and for that matter, to get even more granular, even refine it to a particular cell's default style:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Friday, August 25, 2017 10:31 PM ... added links to MSDN documentation
    • Marked as answer by AussieHack Saturday, August 26, 2017 3:54 AM
    Friday, August 25, 2017 10:10 PM
  • Again Tyvm who replied. The linked information supplied the solution and more...

    Example to change a particular cells text to bold...

    DataGridView.Rows([RowIndex]).Cells([CellIndex]).Style.Font = New Font(DataGridView.Font, FontStyle.Bold)

    Example to change text color red...

    DataGridView.Rows([RowIndex]).Cells([CellIndex]).Style.Font = Color.Red

    So to do what I initially asked, I can setup a 'For-Next' to reference all cells in a particular row and apply the statement to each, should get the job done...

    For CountAllCellsInRow = 0 to DataGridView.ColumnCount-1
    DataGridView.Rows([RowIndex]).Cells(CountAllCellsInRow).Style.Font = New Font(DataGridView.Font, FontStyle.Bold)
    Next



    • Marked as answer by AussieHack Saturday, August 26, 2017 3:54 AM
    • Edited by AussieHack Saturday, August 26, 2017 4:02 AM Added Image
    Saturday, August 26, 2017 3:53 AM

All replies

  • Hi AussieHack,

    Based on your description, you want to bold the text for datagridview, I think you need to ues the following code to set.

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
                   DataGridView1.ColumnCount = 5
            DataGridView1.Columns(0).Name = "Column1"
            DataGridView1.Columns(1).Name = "Column2"
            DataGridView1.Columns(2).Name = "Column3"
            DataGridView1.Columns(3).Name = "Column4"
            DataGridView1.Columns(4).Name = "Column5"
            'DataGridView1.Columns(5).Name = "Column6"
    
            'some dummy test data
            With DataGridView1
                .Rows.Add(1, 2, Nothing, 4, Nothing, 6)
                .Rows.Add(11, 12, 13, Nothing, 15, 16)
                .Rows.Add(111, Nothing, 3, 4, 5, 6)
                .Rows.Add(1, 2, 3, Nothing, 5, 6)
                .Rows.Add(Nothing, 2, 3, 4, 5, Nothing)
            End With
    
            With DataGridView1.DefaultCellStyle
                '.BackColor = Color.Navy
                '.ForeColor = Color.White
                .Font = New Font(DataGridView1.Font, FontStyle.Bold)
            End With
    
        End Sub

    If you want to bold the specific column text

     With DataGridView1
                .Columns(3).DefaultCellStyle.Font = New Font(DataGridView1.Font, FontStyle.Bold)
            End With

    More detailed information, please refer to:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle.font%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, August 25, 2017 6:46 AM
  • Hello community,

    Thank you for help with this...

    I am trying to find the correct syntax to set a referenced data grid view cells text to bold.
    Or failing that a referenced rows text to bold

    I was trying to using the current row and cell values as a reference of which cells set.
    I Cant seem to find the correct command to get it to work however. DefaultCellyStyle appears not to be correct, or I'm not implementing it correctly.

    I was trying something along the lines of pseudo code...

    DataGridView.CurrentCell.DefaultCellStyle.Font.Bold = true

    Or failing that the whole rows text set to bold would do at a push...

    DataGridView.CurrentRow.DefaultCellStyle.Font.Bold = True

    Any help would be appreciated.

    It sounds like you want to be specific for a particular column (or group of columns) to have a specific format.

    For that, look at the .Column properties:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn_properties(v=vs.110).aspx

    In that list you'll see the .DefaultCellStyle:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcolumn.defaultcellstyle(v=vs.110).aspx

    Keep in mind that's the cell style for this particular column. As an example (an excerpt):

    With .Columns(1)
        .Width = 210
        
        With .DefaultCellStyle
            .WrapMode = DataGridViewTriState.False
            .Alignment = DataGridViewContentAlignment.MiddleLeft
            .Font = New Font("Tahoma", 11, FontStyle.Bold)
        End With
    End With

    ***** EDIT *****

    You can also access a given row's default cell style properties:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowsdefaultcellstyle(v=vs.110).aspx

    and for that matter, to get even more granular, even refine it to a particular cell's default style:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcellstyle(v=vs.110).aspx


    "A problem well stated is a problem half solved.” - Charles F. Kettering



    • Edited by Frank L. Smith Friday, August 25, 2017 10:31 PM ... added links to MSDN documentation
    • Marked as answer by AussieHack Saturday, August 26, 2017 3:54 AM
    Friday, August 25, 2017 10:10 PM
  • ty for reply, I appreciate your time.

    As I understand the DefaultCellStyle function setting to cover text as it is inserted into the DataGridView.
    I was more so trying to change the text already in the cell to bold to supersede or overwrite the DefaulCellStyle font value.


    To elaborate on the effect I am trying to achieve...
    I have one column in The DataGridView containing System.Date values relevant to that rows data.

    What I would like to do is display rows with a date less than today's date as the DefaultCellStyle text.
    But any rows with a date equal to or greater than today's date in bold text.

    The idea was to visually show the point where past dated rows end and future dated rows begin. Also I would like to be able to apply the effect after the data has been inserted into the DataGridView; to link it to a button event so the user can toggle it on or off at their discretion.

    Saturday, August 26, 2017 3:24 AM
  • Private Sub DGV_CellPainting(ByVal sender As Object, ByVal e As DataGridViewCellPaintingEventArgs)
    
            If e.RowIndex >= 0 And e.ColumnIndex >= 0 Then
    
                e.Handled = True
                e.PaintBackground(e.CellBounds, True)
    
                Dim sw As String = GetSearchWord(e.ColumnIndex)
                If Not String.IsNullOrEmpty(sw) Then
    
                    Dim val As String = DirectCast(e.FormattedValue, String)
    
                    Dim sindx As Integer = val.ToLower.IndexOf(sw.ToLower)
                    If sindx >= 0 Then
                        'the highlite rectangle
                        Dim hl_rect As New Rectangle()
                        hl_rect.Y = e.CellBounds.Y + 2
                        hl_rect.Height = e.CellBounds.Height - 5
    
                        'find the size of the text before the search word 
                        'and the size of the search word
                        Dim sBefore As String = val.Substring(0, sindx)
                        Dim sWord As String = val.Substring(sindx, sw.Length)
                        Dim s1 As Size = TextRenderer.MeasureText(e.Graphics, sBefore, e.CellStyle.Font, e.CellBounds.Size)
                        Dim s2 As Size = TextRenderer.MeasureText(e.Graphics, sWord, e.CellStyle.Font, e.CellBounds.Size)
    
                        'adjust the widths to make the highlite more accurate
                        If s1.Width > 5 Then
                            hl_rect.X = e.CellBounds.X + s1.Width - 5
                            hl_rect.Width = s2.Width - 6
                        Else
                            hl_rect.X = e.CellBounds.X + 2
                            hl_rect.Width = s2.Width - 6
                        End If
    
                        'use darker highlight when the row is selected
                        Dim hl_brush As SolidBrush
                        If ((e.State And DataGridViewElementStates.Selected) <> DataGridViewElementStates.None) Then
                            hl_brush = New SolidBrush(Color.DarkGoldenrod)
                        Else
                            hl_brush = New SolidBrush(Color.LightGoldenrodYellow)
                        End If
    
                        'paint the background behind the search word
                        e.Graphics.FillRectangle(hl_brush, hl_rect)
    
                        hl_brush.Dispose()
                    End If
                End If
    
                'paint the content as usual
                e.PaintContent(e.CellBounds)
            End If
        End Sub

    Hi Team,

    This May Help You

    Thanks,

    Saturday, August 26, 2017 3:40 AM
  • Again Tyvm who replied. The linked information supplied the solution and more...

    Example to change a particular cells text to bold...

    DataGridView.Rows([RowIndex]).Cells([CellIndex]).Style.Font = New Font(DataGridView.Font, FontStyle.Bold)

    Example to change text color red...

    DataGridView.Rows([RowIndex]).Cells([CellIndex]).Style.Font = Color.Red

    So to do what I initially asked, I can setup a 'For-Next' to reference all cells in a particular row and apply the statement to each, should get the job done...

    For CountAllCellsInRow = 0 to DataGridView.ColumnCount-1
    DataGridView.Rows([RowIndex]).Cells(CountAllCellsInRow).Style.Font = New Font(DataGridView.Font, FontStyle.Bold)
    Next



    • Marked as answer by AussieHack Saturday, August 26, 2017 3:54 AM
    • Edited by AussieHack Saturday, August 26, 2017 4:02 AM Added Image
    Saturday, August 26, 2017 3:53 AM
  • If you want to change the type to bold in a specific cell, this worked for me..

    dataGridView1.Rows[rowIndex].Cells[cellIndex].Style.Font = new Font("Segoe UI",9,FontStyle.Bold);



    • Edited by nadinCodeHat Wednesday, September 9, 2020 12:58 PM
    • Proposed as answer by nadinCodeHat Wednesday, September 9, 2020 1:00 PM
    • Unproposed as answer by KareninstructorMVP Wednesday, September 9, 2020 1:46 PM
    Wednesday, September 9, 2020 12:57 PM
  • If you want to change the type to bold in a specific cell, this worked for me..

    dataGridView1.Rows[rowIndex].Cells[cellIndex].Style.Font = new Font("Segoe UI",9,FontStyle.Bold);



    Please do not propose your reply as a answer and don't bold code.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, September 9, 2020 1:47 PM