Problem with new rows in a datagrid

  • Thread starter Thread starter Derek Chong
  • Start date Start date
D

Derek Chong

Hi all,

How do I stop the grid control from trying to save a new row to the
datatable rows collection when I change the selected cell on the new row.
It is only a problem with mandatory columns. After adding a new row,
clicking on the Date_Recorded cell of the new row raises the following error

An unhandled exception of type 'System.Data.NoNullAllowedException' occurred
in System.Data.dll
Additional information: Column 'Id' does not allow nulls.

Here is a code snippet that will cause the problem

Dim dtInventory As DataTable

Private Sub frmTest_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
dtInventory = New DataTable

'Define inventory table
dtInventory.Columns.Add("Id", GetType(Integer))
dtInventory.Columns!Id.AllowDBNull = False
dtInventory.Columns.Add("Date_Recorded", GetType(Date))
dtInventory.Columns!Date_Recorded.DefaultValue = Now
dtInventory.Columns!Date_Recorded.AllowDBNull = False

grdData.DataSource = dtInventory
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnAdd.Click
Me.BindingContext(dtInventory).AddNew()
End Sub

Pocket PC 2003
CF SR1
 
Thanks for that Alex but I will also need to handle situations where I want
users to enter a mandatory value and starting with a default value would be
inappropriate.

Derek
 
I see the problem.
Unfortunately the record has to be added to the table first in order for it
to be displayed by a datagarid bound to that table. This means that you have
a choice:
1) Either provide a value that would prepopulate the field
2) Or do not mark fields as NOT NULL

The reason is that being a limited implementation, CF DataGrid does not
support adding records internally. On the desktop you are using the fact
that the new record created via datagrid does not really exist in the
underlying dataset, untill commited. In CF it has to be in the dataset - the
datagrid is a display-only control
 
Thought that might be the case. Will just have to remove the allow Nulls
and check for them separatley. Thanks for you feed back Alex.

Derek
 
Back
Top