VS 2012 Datagridview new row and prevent multiple blank rows-VBForums
Results 1 to 6 of 6

Thread: Datagridview new row and prevent multiple blank rows

  1. #1

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    629

    Datagridview new row and prevent multiple blank rows

    Hi, I have two tables that is one to many. I have notice that users work with DGV's like MS Excel.. In DGV they leave many blank rows and this looks bad in reports..

    So I have change DGV's to "Allow users to add rows" to FALSE and insert button where user must now click to add row:

    Code:
    MyBindingSource.AddNew()
    So this is better. And looks like this:

    Name:  One.JPG
Views: 121
Size:  16.7 KB


    When user click button:

    Name:  two.JPG
Views: 115
Size:  17.5 KB

    Again this is better. But now what if user keeps clicking button? I not want this:

    Name:  three.JPG
Views: 122
Size:  19.3 KB

    How can I:

    1- Prevent insert more blank row if there blank row already? (when button is click)
    2- Because there is 2 tables (one-to-many) there is 2 DGV's I want prevent save (if save button is click) if there is blank rows in any of this 2 DGV's

    Thanks
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    5,165

    Re: Datagridview new row and prevent multiple blank rows

    Hi.

    For the first issue, you can get the previous row value cell . Haven't look at t but something like that, give or take a property:
    DGV.Rows[CurrentRowIndex-1].Cells[0].EdititedFormattedValue.ToString() . Careful not to try this on the first row as you will get out of index range. So if the value is empty then on the event you are using, i guess there must be an e.cancel ? Or do what you want to no let the add new row code run.

    For the second issue an iteration to every, row(index)(cell) value to see if you have blank rows. Now if you have millions of rows that might be a problem though. You may need to use paging.
    Slow as hell.

  3. #3
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    344

    Re: Datagridview new row and prevent multiple blank rows

    Code:
    Public Class Form28_DataGridView
        Dim dt As New DataTable
        Dim bs As New BindingSource
        Private Sub Form28_DataGridView_Load(sender As Object, e As EventArgs) Handles MyBase.Load      
            dt.Columns.Add("Col1", GetType(System.String))
            bs.DataSource = dt
            With DataGridView1
                .DataSource = bs
                .AllowUserToAddRows = False
            End With
        End Sub
    
        Private Sub ButtonAddRow_Click(sender As Object, e As EventArgs) Handles ButtonAddRow.Click
            If bs.Count <= 0 Then
                bs.AddNew()
            ElseIf CType(bs.Current, DataRowView)("Col1").ToString.Length > 0 Then
                bs.AddNew()
            End If
        End Sub
    
        Private Sub ButtonSave_Click(sender As Object, e As EventArgs) Handles ButtonSave.Click
            For Each DRV As DataRowView In bs
                If DRV("col1").ToString.Length <= 0 Then
                    MsgBox("Blank rows cant save")
                Else
                    'save
                End If
            Next
        End Sub
    End Class
    Last edited by kpmc; Feb 9th, 2018 at 08:58 AM.

  4. #4
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    344

    Re: Datagridview new row and prevent multiple blank rows

    forgot about a condition...

    Code:
        Private Sub ButtonAddRow_Click(sender As Object, e As EventArgs) Handles ButtonAddRow.Click
            If bs.Count <= 0 Then
                bs.AddNew()
            ElseIf CType(bs.Current, DataRowView)("Col1").ToString.Length > 0 And CType(bs(bs.count - 1), DataRowView)("Col1").ToString.Length > 0 Then
                bs.AddNew()
            End If
        End Sub

  5. #5

    Thread Starter
    Fanatic Member schoemr's Avatar
    Join Date
    Apr 2016
    Location
    South Africa
    Posts
    629

    Re: Datagridview new row and prevent multiple blank rows

    thank you kpmc! I shall try this
    Don't miss the whole point of the dance...

    https://www.youtube.com/watch?v=qHnIJeE3LAI

  6. #6
    Hyperactive Member
    Join Date
    Sep 2017
    Posts
    344

    Re: Datagridview new row and prevent multiple blank rows

    Should work out for you.

    Also I just noticed you will need an exit on your save procedure

    Code:
            For Each DRV As DataRowView In bs
                If DRV("col1").ToString.Length <= 0 Then
                    MsgBox("Blank rows cant save")
                    Exit For 
                Else
                    'save
                End If
            Next

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width