Primary Key column show null in DataGrid

T

travlintom

Hi all.

I have a datagrid that has several columns including the Primary Key as
the first column.

When I add records to the grid the Primary Key column show "Null". This
would not be a problem, but I want my users to be able to delete the
column right away if they made a mistake with the entry. If the Primary
key is null I can't access the dataset to get the ID number of the row
to delete. Is there a way to refresh the datagrid immediately after
adding the row so I can see the Primary key value and not "null".
Thanks in advance.
Tom
 
K

Ken Tucker [MVP]

Hi,

Try setting a default value for the primary key column

http://msdn.microsoft.com/library/d...ystemdatadatacolumnclassdefaultvaluetopic.asp

Ken
-----------------------
Hi all.

I have a datagrid that has several columns including the Primary Key as
the first column.

When I add records to the grid the Primary Key column show "Null". This
would not be a problem, but I want my users to be able to delete the
column right away if they made a mistake with the entry. If the Primary
key is null I can't access the dataset to get the ID number of the row
to delete. Is there a way to refresh the datagrid immediately after
adding the row so I can see the Primary key value and not "null".
Thanks in advance.
Tom
 
T

travlintom

Thanks Ken. I read the article. Unfortunately, my datadgrid uses the
Autonumber as it's primary key. I did notice though that when I change
the values in the datagrid and then redisplay what I previously added,
the autonumber column is displayed. Is there a way to make this happen
without reopening the form? Or do I have to change the table so it
increments manually. Thanks.
 
C

Cor Ligthert [MVP]

Tom,

Your autonumber is not in the datagrid, it is in the datatable (as I assume
it normally is).

Have a look at the seed and increment from that.
http://msdn.microsoft.com/library/d...stemdatadatacolumnclassautoincrementtopic.asp

It is adviced to set the seed to a negative value.

The official autonumber will be set at the update time, what you see is not
the real primary key.

That key is to get using a execute.scalar for the @@Identity (before you ask
it)

I hope this helps,

Cor
 

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