-
DGV Load
Hi all
Got the following code:
Code:
Dim i As Integer
DataGridView1.AllowUserToAddRows = True
If Not DataGridView1.LoadTextFile("C:\test.txt", ErrorList) Then
Dim sb As New System.Text.StringBuilder
sb.AppendLine("Failed to load text file, see error messages below.")
For Each item In ErrorList
sb.AppendLine(item)
Next
MessageBox.Show(sb.ToString)
End If
If CBool(Me.DataGridView1.Rows(i).Cells(3).Value) = True Then
DataGridView1.Rows(i).Cells("Column2").Style.BackColor = Color.Blue
DataGridView1.Rows(i).Cells("Column2").Style.ForeColor = Color.White
End If
The idea being that the DGV is loaded from a text file, and if the relevant column (checkbox) value is true, then the cells in Column 2 go blue with white font.
However, this only works if the datagridview is loaded with one row of data and needs to be repeated for however many rows might be loaded.
thanks for advice and time!
-
Re: DGV Load
You will need to get the number of rows in the DataGridView and then wrap your if statement that has a loop. Take a look around and you will find some examples.
Pseudocode:
Code:
DIM numRows as integer=dgv.rows.count
for i=0 to numRows
'... do your code here
next i
-
Re: DGV Load
hi matt
thanks for this! it seems to add my file twice to the dgv even when i only press the button once? any advice?
-
Re: DGV Load
Take your file load code out of the loop
-
Re: DGV Load
I would use CellFormatting event for changing the style of a column or columns. Check out the examples at this post. On top of the example shown you could have a if statement that even allows you to conditionally colorize cells dependent on the CheckBox in real time.
-
Re: DGV Load
hi matt, i can't seem to work out the order that my code should be in to take the load code out of the loop? can you help?
kevin - currently looking at your solution as well!
thanks to both!