Le réseau pour les développeurs > Forums - Accueil > Visual Basic General > Programmatic Select of DataGridViewRow?
Poser une questionPoser une question
 

TraitéeProgrammatic Select of DataGridViewRow?

  • mercredi 4 novembre 2009 16:02jamesfreddyc Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    I have been looping thru all of the rows in a DGV and matching a variable to a specific cell in the row to 'do stuff to that DGVRow.


    For Each dgRow As DataGridViewRow In Me.dgvInsertCommercial.Rows
      If dgRow.Cells("SomeColumn").Value = pid Then
         'do stuff
      end if<br/>
    


    Is there a way to actually query or make a selection instead of having to loop thru all of the dgvRows?
    jfc

Réponses

  • mercredi 4 novembre 2009 16:20jwavila Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    if you have an in-memory datatable populating the DGV, you can use an SQL Select query to find a row with a specific value in a specific column.

    this is from something else I've used but you should be able to adapt it. dtTest is the name of the DataTable, and I'm typing my search term into a TextBox

    hope this helps


    If TextBox1.Text <> String.Empty Then
                Dim rows() As DataRow = dtTest.Select("Column1 = '" & TextBox1.Text & "'")
                
                If rows.Length > 0 Then
                    Label1.Text = rows.Length.ToString & "  Records"
                Else
                    Label1.Text = "Name not found"
                End If
            End If
    
    
    • Marqué comme réponsejamesfreddyc mercredi 4 novembre 2009 17:43
    •  

Toutes les réponses

  • mercredi 4 novembre 2009 16:18DeborahKMVPMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    If your DGV is bound to something, oftentimes it is easier to query against the original binding source than to loop through the grid.

    Hope this helps.
    www.insteptech.com ; msmvps.com/blogs/deborahk
    We are volunteers and ask only that if we are able to help you, that you mark our reply as your answer. THANKS!
  • mercredi 4 novembre 2009 16:20jwavila Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     TraitéeA du code
    if you have an in-memory datatable populating the DGV, you can use an SQL Select query to find a row with a specific value in a specific column.

    this is from something else I've used but you should be able to adapt it. dtTest is the name of the DataTable, and I'm typing my search term into a TextBox

    hope this helps


    If TextBox1.Text <> String.Empty Then
                Dim rows() As DataRow = dtTest.Select("Column1 = '" & TextBox1.Text & "'")
                
                If rows.Length > 0 Then
                    Label1.Text = rows.Length.ToString & "  Records"
                Else
                    Label1.Text = "Name not found"
                End If
            End If
    
    
    • Marqué comme réponsejamesfreddyc mercredi 4 novembre 2009 17:43
    •  
  • mercredi 4 novembre 2009 17:15jamesfreddyc Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     
    Thanks to the both of you for your input.

    jwavila,

    Sorry, I forgot to include that I have a DataTable as the DGV's DataSource.  Anyway, I actually have the DataTable.Select(expression) working great.  But I am not quite certain how to related those selected DataRow(s) to the DGV in order to 'do stuff' to the DGV.

    Specifically, I need to change those DGV's row colors that correspond to the selected dataRows.  I was initially using .IndexOf, but was not getting the correct rows.  Maybe I need to re-work that and attempt it again?

    Thanks again

    j
    jfc
  • mercredi 4 novembre 2009 17:42jamesfreddyc Médailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateurMédailles de l'utilisateur
     A du code
    Ok,

    Yep -- this works for me:

     Dim expr As String = "Property_ID ='" & gen2Ins.Rows(i).Item("Property_ID") & "'"
     Dim genRows() As DataRow = stormsExceptionsEntity.Excp_InPANotInStorms_GEN.Select(expr)
      For genr = 0 To genRows.Length - 1
        Dim dgvr As DataGridViewRow = Me.dgvInsertCommercial.Rows(stormsExceptionsEntity.Excp_InPANotInStorms_GEN.Rows.IndexOf(genRows(genr))) '(rowIndx)
        dgvr.DefaultCellStyle.BackColor = Drawing.Color.Khaki
        dgvr.ReadOnly = True
      Next genr
    

    jfc