I hope someone here can give me a clue. I have been developing forms which
use a datagrid to display some information from a dataset, the rest of the
information from the dataset is shown in various textboxes, comboboxes.....
I keep the datagrid in readonly until the user clicks on an Insert or Edit
button.
On Insert the following code works great.... Unless the dataset contains 0
rows.
In the form Load:
Load_SubRepo_Data()
Me.DataGrid1.DataSource = ds_SubRepo
Me.DataGrid1.DataMember = "SubRepo"
Dim ts1 As New DataGridTableStyle
ts1.MappingName = "SubRepo"
Dim myDataCol1 As New DataGridTextBoxColumn
With ts1.GridColumnStyles
ts1.GridColumnStyles.Add(New DataGridTextBoxColumn)
With .Item(0)
.HeaderText = "Description"
.MappingName = "descriptio"
.Width = 0.97 * Me.DataGrid1.Width '50 'Me.DataGrid1.Width
.NullText = String.Empty
End With
ts1.GridColumnStyles.Add(myDataCol1)
DataGrid1.TableStyles.Add(ts1)
End With
Me.DataGrid1.CaptionVisible = False
In the Insert button click:
Private Sub g1_Insert_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles g1_Insert.Click
Me.g1_Delete.Visible = False
Me.g1_Insert.Visible = False
Me.g1_Edit.Visible = False
Me.g1_Options.Visible = False
Me.g1_Custom.Visible = False
Me.g1_OK.Visible = True
Me.g1_Cancel.Visible = True
Me.DataGrid1.ReadOnly = False
Me.DataGrid1.AllowNavigation = True
Me.DataGrid1.Enabled = True
Try
Me.BindingContext(Me.ds_SubRepo.Tables(0)).EndCurrentEdit()
Me.BindingContext(Me.ds_SubRepo.Tables(0)).Position = Me.
BindingContext(Me.ds_SubRepo.Tables(0)).Count - 1
Me.BindingContext(Me.ds_SubRepo.Tables(0)).AddNew()
Me.BindingContext(Me.ds_SubRepo.Tables(0)).Position = Me.
BindingContext(Me.ds_SubRepo.Tables(0)).Count - 1
Dim NewRowDex As Integer = Me.BindingContext(Me.ds_SubRepo.Tables
(0)).Position
Dim z As Integer
For z = 0 To Me.ds_SubRepo.Tables(0).Columns.Count - 1
If Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"String" Then
Me.ds_SubRepo.Tables(0).Rows(NewRowDex)(z) = ""
ElseIf Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"Int32" Or _
Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"Double" Or _
Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"Single" Then
Me.ds_SubRepo.Tables(0).Rows(NewRowDex)(z) = 0
ElseIf Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"Boolean" Then
Me.ds_SubRepo.Tables(0).Rows(NewRowDex)(z) = False
ElseIf Me.ds_SubRepo.Tables(0).Columns(z).DataType.Name =
"DateTime" Then
Me.ds_SubRepo.Tables(0).Rows(NewRowDex)(z) = #1/1/1900#
Else
MsgBox("Unhandled datatype in Gsel Insert: " + Me.
ds_SubRepo.Tables(0).Columns(z).DataType.Name, MsgBoxStyle.Information +
MsgBoxStyle.OkOnly, "Need to Add code to DataGrid1 Insert")
End If
Next
Me.DataGrid1.CurrentRowIndex = Me.BindingContext(Me.ds_SubRepo.
Tables(0)).Count - 1
Me.DataGrid1.Refresh()
ds_SubRepo.Tables(0).Rows(Me.DataGrid1.CurrentRowIndex)
(ds_SubRepo.Tables(0).Columns.Count - 1) = GetaKey("SubRepo")
SubRepo_Enabler(True)
Catch ex As Exception
MsgBox("error" + ex.Message)
End Try
End Sub
As I said this works great as long as there is at least 1 row in the dataset
before clicking the insert button. Then the dataset is empty, I watch
ds_SubRepo.Tables(0).Rows.Count and it goes from 0 to 2 when the Me.
BindingContext(Me.ds_SubRepo.Tables(0)).AddNew() is executed. And Me.
DataGrid1.CurrentRowIndex remains at -1 when the Me.DataGrid1.
CurrentRowIndex = Me.BindingContext(Me.ds_SubRepo.Tables(0)).Count - 1
and It errors when the ds_SubRepo.Tables(0).Rows(Me.DataGrid1.CurrentRowIndex)
(ds_SubRepo.Tables(0).Columns.Count - 1) = GetaKey("SubRepo") statement is
executed stating that "there is no row at postion -1"
I'm at a loss. I can't understand why when there is 3 rows in the dataset
and I execute Me.BindingContext(Me.ds_SubRepo.Tables(0)).AddNew() I have 4
rows, but if I have 0 rows and I execute the same line of code I end up with
3 rows! I have many forms where I need to be able to handle empty dataset
and allow users to add a row.
Thanks,
Craig
--
Message posted via
http://www.dotnetmonster.com