Results 1 to 15 of 15

Thread: DataGridView formatting firiing multiple times

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    DataGridView formatting firiing multiple times

    I have a DataGridView populated when entering a Tab. I need to loop through the DataGridView after the Datasource is assigned to apply formatting, however the method I'm using to format the grid is firing multiple times. I've tried a few methods like .DataBindingComplete and .Sorted but the results are the same. I read a post where someone used DataGridViewCellFormattingEventArgs but I didn't understand how to handle this. I just need to find a method that reliably fires only once after the DGV is populated to do the loop.

    I had to sanitize this pretty heavily so I apologize for any typos:

    VB.NET Code:
    1. Private Sub Tab3Enter(sender, e) Handles TabPage3.Enter
    2.         Call ProjTab()
    3. End Sub
    4.  
    5. Dim dt As New
    6.  
    7. Public Sub ProjTab()
    8.  
    9.         'DataGridView 3
    10.         dt.Clear()
    11.  
    12.         Dim sqConQC As New SqlClient.SqlConnection(DATABASE)
    13.         Dim sqCmdQC As New SqlClient.SqlCommand
    14.  
    15.         sqCmdQC.Connection = sqConQC            'create the DB connection
    16.         sqConQC.Open()                        'open the connection
    17.         Dim Adapter As New SqlDataAdapter
    18.  
    19.  
    20.         sql = "SELECT * FROM QuoteView WHERE DateModified >= '2019-01-01' AND Accepted = 1 AND (Released = 1 AND SONO IS NULL) OR ReleaseMod = 1 ORDER BY QuoteNo DESC"
    21.         Adapter.SelectCommand = New SqlCommand(sql, sqConQC)
    22.         Adapter.Fill(dt)
    23.         sqConQC.Close()
    24.  
    25.         Me.DataGridView3.DataSource = dt
    26.  
    27.   End Sub
    28.  
    29.    Private Sub ColorDGV(sender As Object, e As EventArgs) Handles DataGridView1.DataBindingComplete, DataGridView2.DataBindingComplete, DataGridView3.DataBindingComplete
    30.  
    31.         Dim DGV As DataGridView = Nothing
    32.  
    33.         If Me.TabControl1.SelectedTab Is TabPage1 Then
    34.             DGV = Me.DataGridView1
    35.         ElseIf Me.TabControl1.SelectedTab Is TabPage2 Then
    36.             DGV = Me.DataGridView2
    37.         ElseIf Me.TabControl1.SelectedTab Is TabPage3 Then
    38.             DGV = Me.DataGridView3
    39.         End If
    40.  
    41.         If DGV.IsHandleCreated Then
    42.  
    43.             Me.ProgressBar1.Visible = True
    44.             Dim i As Integer = DGV.Rows.Count
    45.  
    46.             If i > 0 Then
    47.  
    48.                 ProgressBar1.Maximum = i
    49.                 Dim c As Integer = 0
    50.                 For Each row As DataGridViewRow In DGV.Rows
    51.                  
    52.                    'do lots of stuff
    53.  
    54.                     If ProgressBar1.Value <> ProgressBar1.Maximum Then
    55.                         c = c + 1
    56.                     End If
    57.                     ProgressBar1.Value = c
    58.                 Next
    59.             End If
    60.         End If
    61.         Me.ProgressBar1.Visible = False
    62.  
    63.     End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataGridView formatting firiing multiple times

    Quote Originally Posted by Fedaykin View Post
    I need to loop through the DataGridView after the Datasource is assigned to apply formatting
    That is almost certainly not the case. There are two ways that you should be formatting a DataGridView. In simple cases, set the DefaultCellStyle.Format property of columns as required. In more complex cases, handle the CellFormatting event of the grid. If "formatting" means more than how the text is displayed, you may need to handle the CellPainting event.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    I tried handling CellFormatting and it fired more times than I could count. I tried setting breakpoints on every other sub in the form but nothing else fired. It completes the loop in ColorDGV and then fires DGVLoaded again and again.

    VB.NET Code:
    1. Private Sub DGVLoaded(sender As Object, e As EventArgs) Handles DataGridView1.CellFormatting, DataGridView2.CellFormatting, DataGridView3.CellFormatting
    2.  
    3.         Call ColorDGV()
    4.  
    5.     End Sub
    6.  
    7. Private Sub ColorDGV()
    8.  
    9.         Dim DGV As DataGridView = Nothing
    10.  
    11.         If Me.TabControl1.SelectedTab Is TabPage1 Then
    12.             DGV = Me.DataGridView1
    13.         ElseIf Me.TabControl1.SelectedTab Is TabPage2 Then
    14.             DGV = Me.DataGridView2
    15.         ElseIf Me.TabControl1.SelectedTab Is TabPage3 Then
    16.             DGV = Me.DataGridView3
    17.         End If
    18.  
    19.         If DGV.IsHandleCreated Then
    20.  
    21.             Me.ProgressBar1.Visible = True
    22.             Dim i As Integer = DGV.Rows.Count
    23.  
    24.             If i > 0 Then
    25.  
    26.                 ProgressBar1.Maximum = i
    27.                 Dim c As Integer = 0
    28.                 For Each row As DataGridViewRow In DGV.Rows
    29.                  
    30.                    'do lots of stuff
    31.  
    32.                     If ProgressBar1.Value <> ProgressBar1.Maximum Then
    33.                         c = c + 1
    34.                     End If
    35.                     ProgressBar1.Value = c
    36.                 Next
    37.             End If
    38.         End If
    39.         Me.ProgressBar1.Visible = False
    40.  
    41.     End Sub

    Changing my sequence to directly call ColorDGV after the DataGridView populates solves the issue when the form loads, however when changing tabs the DataGridViews populates but refuse to run the ColorDGV unless I chose to handle DataGridView3.Sorted and manually sort the column.

    ColorDGV is directly called in after the DataSource is set, but it doesn't actually run until the form is fully populated and visible. Any attempt to fake out the system and do direct calls ends up firing more than once. If I put this in a button click it will work fine, but I need to automatically color the cells when the form loads.
    Last edited by Fedaykin; Apr 1st, 2020 at 04:07 PM.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    I tried handling DataGridView3.DataSourceChanged and the DGVColor sub definitely runs, but it does not update the colors in the DataGridView. Again, if I let the DataGridView fully populate on the tab and the form completely opens and I put this in a button click it will work.

    VB.NET Code:
    1. Private Sub ColorDGV(sender As Object, e As EventArgs) Handles DataGridView1.DataSourceChanged, DataGridView2.DataSourceChanged, DataGridView3.DataSourceChanged
    2.  
    3.    'Loop and change Style.BackColors by criteria
    4.  
    5. End Sub

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    I tried handling DataGridView3.DataSourceChanged and the DGVColor sub definitely runs, but it does not update the colors in the DataGridView. Again, if I let the DataGridView fully populate on the tab and the form completely opens and I put this in a button click it will work.

    VB.NET Code:
    1. Private Sub ColorDGV(sender As Object, e As EventArgs) Handles DataGridView1.DataSourceChanged, DataGridView2.DataSourceChanged, DataGridView3.DataSourceChanged
    2.  
    3.    'Loop and change Style.BackColors by criteria
    4.  
    5. End Sub

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: DataGridView formatting firiing multiple times

    I would use the Tabpage SelectedIndex
    to Load and Change each DGV

    something like this
    the ' modMain.SQLSelectDataSet' is a reusable Function in a Module
    but this should give you an idea to use the 'TabControl1.SelectedIndexChanged'
    Code:
     Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
                                                     ByVal e As System.EventArgs) _
             Handles TabControl1.SelectedIndexChanged
    
            Dim objDataSet As DataSet
            objDataSet = New DataSet()
    
            Select Case TabControl1.SelectedIndex
                Case 0 ' User clicks on First Tab
                    modMain.SQLSelectDataSet("Select * From Personal", objDataSet)
                    DataGridView1.DataSource = objDataSet.Tables(0)
                    'Color DGV here
                Case 1 ' User clicks on Second Tab
                    modMain.SQLSelectDataSet("Select * From Customers", objDataSet)
                    DataGridView2.DataSource = objDataSet.Tables(0)
                    'Color DGV here
    
                Case 2 ' User clicks on Third Tab
                    ' you get the Idea
    
                Case 3 ' User clicks on Fourth Tab
    
            End Select
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    I was using TabPage1.Enter, do you think TabControl SelectedIndexChanged will have a different result? I'm not sure why it would but I will definitely give it a try.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    Quote Originally Posted by ChrisE View Post
    I would use the Tabpage SelectedIndex
    to Load and Change each DGV

    something like this
    the ' modMain.SQLSelectDataSet' is a reusable Function in a Module
    but this should give you an idea to use the 'TabControl1.SelectedIndexChanged'
    Code:
     Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
                                                     ByVal e As System.EventArgs) _
             Handles TabControl1.SelectedIndexChanged
    
            Dim objDataSet As DataSet
            objDataSet = New DataSet()
    
            Select Case TabControl1.SelectedIndex
                Case 0 ' User clicks on First Tab
                    modMain.SQLSelectDataSet("Select * From Personal", objDataSet)
                    DataGridView1.DataSource = objDataSet.Tables(0)
                    'Color DGV here
                Case 1 ' User clicks on Second Tab
                    modMain.SQLSelectDataSet("Select * From Customers", objDataSet)
                    DataGridView2.DataSource = objDataSet.Tables(0)
                    'Color DGV here
    
                Case 2 ' User clicks on Third Tab
                    ' you get the Idea
    
                Case 3 ' User clicks on Fourth Tab
    
            End Select
        End Sub
    You saved my life!!! Why on Earth does TabControl1.SelectedIndexChanged work when TabPage1.Enter does not?? It's not logical. Actually, all of the other methods suggested SHOULD be able to be applied to this scenario but they just freak out and fire all over the place. I love VB.net but sometimes it behaves like a 2 year old on chocolate.

    Thank you very much!

  9. #9
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: DataGridView formatting firiing multiple times

    you want to start you code when clicking a certain Tabpage thats why= TabControl1.SelectedIndexChanged
    the TabPage1.Enter will start only if you click the TabPage first and then click somewhere within that selected Tabpage

    so for your needs not suitable
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    Thank you for the explanation, however now I lose my formatting if I sort the DGV. I have tried changing the ColorDGV to:

    VB.NET Code:
    1. Private Sub ColorDGV(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
    2.  
    3.  'do formatting here
    4.  
    5. EndSub

    But this fires for every cell on every row in the DataGridView.

    I need to keep my formatting if the user sorts the data.

  11. #11
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: DataGridView formatting firiing multiple times

    you will have to show what and how you are coloring the DGV


    just looked at you other post...
    ColorDGV is directly called in after the DataSource is set, but it doesn't actually run until the form is fully populated and visible. Any attempt to fake out the system and do direct calls ends up firing more than once. If I put this in a button click it will work fine, but I need to automatically color the cells when the form loads.
    so if you look at what I wrote before...
    if the Button Click works as you said
    Code:
     Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
                                                     ByVal e As System.EventArgs) _
             Handles TabControl1.SelectedIndexChanged
    
            Dim objDataSet As DataSet
            objDataSet = New DataSet()
    
            Select Case TabControl1.SelectedIndex
                Case 0 ' User clicks on First Tab
                    modMain.SQLSelectDataSet("Select * From Personal", objDataSet)
                    DataGridView1.DataSource = objDataSet.Tables(0)
                    'Color DGV here
                    Button1.PerformClick() ' <---- color DGV
                Case 1 ' User clicks on Second Tab
                    modMain.SQLSelectDataSet("Select * From Customers", objDataSet)
                    DataGridView2.DataSource = objDataSet.Tables(0)
                    'Color DGV here
                    Button2.PerformClick() ' <---- color DGV
    
                Case 2 ' User clicks on Third Tab
                    ' you get the Idea
    
                Case 3 ' User clicks on Fourth Tab
    
            End Select
        End Sub
    Last edited by ChrisE; Apr 2nd, 2020 at 02:16 PM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    To be clear your solution worked perfectly for populating the form (without using the button click):

    VB.NET Code:
    1. Private Sub TabControl1_Changed(sender As Object, e As EventArgs) Handles TabControl1.SelectedIndexChanged
    2.         Dim DGV As DataGridView = Nothing
    3.         Select Case TabControl1.SelectedIndex
    4.             Case 0 ' User clicks on First Tab
    5.                 Call ProjTab()
    6.                 DGV = DataGridView3
    7.             Case 1 ' User clicks on Second Tab
    8.                 Call RefOpen()
    9.                 DGV = DataGridView1
    10.             Case 2 ' User clicks on Third Tab
    11.                 Call RefClosed()
    12.                 DGV = DataGridView2
    13.         End Select
    14.  
    15.         Call ColorDGV(DGV)
    16.  
    17.     End Sub
    18.  
    19.  
    20.     Private Sub ColorDGV(DGV As DataGridView)
    21.  
    22.         If DGV.IsHandleCreated Then
    23.             If DGV Is DataGridView2 And Me.AllDatesCheckBox_Closed.Checked = True Then
    24.                 Exit Sub
    25.             End If
    26.  
    27.             Dim col As DataGridViewColumn = DGV.Columns("QRev")
    28.             col.DefaultCellStyle.BackColor = Color.LightGray
    29.  
    30.             col = DGV.Columns("QuoteNo")
    31.             col.DefaultCellStyle.BackColor = Color.LightGray
    32.  
    33.             col = DGV.Columns("PType")
    34.             col.DefaultCellStyle.Font = New Font(DGV.Font, FontStyle.Italic)
    35.  
    36.             Me.ProgressBar1.Visible = True
    37.             Dim i As Integer = DGV.Rows.Count
    38.  
    39.             If i > 0 Then
    40.  
    41.                 ProgressBar1.Maximum = i
    42.                 Dim c As Integer = 0
    43.                 For Each row As DataGridViewRow In DGV.Rows
    44.                     'Read a record from the database table
    45.                     Dim sqCon As New SqlClient.SqlConnection(MAINDB)
    46.                     Dim sqCmd As New SqlClient.SqlCommand
    47.                     Dim QuoteNo As String = row.Cells.Item("QuoteNo").Value
    48.                     Dim dbDORate As Boolean = False
    49.                     Dim dbDXRate As Boolean = False
    50.                     Dim SageSoReq As Boolean = False
    51.                     Dim ReleaseModBol As Boolean = False
    52.                     sqCmd.Connection = sqCon            'create the DB connection
    53.                     sqCon.Open()                        'open the connection
    54.                     sqCmd.CommandText = "SELECT DORate, DXRate, SageSoReq, ReleaseMod FROM QuoteView WHERE QuoteNo = '" & QuoteNo & "'"
    55.  
    56.                     Dim sqReader As SqlDataReader = sqCmd.ExecuteReader()        'execute the SQL command
    57.                     If sqReader.HasRows Then
    58.                         While sqReader.Read()
    59.                             If Not IsDBNull(sqReader.Item("DORate")) Then
    60.                                 dbDORate = sqReader.Item("DORate")
    61.                             End If
    62.                             If Not IsDBNull(sqReader.Item("DXRate")) Then
    63.                                 dbDXRate = sqReader.Item("DXRate")
    64.                             End If
    65.                             If Not IsDBNull(sqReader.Item("SageSoReq")) Then
    66.                                 SageSoReq = sqReader.Item("SageSoReq")
    67.                             End If
    68.                             If Not IsDBNull(sqReader.Item("ReleaseMod")) Then
    69.                                 ReleaseModBol = sqReader.Item("ReleaseMod")
    70.                             End If
    71.                         End While
    72.                     End If
    73.                     sqReader.Close()         'always close the reader when you're done with it.
    74.                     sqCon.Close()
    75.  
    76.                     If dbDORate = True Then
    77.                         row.Cells("DueDate").Style.BackColor = Color.DarkOliveGreen
    78.                         row.Cells("DueDate").Style.ForeColor = Color.White
    79.                         If DGV Is DataGridView1 Then
    80.                             Me.DOLbl1.Visible = True
    81.                         End If
    82.                     End If
    83.                     If dbDXRate = True Then
    84.                         row.Cells("DueDate").Style.BackColor = Color.Navy
    85.                         row.Cells("DueDate").Style.ForeColor = Color.White
    86.                         If DGV Is DataGridView1 Then
    87.                             Me.DXLbl1.Visible = True
    88.                         End If
    89.                     End If
    90.                     If SageSoReq = True Then
    91.                         row.Cells("CustName").Style.BackColor = Color.LightGray
    92.                         row.Cells("PartNum").Style.BackColor = Color.LightGray
    93.                     End If
    94.                     If DGV Is DataGridView3 Then
    95.                         Dim JobID As String = QuoteNo & row.Cells("PType").Value
    96.                         If SageSOBol(JobID) = True Then
    97.                             row.Cells("QuoteNo").Style.BackColor = Color.LightGreen
    98.                             row.Cells("QRev").Style.BackColor = Color.LightGreen
    99.                             row.Cells("CustName").Style.BackColor = Color.LightGreen
    100.                             row.Cells("PartNum").Style.BackColor = Color.LightGreen
    101.                         End If
    102.                     ElseIf DGV Is DataGridView1 Then
    103.                         If row.Cells.Item("DueDate").Value < Date.Today Then
    104.                             row.Cells.Item("DueDate").Style.BackColor = Color.Red
    105.                         End If
    106.                         If row.Cells.Item("DueDate").Value > Date.Today Then
    107.                             row.Cells.Item("DueDate").Style.BackColor = Color.Honeydew
    108.                         End If
    109.                     End If
    110.                     If ReleaseModBol = True Then
    111.                         row.Cells("QuoteNo").Style.BackColor = Color.Orange
    112.                         row.Cells("QRev").Style.BackColor = Color.Orange
    113.                         row.Cells("CustName").Style.BackColor = Color.Orange
    114.                         row.Cells("PartNum").Style.BackColor = Color.Orange
    115.                     End If
    116.                     'Check for open tickets on this Work Order
    117.                     Try
    118.                         Dim TagNo As String = Nothing
    119.                         Dim dbClosed As Boolean = True
    120.  
    121.                         TagNo = row.Cells.Item("QuoteNO").Value
    122.                         sqCmd = sqCon.CreateCommand()
    123.                         sqCmd.CommandText = "SELECT Closed FROM dbo.Tagged WHERE TagNo = '" & TagNo & "'"
    124.                         sqCon.Open()
    125.                         Dim RetVal As Object = sqCmd.ExecuteScalar()
    126.                         If Not IsNothing(RetVal) And Not IsDBNull(RetVal) Then
    127.                             dbClosed = RetVal 'Returns a single value
    128.                         End If
    129.                         sqCon.Close()
    130.                         If dbClosed = False Then
    131.                             row.Cells.Item("QuoteNO").ErrorText = "Open Ticket"
    132.                         End If
    133.                     Catch ex As Exception
    134.                         MessageBox.Show(ex.ToString)
    135.                     End Try
    136.                     If ProgressBar1.Value <> ProgressBar1.Maximum Then
    137.                         c = c + 1
    138.                     End If
    139.                     ProgressBar1.Value = c
    140.                 Next
    141.             End If
    142.         End If
    143.         Me.ProgressBar1.Visible = False
    144.  
    145.     End Sub

    The problem now is that when the DataGridView is loaded and formatted with color (and the ColorDGV is only firing once using your method of TabControl1.SelectedIndexChanged!) If the user sorts a column then all of the formatting goes away and we are left with no background colors. How do we get the formatting we worked so hard for to survive a sort without having to loop back through the ColorDGV again?
    Last edited by Fedaykin; Apr 2nd, 2020 at 02:51 PM.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: DataGridView formatting firiing multiple times

    your ColorDGV code is for Datagridview1 to Datagridview3
    what is the point of that if you are using a Tabcontrol ?

    if you have a Tabcontrol with 3 Pages and you click Tabpage2 you will see Datagridview2
    you should only execute the color routine Code for that Datagridview
    I mean what is the point of calling the Color routine for DGV1 and DGV2 if you clicked Tabpage3 to show DGV3

    I would separate those color routines

    EDIT:
    consider this sample, click on any HeaderCell for sorting
    Code:
    Public Class Form10
    
        Dim tb As DataTable = New DataTable
    
    
        Private Sub Form10_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            tb.Columns.Add("ID")
            tb.Columns.Add("QtyOut")
            tb.Columns.Add("QtyIn")
            tb.Columns.Add("Stock")
            tb.Columns.Add("Balance")
            tb.Rows.Add("1", "20", "230", "400", "Confirmed")
            tb.Rows.Add("2", "6", "210", "300", "On Hold")
            tb.Rows.Add("3", "5", "220", "25", "Incomplete")
            tb.Rows.Add("4", "4", "126", "1233", "Urgent")
            DataGridView1.DataSource = tb
            ApplyFormatting()
        End Sub
    
        Private Sub ApplyFormatting()
            For Each row As DataGridViewRow In DataGridView1.Rows
                Select Case CStr(row.Cells(4).Value) 'based on Cell 5 color Cell 2
                    Case Is = "Confirmed"
                        row.Cells(0).Style.BackColor = Color.Aquamarine
                        'or any other Cell  1 or 3 in this case
                    Case Is = "On Hold"
                        row.Cells(2).Style.BackColor = Color.Pink
                    Case Is = "Incomplete"
                        row.Cells(3).Style.BackColor = Color.Bisque
                    Case Is = "Urgent"
                        row.Cells(2).Style.BackColor = Color.Red
                End Select
            Next
        End Sub
    
        Private Sub DataGridView1_ColumnHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DataGridView1.ColumnHeaderMouseClick
            ApplyFormatting()
        End Sub
    End Class
    Last edited by ChrisE; Apr 3rd, 2020 at 02:22 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Jul 2013
    Posts
    178

    Re: DataGridView formatting firiing multiple times

    No, the ColorDGV only runs on the DataGridView that is opened, it does not run all 3. The code even has IF statements that only run based on the the DGV that is selected/opened.

    I was trying not to rerun the color routine each time something is sorted, find some way to make it 'sticky'. Is there no way to avoid re-running formatting everytime there is a sort?

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,129

    Re: DataGridView formatting firiing multiple times

    did you try the
    DataGridView1_CellFormatting ?

    Code:
     Dim tb As DataTable = New DataTable
    
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            tb.Columns.Add("ID")
            tb.Columns.Add("QtyOut")
            tb.Columns.Add("QtyIn")
            tb.Columns.Add("Stock")
            tb.Columns.Add("Balance")
            tb.Rows.Add("1", "20", "230", "400", "Confirmed")
            tb.Rows.Add("2", "6", "210", "300", "On Hold")
            tb.Rows.Add("3", "5", "220", "25", "Incomplete")
            tb.Rows.Add("4", "4", "126", "1233", "Urgent")
            DataGridView1.DataSource = tb
    
        End Sub
    
    
        Private Sub DataGridView1_CellFormatting(sender As Object, e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting
            For Each row As DataGridViewRow In DataGridView1.Rows
                Select Case CStr(row.Cells(4).Value) 'based on Cell 5 color Cell 2
                    Case Is = "Confirmed"
                        row.Cells(0).Style.BackColor = Color.Aquamarine
                        'or any other Cell  1 or 3 in this case
                    Case Is = "On Hold"
                        row.Cells(2).Style.BackColor = Color.Pink
                    Case Is = "Incomplete"
                        row.Cells(3).Style.BackColor = Color.Bisque
                    Case Is = "Urgent"
                        row.Cells(2).Style.BackColor = Color.Red
                End Select
            Next
        End Sub
    No, the ColorDGV only runs on the DataGridView that is opened, it does not run all 3. The code even has IF statements that only run based on the the DGV that is selected/opened.
    what's the point of doing that if you allready use the 'TabControl1_SelectedIndexChanged'
    why do you want to check which DGV to Color ?
    Last edited by ChrisE; Apr 3rd, 2020 at 11:26 PM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width