I'm experiencing a problem with the datagridview
. I'm trying to create a program that allows me to run certain tests. I want to list them in a datagridview
. Then with a counter I want the program to go through the active serverprocesses and run them. In the overview I want to visualize whether a process is active by coloring a cell that i add. I've done this before and used the same code but this time it doesn't seem to work. You can find my code for the load
event of the form here. Can anyone tell me what I'm doing wrong?
Private Sub frmServerProcesses_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim strSQL As String
Dim oDR As SqlClient.SqlDataReader = Nothing
'bij het opstarten moet de stopknop disabled zijn
btnServerStop.Enabled = False
btnServerStart.Enabled = True
statServerProcesseslbl.Text = "Server ready"
'opvragen van de processen
strSQL = " SELECT * FROM SERVERPROCESSES"
Dim connection As New SqlConnection(SQLConnectstring)
Dim dataadapter As New SqlDataAdapter(strSQL, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "SERVERPROCESSES")
connection.Close()
dgvServerProcesses.DataSource = ds
dgvServerProcesses.DataMember = "SERVERPROCESSES"
dgvServerProcesses.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
dgvServerProcesses.Columns("LASTRUNDATE").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
'lege row die normaal wordt toegevoegd verwijderen
dgvServerProcesses.AllowUserToAddRows = False
'extra kolom toevoegen voor kleuren van actief of niet
dgvServerProcesses.Columns.Add("TEST", "TEST")
For i As Integer = 1 To dgvServerProcesses.Rows.Count - 1
If dgvServerProcesses.Rows(i).Cells(2).Value = "True" Then
dgvServerProcesses.Rows(i).Cells(2).Style.BackColor = Color.Black
End If
Next
'lege row die normaal wordt toegevoegd verwijderen
dgvServerProcesses.AllowUserToAddRows = False
End Sub
I've just tried to put both things in separate subs. So filling up the grid and marking the active ones are separate now. Both subs are called by the load
sub. But still the colors don't work. But if I call the checkprocess
with a button it does work. But what is also very strange is that the columncount is different. If I call it with a button he starts counting on 1 in the other case on 0. Can someone explain that to me? My new code to check the active boolean is shown here
Public Sub CheckProcessActive()
For i As Integer = 0 To dgvServerProcesses.Rows.Count - 1
If dgvServerProcesses.Rows(i).Cells(2).Value = "True" Then
dgvServerProcesses.Rows(i).Cells("ACTIVE").Style.BackColor = Color.Green
Else
dgvServerProcesses.Rows(i).Cells("ACTIVE").Style.BackColor = Color.Red
End If
Next
End Sub
The behavior you have noticed is normal. Even if you set the DataSource
property, the DatagridView
won't create columns and be populated until the grid is displayed, it's not populated immediately after setting this property.
You can use the DataGridView.DataBindingComplete event to be sure the grid is populated when you change the style and call CheckProcessActive()
here.