DataGridView + Add Row
Hi.....
When I try to add a new row to a DataGridView in VS.net 2005, the new row is getting added with a row index of zero.
How do I get it added as the lower most row with max row index?
Thanks......
GeoNav
[244 byte] By [
GeoNav] at [2007-11-20 11:51:32]

# 1 Re: DataGridView + Add Row
Calculate it in the validate event:
Private Sub dataGridView1_RowValidating(ByVal sender As Object, ByVal e As DataGridViewCellCancelEventArgs) Handles dataGridView1.RowValidating
' Calculate the customer ID for offline mode. Currently this is done by handling
' the RowValidating event, but note that this event fires each time focus leaves
' a row. Currently the row index has to be special cased to decide when to perform
' calculate the customerID.
If (e.RowIndex = (dataGridView1.Rows.Count - 2)) AndAlso _
dataGridView1.Rows(e.RowIndex + 1).IsNewRow Then
Dim row As DataGridViewRow = dataGridView1.Rows(e.RowIndex)
' Only want to calculate the CustomerID if it hasn't been calculated.
If Not row.Cells("CustomerID").Value Is Nothing Then
If row.Cells("CustomerID").Value.ToString = "<AUTO>" Then
' CustomerID is calculated by taking the first 2 characters
' of the Company name and appending the RowIndex.
Dim coname As String = TryCast(row.Cells("Company").Value, String)
If Not (coname Is Nothing) Then
Dim coid As String = coname.ToUpper.Substring(0, Math.Min(2, coname.Length)) + e.RowIndex.ToString
row.Cells("CustomerID").Value = coid
e.Cancel = False
End If
End If
End If
End If
End Sub
this builds a string
# 2 Re: DataGridView + Add Row
How are you adding it in the first place? If you Add a row then it wil be the last row by default, so I'm guessing that you're specifically inserting it at index 0.
Also, is this grid bound or unbound? If it's bound to a DataSource then you shouldn't be adding a row to the grid at all, but rather adding an item to the data source.
# 3 Re: DataGridView + Add Row
How are you adding it in the first place? If you Add a row then it wil be the last row by default, so I'm guessing that you're specifically inserting it at index 0.
Also, is this grid bound or unbound? If it's bound to a DataSource then you shouldn't be adding a row to the grid at all, but rather adding an item to the data source.
I tried Datagridview.Rows.Add(datagridview.rowcount-1) as well as Datagridview.Rows.Add. both gave me same result.
The control is not bound.
Anyway I found a way to add them in place by specifying the cells and value and then add a row. But, I am not happy coz this asks for larger amount of code.
GeoNav at 2007-11-10 3:10:10 >
