Tired of this datagridview error for 3 weeks. Need Help!!!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a datagridview which is linked to a table which has 3 fields with the
primary key being an autoincrement (tinyint). I use the wizard to create
sqldataadapter, dataset and datagridview. Now when I try to enter second row,
I get error : "PrimaryKey1 cannot be null". (Primarykey1 is the name of
primary key). I had this error with several other forms and by deleting
tables, stored procedures, sqldataadapter, dataset, datagridview and form, it
then works! What is going on here? Does it work sometimes? There seems to be
no logic as to why this happens. I can't be deleting all this data if it does
not work and then hope it the does later. Is there some quirk with
datagridview?
 
I reckon there are a lot of quirks with DataGridView. If you trace through what
is called when, there seem to be different cases for an empty table, a table
with one row, and other cases.

Anyway, try this (not guaranteed)

In Grid_CellFormatting (if you use it)

if ( myGridView.Rows [ e.RowIndex ].IsNewRow )
{
if ( e.RowIndex > myTable.Rows.Count )
return;
}
else
{
if ( e.RowIndex >= myTable.Rows.Count )
return;
}

You may also have to put in Grid_RowEnter ( if you use it )

if ( e.RowIndex >= myDataGridView.Rows.Count - 1 )
return;

There may be other cases.

I think the problem arises as to when DGV updates its row Count property.

(You also need the _DefaultValuesNeeded exit to setup defaults in your cells)
 
Could you use the DataViewGrid's DefaultValuesNeeded event to populate
the row's primary key with sensible values?

When using RowIndex I found problems in the DataGrid class as if you
allow sorting the RowIndex in the DataGrid will not match the index in
the table. Also new rows are created as detached, thus havn't currently
been added to the data table yet, thus they will most likely have an
index greater than the count of the table.

The way round this was achived by using Control.BindingContext to get a
currency manager, you could use this to detect a change of row and in
the change test if the row was detached (and thus new). It seems the
DefaultValuesNeeded and other such events replace a lot of the need for
the BindingContext if you're working with data view grids.


I reckon there are a lot of quirks with DataGridView. If you trace through what
is called when, there seem to be different cases for an empty table, a table
with one row, and other cases.

Anyway, try this (not guaranteed)

In Grid_CellFormatting (if you use it)

if ( myGridView.Rows [ e.RowIndex ].IsNewRow )
{
if ( e.RowIndex > myTable.Rows.Count )
return;
}
else
{
if ( e.RowIndex >= myTable.Rows.Count )
return;
}

You may also have to put in Grid_RowEnter ( if you use it )

if ( e.RowIndex >= myDataGridView.Rows.Count - 1 )
return;

There may be other cases.

I think the problem arises as to when DGV updates its row Count property.

(You also need the _DefaultValuesNeeded exit to setup defaults in your cells)


I have a datagridview which is linked to a table which has 3 fields with the
primary key being an autoincrement (tinyint). I use the wizard to create
sqldataadapter, dataset and datagridview. Now when I try to enter second row,
I get error : "PrimaryKey1 cannot be null". (Primarykey1 is the name of
primary key). I had this error with several other forms and by deleting
tables, stored procedures, sqldataadapter, dataset, datagridview and form, it
then works! What is going on here? Does it work sometimes? There seems to be
no logic as to why this happens. I can't be deleting all this data if it does
not work and then hope it the does later. Is there some quirk with
datagridview?
 
I'm sure that buried in the seemingly hundreds of combinations of events for the
datatable, datagridview, binding context etc there is a simpler way.

What I am doing, is just starting from an empty table, trying to allow the user
to just keep on entering data and tabbing between fields and automatically
adding new records as they tab out of one record into another without having to
click an add or save button as supplied by the wizard-generated code for a typed
dataset.

I found I had to add the previous code to get it to work.

Chris said:
Could you use the DataViewGrid's DefaultValuesNeeded event to populate
the row's primary key with sensible values?

When using RowIndex I found problems in the DataGrid class as if you
allow sorting the RowIndex in the DataGrid will not match the index in
the table. Also new rows are created as detached, thus havn't currently
been added to the data table yet, thus they will most likely have an
index greater than the count of the table.

The way round this was achived by using Control.BindingContext to get a
currency manager, you could use this to detect a change of row and in
the change test if the row was detached (and thus new). It seems the
DefaultValuesNeeded and other such events replace a lot of the need for
the BindingContext if you're working with data view grids.


I reckon there are a lot of quirks with DataGridView. If you trace through what
is called when, there seem to be different cases for an empty table, a table
with one row, and other cases.

Anyway, try this (not guaranteed)

In Grid_CellFormatting (if you use it)

if ( myGridView.Rows [ e.RowIndex ].IsNewRow )
{
if ( e.RowIndex > myTable.Rows.Count )
return;
}
else
{
if ( e.RowIndex >= myTable.Rows.Count )
return;
}

You may also have to put in Grid_RowEnter ( if you use it )

if ( e.RowIndex >= myDataGridView.Rows.Count - 1 )
return;

There may be other cases.

I think the problem arises as to when DGV updates its row Count property.

(You also need the _DefaultValuesNeeded exit to setup defaults in your cells)


I have a datagridview which is linked to a table which has 3 fields with the
primary key being an autoincrement (tinyint). I use the wizard to create
sqldataadapter, dataset and datagridview. Now when I try to enter second row,
I get error : "PrimaryKey1 cannot be null". (Primarykey1 is the name of
primary key). I had this error with several other forms and by deleting
tables, stored procedures, sqldataadapter, dataset, datagridview and form, it
then works! What is going on here? Does it work sometimes? There seems to be
no logic as to why this happens. I can't be deleting all this data if it does
not work and then hope it the does later. Is there some quirk with
datagridview?
 
I found out the error. The error is with the dataset. It seems that when you
create the dataset on a sql that is autonumber, at times the dataset
property's autonumber is set to true and sometimes false! How this is
determined, I'm unsure. But once you set the dataset property's autonumber to
true, it will be OK. Thank you guys for ur assistance.
L. A. Jones


Ian Semmel said:
I'm sure that buried in the seemingly hundreds of combinations of events for the
datatable, datagridview, binding context etc there is a simpler way.

What I am doing, is just starting from an empty table, trying to allow the user
to just keep on entering data and tabbing between fields and automatically
adding new records as they tab out of one record into another without having to
click an add or save button as supplied by the wizard-generated code for a typed
dataset.

I found I had to add the previous code to get it to work.

Chris said:
Could you use the DataViewGrid's DefaultValuesNeeded event to populate
the row's primary key with sensible values?

When using RowIndex I found problems in the DataGrid class as if you
allow sorting the RowIndex in the DataGrid will not match the index in
the table. Also new rows are created as detached, thus havn't currently
been added to the data table yet, thus they will most likely have an
index greater than the count of the table.

The way round this was achived by using Control.BindingContext to get a
currency manager, you could use this to detect a change of row and in
the change test if the row was detached (and thus new). It seems the
DefaultValuesNeeded and other such events replace a lot of the need for
the BindingContext if you're working with data view grids.


I reckon there are a lot of quirks with DataGridView. If you trace through what
is called when, there seem to be different cases for an empty table, a table
with one row, and other cases.

Anyway, try this (not guaranteed)

In Grid_CellFormatting (if you use it)

if ( myGridView.Rows [ e.RowIndex ].IsNewRow )
{
if ( e.RowIndex > myTable.Rows.Count )
return;
}
else
{
if ( e.RowIndex >= myTable.Rows.Count )
return;
}

You may also have to put in Grid_RowEnter ( if you use it )

if ( e.RowIndex >= myDataGridView.Rows.Count - 1 )
return;

There may be other cases.

I think the problem arises as to when DGV updates its row Count property.

(You also need the _DefaultValuesNeeded exit to setup defaults in your cells)



Dave wrote:

I have a datagridview which is linked to a table which has 3 fields with the
primary key being an autoincrement (tinyint). I use the wizard to create
sqldataadapter, dataset and datagridview. Now when I try to enter second row,
I get error : "PrimaryKey1 cannot be null". (Primarykey1 is the name of
primary key). I had this error with several other forms and by deleting
tables, stored procedures, sqldataadapter, dataset, datagridview and form, it
then works! What is going on here? Does it work sometimes? There seems to be
no logic as to why this happens. I can't be deleting all this data if it does
not work and then hope it the does later. Is there some quirk with
datagridview?
 

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

Back
Top