-
Feb 9th, 2018, 04:53 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Datagridview new row and prevent multiple blank rows
-
Feb 9th, 2018, 06:22 AM
#2
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.
-
Feb 9th, 2018, 08:32 AM
#3
Fanatic Member
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.
-
Feb 9th, 2018, 08:36 AM
#4
Fanatic Member
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
-
Feb 14th, 2018, 06:10 AM
#5
Thread Starter
Fanatic Member
Re: Datagridview new row and prevent multiple blank rows
thank you kpmc! I shall try this
-
Feb 14th, 2018, 09:26 AM
#6
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|