none
How to change the backcolor, forecolor and font of all cells in a datagridview

    Question

  • I have a datagridview of 10000 rows and 250 columns

    i change the color and font of any specific cell like

      dg.Item(1, 1).Style.Font = New Font("Arial", 25)
      dg.Item(1, 1).Style.ForeColor = Color.GreenYellow

      dg.Item(2, 1).Style.Font = New Font("Arial", 15)
      dg.Item(2, 1).Style.ForeColor = Color.Red

    At one point of time

    at form_clik I want to reset complete cells to my own color and font

    Is there any quick way to do this? (without going to all cells)

    Regards

    Wednesday, March 15, 2017 6:05 AM

All replies

  • Yea set the column of the datagridview 

    There are endless possibilities

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


    Success
    Cor

    Wednesday, March 15, 2017 6:43 AM
  • I don't want iteration through cells or rows or columns.
    Wednesday, March 15, 2017 6:46 AM
  • I don't want iteration through cells or rows or columns.

    Why not? 

    Because you don't want to write 3 rows of code or because that you find 0,00000000001 microsecond to long?

    Be aware there is no need to iterate through rows and cells if you change the columnstyle



    Success
    Cor

    Wednesday, March 15, 2017 8:47 AM
  • Hi ackid50,

    You can write simply...
    dg.ForeColor = Color.White
    dg.RowsDefaultCellStyle.BackColor = Color.Black

    Regards,
    Ashidacchi
    Wednesday, March 15, 2017 8:48 AM
  • It takes some time to iterate 10000 rows. I'm searching for a quick method to do. Any idea?
    Wednesday, March 15, 2017 9:27 AM
  • No, it is not chage the cell style of cells which i set manually.
    Wednesday, March 15, 2017 9:28 AM
  • Hi,

    What do you mean by "set manually"?  Would you explain it.
    and what happens after an event of form-clicking with code I've shown.

    BTW, iteration of 10,000 times will not take much time, if screen updating is off while iterating. It should be ignored as Cor Ligthert wrote.
    • Edited by Ashidacchi Wednesday, March 15, 2017 10:02 AM
    Wednesday, March 15, 2017 9:58 AM
  • It takes some time to iterate 10000 rows. I'm searching for a quick method to do. Any idea?

    Hello,

    There is no quick method to iterate that many rows and columns. If at all possible you would be better off using paging for the DataGridView so that the number of cells to process makes it manageable. Not knowing all the requirements I can't say if this will fit into logic.

    In the majority of apps that many rows and columns would be considered unmanageable for any user to traverse even if this data were in say Excel which is good at managing formatting of cells.  


    Please remember to mark the replies as answers if they help and unmark 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.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, March 15, 2017 10:29 AM
    Moderator
  • I have a datagridview of 10000 rows and 250 columns

    I didn't manage to see this until I got coffee in me. You can use this event:

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

    That will only paint the cells which are then visible, but with that many (which isn't practical), it will be slow.


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

    Wednesday, March 15, 2017 11:08 AM
  • I have a datagridview of 10000 rows and 250 columns

    ackid,

    This has me curious:

    Is the DGV bound?

    I'm guessing that it's not?


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


    • Edited by Frank L. Smith Wednesday, March 15, 2017 11:56 AM ...formatting
    Wednesday, March 15, 2017 11:56 AM
  • It takes some time to iterate 10000 rows. I'm searching for a quick method to do. Any idea?

    If you change a column there is no iterations in Rows, what is your intention that you ask here.

    You told you don't want to iterate through columns. 10000 columns are real impossible to use in a datagridview. 

    I get the idea you are just sending fake messages to this forum. 



    Success
    Cor

    Wednesday, March 15, 2017 12:36 PM
  • I don't want iteration through cells or rows or columns.
    Create a new DataGridview, Inheriting the Default One and write your own code to handle MyDataGridview.ResetToDefaults
    Wednesday, March 15, 2017 5:28 PM
  • I have a datagridview of 10000 rows and 250 columns

    i change the color and font of any specific cell like

      dg.Item(1, 1).Style.Font = New Font("Arial", 25)
      dg.Item(1, 1).Style.ForeColor = Color.GreenYellow

      dg.Item(2, 1).Style.Font = New Font("Arial", 15)
      dg.Item(2, 1).Style.ForeColor = Color.Red

    At one point of time

    at form_clik I want to reset complete cells to my own color and font

    Is there any quick way to do this? (without going to all cells)

    Regards

    Hi

    Just my tuppenceworth

    dg.DefaultCellStyle.BackColor = Color.Beige
    dg.DefaultCellStyle.ForeColor = Color.Maroon
    don't know time overhead for 10000 rows by 250 columns, but all the largish DataGridViews I have ever used have negligible time delay doing this.


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, March 15, 2017 5:46 PM
    • Proposed as answer by Devon_Nullman Wednesday, March 15, 2017 11:40 PM
    Wednesday, March 15, 2017 5:45 PM
  • It takes some time to iterate 10000 rows. I'm searching for a quick method to do. Any idea?

    If you change a column there is no iterations in Rows, what is your intention that you ask here.

    You told you don't want to iterate through columns. 10000 columns are real impossible to use in a datagridview. 

    I get the idea you are just sending fake messages to this forum. 



    Success
    Cor

    See Below, oops


    Wednesday, March 15, 2017 10:23 PM
  • ...I cannot make 10000 columns at all...

    The OP said 10,000 rows and 250 columns.

    Not that that's any better - I'm not sure if it's even possible but if so, my thought was that he/she may be using the DGV as the data store (thus prompting my follow-up question).


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

    Wednesday, March 15, 2017 10:40 PM
  • ...I cannot make 10000 columns at all...

    The OP said 10,000 rows and 250 columns.

    Not that that's any better - I'm not sure if it's even possible but if so, my thought was that he/she may be using the DGV as the data store (thus prompting my follow-up question).


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

    I redid it, Form with two buttons and a DataGridView named DGV - I used Les's idea to reset. Using a temp filename likely slows things down but hey....

    Public Class Form1
        Dim R As New Random
        Dim sw As New Stopwatch
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            sw.Reset()
            sw.Start()
            Dim columns(249) As DataGridViewColumn
            For i As Integer = 0 To columns.Length - 1
                Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
                column.CellTemplate = New DataGridViewTextBoxCell()
                column.FillWeight = 1
                column.HeaderText = i.ToString
                columns(i) = column
            Next i
    
            DGV.SuspendLayout()
            DGV.Columns.AddRange(columns)
    
            Dim RowData(249) As String
            For rw = 0 To 9999
                RowData(0) = rw.ToString
                For cl = 1 To RowData.Length - 1
                    RowData(cl) = IO.Path.GetRandomFileName  'R.Next(100, 1000).ToString
                Next
                DGV.Rows.Add(RowData)
            Next
            DGV.ResumeLayout()
            sw.Stop()
            MessageBox.Show("Took " & sw.ElapsedMilliseconds.ToString & " ms to create 10000 rows and 250 Columns")
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            sw.Reset()
            sw.Start()
            DGV.DefaultCellStyle.BackColor = Color.Beige
            DGV.DefaultCellStyle.ForeColor = Color.Maroon
            DGV.DefaultCellStyle.Font = New Font("Consolas", 9)
            sw.Stop()
            MessageBox.Show("Took " & sw.ElapsedMilliseconds.ToString & " ms to reset colors and font")
        End Sub
    End Class
    
    

    • Proposed as answer by Frank L. Smith Wednesday, March 15, 2017 11:49 PM
    Wednesday, March 15, 2017 11:46 PM
  • DGV.DefaultCellStyle.BackColor = Color.Beige
            DGV.DefaultCellStyle.ForeColor = Color.Maroon
            DGV.DefaultCellStyle.Font = New Font("Consolas", 9)

    For all the above code does not change the color or font of the cells which i set already

      dg.Item(1, 1).Style.Font = New Font("Arial", 25)
      dg.Item(1, 1).Style.ForeColor = Color.GreenYellow

     dg.Item(2, 1).Style.Font = New Font("Arial", 15)
     dg.Item(2, 1).Style.ForeColor = Color.Red

    is there any way selectall and change the color and font of the selected cells?

    Thursday, March 16, 2017 5:00 AM
  • Hi

    Here is some code that lets the user select multiple cells and then click one button to set properties, and another button to reset to defaults.

    You need to edit the 'def' properties and the 'set'properties yourself at the top of the code.

    Per the title of your question, you could have a button to 'Select All'.

    ' Form1 with blank DataGridView1, 
    ' Button1, Button2
    Option Strict On
    Option Explicit On
    Option Infer Off
    Public Class Form1
        Dim myTable As New DataTable("Freddy")
        Dim rand As New Random
        Dim defBC As Color = Color.Beige
        Dim defFC As Color = Color.DarkBlue
        Dim defFNT As Font = New Font("TimesNew Roman", 9, FontStyle.Regular)
    
        Dim setBC As Color = Color.Red
        Dim setFC As Color = Color.White
        Dim setFNT As Font = New Font("Arial", 16, FontStyle.Bold)
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Button1.Text = "Set Properties"
            Button2.Text = "Default Properties"
    
            With myTable
                .Columns.Add("One", GetType(String))
                .Columns.Add("Two", GetType(String))
                .Columns.Add("Three", GetType(String))
                .Columns.Add("Four", GetType(String))
                .Columns.Add("Five", GetType(String))
                .Columns.Add("Six", GetType(String))
                .Columns.Add("Seven", GetType(String))
                .Columns.Add("Eight", GetType(String))
    
                For i As Integer = 1 To 20
                    .Rows.Add(rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999), rand.Next(999, 9999))
                Next
            End With
            With DataGridView1
                .DataSource = myTable
                .SelectionMode = DataGridViewSelectionMode.CellSelect
                .DefaultCellStyle.BackColor = defBC
                .DefaultCellStyle.ForeColor = defFC
                .DefaultCellStyle.Font = defFNT
            End With
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For Each c As DataGridViewCell In DataGridView1.SelectedCells
                c.Style.BackColor = setBC
                c.Style.ForeColor = setFC
                c.Style.Font = setFNT
            Next
            DataGridView1.AutoResizeRows()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            For Each c As DataGridViewCell In DataGridView1.SelectedCells
                c.Style.BackColor = defBC
                c.Style.ForeColor = defFC
                c.Style.Font = defFNT
            Next
            DataGridView1.AutoResizeRows()
        End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, March 16, 2017 2:54 PM
    Thursday, March 16, 2017 2:48 PM

  • For all the above code does not change the color or font of the cells which i set already


    is there any way selectall and change the color and font of the selected cells?

    Yes but in the same way as changing the hair color of all Americans, you have to do it one by one. 

    There is no big brush which give them with one stroke the same color as their president. 



    Success
    Cor


    Thursday, March 16, 2017 2:58 PM
  • LOL!

    Regards Les, Livingston, Scotland

    Thursday, March 16, 2017 3:11 PM
  • DGV.DefaultCellStyle.BackColor = Color.Beige
            DGV.DefaultCellStyle.ForeColor = Color.Maroon
            DGV.DefaultCellStyle.Font = New Font("Consolas", 9)

    For all the above code does not change the color or font of the cells which i set already

      dg.Item(1, 1).Style.Font = New Font("Arial", 25)
      dg.Item(1, 1).Style.ForeColor = Color.GreenYellow

     dg.Item(2, 1).Style.Font = New Font("Arial", 15)
     dg.Item(2, 1).Style.ForeColor = Color.Red

    is there any way selectall and change the color and font of the selected cells?

    Maintain a list of row and Column Indexes for each cell as it is selected

    Form Level:

        Dim SelectedColumnIndexes As New List(Of Integer)
        Dim SelectedRowIndexes As New List(Of Integer)

    Clear both lists when the DataGridView is populated
    When a cell is Selected(clicked):

        Private Sub DGV_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGV.CellContentClick
            DGV.Item(e.ColumnIndex, e.RowIndex).Style.BackColor = Color.Red
            DGV.Item(e.ColumnIndex, e.RowIndex).Style.ForeColor = Color.White
            SelectedColumnIndexes.Add(e.ColumnIndex)
            SelectedRowIndexes.Add(e.RowIndex)
        End Sub

    When you wish to reset:

            For index = 0 To SelectedColumnIndexes.Count - 1
                DGV.Item(SelectedColumnIndexes(index), SelectedRowIndexes(index)).Style.BackColor = Color.Beige
                DGV.Item(SelectedColumnIndexes(index), SelectedRowIndexes(index)).Style.ForeColor = Color.Maroon
            Next
    Reset the Font also if desired. 

    Clear the lists after the reset also

    Friday, March 17, 2017 1:38 AM
  • DGV.DefaultCellStyle.BackColor = Color.Beige
            DGV.DefaultCellStyle.ForeColor = Color.Maroon
            DGV.DefaultCellStyle.Font = New Font("Consolas", 9)

    For all the above code does not change the color or font of the cells which i set already

      dg.Item(1, 1).Style.Font = New Font("Arial", 25)
      dg.Item(1, 1).Style.ForeColor = Color.GreenYellow

     dg.Item(2, 1).Style.Font = New Font("Arial", 15)
     dg.Item(2, 1).Style.ForeColor = Color.Red

    is there any way selectall and change the color and font of the selected cells?

    Using the same form as I posted, substitute this code:

    Public Class Form1
        Dim R As New Random
        Dim SelectedColumnIndexes As New List(Of Integer)
        Dim SelectedRowIndexes As New List(Of Integer)
        Dim MyFonts As New List(Of String)
    
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            Dim columns(49) As DataGridViewColumn
            For i As Integer = 0 To columns.Length - 1
                Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
                column.CellTemplate = New DataGridViewTextBoxCell()
                column.FillWeight = 1
                column.HeaderText = i.ToString
                columns(i) = column
            Next i
    
            DGV.SuspendLayout()
            DGV.Columns.AddRange(columns)
    
            Dim RowData(49) As String
            For rw = 0 To 99
                RowData(0) = rw.ToString
                For cl = 1 To RowData.Length - 1
                    RowData(cl) = IO.Path.GetRandomFileName  'R.Next(100, 1000).ToString
                Next
                DGV.Rows.Add(RowData)
            Next
            DGV.ResumeLayout()
    
            For Test = 0 To 1000
                Dim ro As Integer = R.Next(0, DGV.RowCount)
                Dim co As Integer = R.Next(0, DGV.ColumnCount)
                DGV.Item(co, ro).Style.ForeColor = Color.Red
                DGV.Item(co, ro).Style.BackColor = Color.Chartreuse
                DGV.Item(co, ro).Style.Font = New Font(MyFonts(R.Next(0, MyFonts.Count)), CInt(R.Next(8, 13)))
                SelectedColumnIndexes.Add(co)
                SelectedRowIndexes.Add(ro)
            Next
        End Sub
    
        Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
            For index = 0 To SelectedColumnIndexes.Count - 1
                DGV.Item(SelectedColumnIndexes(index), SelectedRowIndexes(index)).Style.BackColor = Color.Beige
                DGV.Item(SelectedColumnIndexes(index), SelectedRowIndexes(index)).Style.ForeColor = Color.Maroon
                DGV.Item(SelectedColumnIndexes(index), SelectedRowIndexes(index)).Style.Font = New Font("Consolas", 9)
            Next
            DGV.DefaultCellStyle.BackColor = Color.Beige
            DGV.DefaultCellStyle.ForeColor = Color.Maroon
            DGV.DefaultCellStyle.Font = New Font("Consolas", 9)
    
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim fonts As New Drawing.Text.InstalledFontCollection
            For Each F As FontFamily In fonts.Families
                MyFonts.Add(F.Name)
            Next
        End Sub
    End Class
    
    

    Friday, March 17, 2017 2:21 AM
  • OK, now I know that what i want was not possible without iteration. So thanks for all.
    Saturday, March 18, 2017 5:18 AM