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:
Private Sub Tab3Enter(sender, e) Handles TabPage3.Enter Call ProjTab() End Sub Dim dt As New Public Sub ProjTab() 'DataGridView 3 dt.Clear() Dim sqConQC As New SqlClient.SqlConnection(DATABASE) Dim sqCmdQC As New SqlClient.SqlCommand sqCmdQC.Connection = sqConQC 'create the DB connection sqConQC.Open() 'open the connection Dim Adapter As New SqlDataAdapter 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" Adapter.SelectCommand = New SqlCommand(sql, sqConQC) Adapter.Fill(dt) sqConQC.Close() Me.DataGridView3.DataSource = dt End Sub Private Sub ColorDGV(sender As Object, e As EventArgs) Handles DataGridView1.DataBindingComplete, DataGridView2.DataBindingComplete, DataGridView3.DataBindingComplete Dim DGV As DataGridView = Nothing If Me.TabControl1.SelectedTab Is TabPage1 Then DGV = Me.DataGridView1 ElseIf Me.TabControl1.SelectedTab Is TabPage2 Then DGV = Me.DataGridView2 ElseIf Me.TabControl1.SelectedTab Is TabPage3 Then DGV = Me.DataGridView3 End If If DGV.IsHandleCreated Then Me.ProgressBar1.Visible = True Dim i As Integer = DGV.Rows.Count If i > 0 Then ProgressBar1.Maximum = i Dim c As Integer = 0 For Each row As DataGridViewRow In DGV.Rows 'do lots of stuff If ProgressBar1.Value <> ProgressBar1.Maximum Then c = c + 1 End If ProgressBar1.Value = c Next End If End If Me.ProgressBar1.Visible = False End Sub




Reply With Quote
