Results 1 to 6 of 6

Thread: [RESOLVED] Datagridview new row and prevent multiple blank rows

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Resolved [RESOLVED] 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:

    Attachment 156095


    When user click button:

    Attachment 156097

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

    Attachment 156099

    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

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,597

    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.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

  3. #3
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Datagridview new row and prevent multiple blank rows

    thank you kpmc! I shall try this

  6. #6
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    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
  •  



Click Here to Expand Forum to Full Width