Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Windows Forms
  4. add new row if not exist row in datagirdview1

add new row if not exist row in datagirdview1

Scheduled Pinned Locked Moved Windows Forms
3 Posts 3 Posters 32 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sufvan Adil 2023
    wrote on last edited by
    #1

    my code

    If DataGridView1.Rows.Count - 1 Then

                    DataGridView1.Rows.Add()
                        DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1)
                        DataGridView1.BeginEdit(True)
    
                Else
                    Exit Sub
                    '    DataGridView1.Rows.Add()
                End If
    

    i many code try

                If (DataGridView1.Rows.Count > 0) Then
                    For i = 0 To DataGridView1.Rows.Count - 1
                        If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
    
                            Exit Sub
                        Else
                            DataGridView1.Rows.Add()
                        End If
                    Next
                End If
    

    but add row

    L Richard DeemingR 2 Replies Last reply
    0
    • S Sufvan Adil 2023

      my code

      If DataGridView1.Rows.Count - 1 Then

                      DataGridView1.Rows.Add()
                          DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1)
                          DataGridView1.BeginEdit(True)
      
                  Else
                      Exit Sub
                      '    DataGridView1.Rows.Add()
                  End If
      

      i many code try

                  If (DataGridView1.Rows.Count > 0) Then
                      For i = 0 To DataGridView1.Rows.Count - 1
                          If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
      
                              Exit Sub
                          Else
                              DataGridView1.Rows.Add()
                          End If
                      Next
                  End If
      

      but add row

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      So what exactly is the problem?

      1 Reply Last reply
      0
      • S Sufvan Adil 2023

        my code

        If DataGridView1.Rows.Count - 1 Then

                        DataGridView1.Rows.Add()
                            DataGridView1.CurrentCell = DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells(1)
                            DataGridView1.BeginEdit(True)
        
                    Else
                        Exit Sub
                        '    DataGridView1.Rows.Add()
                    End If
        

        i many code try

                    If (DataGridView1.Rows.Count > 0) Then
                        For i = 0 To DataGridView1.Rows.Count - 1
                            If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
        
                                Exit Sub
                            Else
                                DataGridView1.Rows.Add()
                            End If
                        Next
                    End If
        

        but add row

        Richard DeemingR Offline
        Richard DeemingR Offline
        Richard Deeming
        wrote on last edited by
        #3

        Sufvan Adil 2023 wrote:

        If DataGridView1.Rows.Count - 1 Then

        Thanks to VB's "evil type coercion", that condition will be False if you have precisely one row in your grid. For any other number of rows, that condition will be True.

        Sufvan Adil 2023 wrote:

        For i = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
        Exit Sub
        Else
        DataGridView1.Rows.Add()
        End If
        Next

        That code walks through all the rows in your grid until it finds one where the d1alisname column is blank. For every row that it encounters where that column is not blank, it adds a new row. You haven't explained exactly what you're trying to do. At a guess, you want to add a new row if there are either no rows in the grid, or all of the rows have a value in the d1alisname column. In which case, you need to move the "add row" code after your For loop from your second code block:

        For i = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(i).Cells("d1alisname").Value = "" Then
        Exit Sub
        End If
        Next

        DataGridView1.Rows.Add()


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups