Results 1 to 6 of 6

Thread: DGV Load

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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!

  2. #2
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    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
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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?

  4. #4
    Hyperactive Member mbutler755's Avatar
    Join Date
    May 2008
    Location
    Peoria, AZ
    Posts
    417

    Re: DGV Load

    Take your file load code out of the loop
    Regards,

    Matt Butler, MBA, BSIT/SE, MCBP
    Owner, Intense IT, LLC
    Find us on Facebook
    Follow us on Twitter
    Link up on LinkedIn
    mb (at) i2t.us

    CODE BANK SUBMISSIONS: Converting Images to Base64 and Back Again

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,684

    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Aug 2011
    Posts
    198

    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
  •  



Click Here to Expand Forum to Full Width