Results 1 to 6 of 6

Thread: [RESOLVED] [2008] Removing last row datagridview

Hybrid View

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Resolved [RESOLVED] [2008] Removing last row datagridview

    Hi

    Okay I have an unbound DataGridView control which users enter as many records as is required. However I have some validation running which checks all cells to ensure data has been entered, however as you know, as soon as data is entered into a row, another appears, well I need to remove this row.

    I can't use
    vb Code:
    1. DataGridView1.Rows.RemoveAt(DataGridView1.Rows.Count - 1)

    As the row has not been committed, therefore it can't be deleted

    Also I need the 'enable adding' on, as without it rows will not appears.

    Help appreciated
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [2008] Removing last row datagridview

    You can't have it both ways. If you configure the grid to remove that row, which you can do, then the user will not be able to simply start typing to create a new row. You will have to provide code to add a new row manually. You need to decide whether that's OK.

    So, to remove that data entry row you simply set AllowUserToAddRows to False. A possibly better idea would be to simply test the IsNewRow property of the rows you're validating. If it's True you simply ignore the row.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2006
    Location
    UK
    Posts
    489

    Re: [2008] Removing last row datagridview

    Thanks for the reply, I came up with counting the rows, and then minus 1 per loop until the variable equals 1, then the loop exits.
    Learning C♯

    Data Binding & Bound Controls - Objects and wizards will never be as intelligent as you, do it yourself! (Unless your Pro)

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] [2008] Removing last row datagridview

    vb.net Code:
    1. For Each row As DataGridViewRow In myDataGridView.Rows
    2.     If Not row.IsNewRow Then
    3.         'Validate here.
    4.     End If
    5. Next

  5. #5
    New Member
    Join Date
    Feb 2012
    Posts
    1

    Re: [RESOLVED] [2008] Removing last row datagridview

    I think I've come up with a little bit different solution. As the previous man stated that the AllowUserToAddRows does prevent you from removing the last row.

    <code>
    If (DataGridView.Rows.Count = 1) Then
    DataGridView.AllowUserToAddRows = False
    DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index)
    DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index)
    DataGridView.AllowUserToAddRows = True
    Else
    DataGridView.Rows.RemoveAt(DataGridView.CurrentRow.Index)
    End If

    </code>

    No there is not a mistake in the first block of the if statement. The reason that its called twice is because (this is coded in button click) if you have the last row and you SELECT it then REMOVE it. The data just gets deleted if you dont call it twice. The reason I need to call it twice is to remove that row entirely, so if user hits add button, we dont have an empty row. So turning off the AllowUserToAddRows temporarily allows me to remove the last row.

  6. #6
    New Member
    Join Date
    Jan 2012
    Posts
    5

    Re: [RESOLVED] [2008] Removing last row datagridview

    this for this very simple code....i got it

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