-
Feb 2nd, 2012, 12:22 PM
#1
Thread Starter
Addicted Member
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!
-
Feb 2nd, 2012, 02:21 PM
#2
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
-
Feb 2nd, 2012, 05:09 PM
#3
Thread Starter
Addicted Member
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?
-
Feb 2nd, 2012, 05:51 PM
#4
Re: DGV Load
Take your file load code out of the loop
-
Feb 2nd, 2012, 08:35 PM
#5
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.
-
Feb 3rd, 2012, 09:51 AM
#6
Thread Starter
Addicted Member
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!
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|