-
Dec 11th, 2008, 04:10 PM
#1
Thread Starter
Hyperactive Member
[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:
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)
-
Dec 11th, 2008, 05:35 PM
#2
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.
-
Dec 11th, 2008, 08:21 PM
#3
Thread Starter
Hyperactive Member
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)
-
Dec 11th, 2008, 08:36 PM
#4
Re: [RESOLVED] [2008] Removing last row datagridview
vb.net Code:
For Each row As DataGridViewRow In myDataGridView.Rows If Not row.IsNewRow Then 'Validate here. End If Next
-
Feb 7th, 2012, 10:18 AM
#5
New Member
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.
-
Nov 27th, 2012, 10:04 PM
#6
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|