DataGrid new record

G

GraemeR

Hi, how can I set a datagrid control to display the new record row? the code
I'm using crashes the .Net Compact Framework. (Also, the new record row is
automatically displayed in the windows datagrid control)

This is how I'm binding the data to my grid - records are displayed (though
I can't edit them yet)

' Module level variables
Private mDataAdapter As SqlServerCe.SqlCeDataAdapter
Private mDataTable As DataTable
mDataTable = New System.Data.DataTable("MyTable")
mDataAdapter.Fill(mDataTable)
grdDataGrid.DataSource = mDataTable

I've tried:

Private mBindingManagerBase As BindingManagerBase
mBindingManagerBase = grdDataGrid.BindingContext(mDataTable)
mBindingManagerBase.AddNew()

but it crashes the .Net framework.

As does:

Private mCurrentcyManager As CurrencyManager
mCurrentcyManager = CType(grdDataGrid.BindingContext(mDataTable),
CurrencyManager)
mCurrentcyManager.AddNew()

Thanks, Graeme.
 
I

Ilya Tumanov [MS]

If you see IndexOutOfRange exception, it's a known bug in the DataGrid.
You can add rows to the underlying DataTable instead as a workaround:

mDataTable.Rows.Add (...)

However, do not delete rows in the DataTable directly using grid's row
index.
Use Delete() method of the DataView this grid is bound to (in this case
mDataTable.DefaultView) instead:

mDataTable.DefaultView.Delete(grdDataGrid.CurrentRowIndex)

Also, CF grid is read only, you can't edit anything in it.
Common practice would be to pop up a dialog to add/edit rows.

Best regards,

Ilya

This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
 
G

GraemeR

Hi, the code is not crashing on a particular statement.
The procedure to create the record exists correctly (I've stepped through it
with debugger

Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAddNew.Click
Try

mbmbBindingManagerBase = grdFruit.BindingContext(mdtDataTable)
mbmbBindingManagerBase.AddNew()

Catch ex As Exception

MessageBox.Show(ex.ToString)

End Try
End Sub

and then debugger jumps to application 'instantiating' call:

Public Shared Sub Main()
Application.Run(New frmMain)
End Sub

The error message from trapping error in Sub Main is:

System.IndexOutOfRangeExeption:Index 1 is not non-negative and below total
rows count.

Cheers, Graeme.
 
G

GraemeR

Thanks Ilya, from my reply in the previous branch in this thread,
IndexOutOfRange exception looks like the problem I'm encountering.

Heads up on the datagrid being read only is also useful. I can feel this is
going to be a long night.

Cheers, Graeme.
 
G

GraemeR

Thanks Sitar, I've implemented something similar with

Private mdgc AS DataGridCell

Private Sub grdFruit_CurrentCellChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles grdFruit.CurrentCellChanged

' Update DataTable with changes (there's checking code not shown)
mDataTable.Rows(mdgc.RowNumber).Item(mdgc.ColumnNumber) =
txtFruitData.Text
txtFruitData.ReadOnly = True


' Get current cell
mdgc = grdFruit.CurrentCell
txtFruitData.Text =
mDataTable.Rows(mdgc.RowNumber).Item(mdgc.ColumnNumber).ToString

End Sub

Walk, walk, walk, ...

Graeme.
 
G

Guest

Looks like you're directly matching the row of your datagrid with the row of
the underlying datatable. This is a bit risky if you want to do some sorting
or if you filter the displayed data later (let's say by selecting values from
a combo). You should consider the use of a dataview.
This link was given in a previous post (not mine) and explains things quite
well I think:
http://www.codeproject.com/useritems/DBGridCurrentRow.asp

Cheers,
Sitar.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top