How to prevent new row in datagrid - prevent new row * icon?

G

Guest

I fill a dataset like this:

dim da As New SqlDataAdapter, ds As New Dataset, conn As New SqlConnection
....
da.Fill(ds, "tbl1")
datagrid1.DataSource = ds
datagrid1.DataMember = "tbl1"

When the form containing datagrid1 displays, all the records are there. The
dataAdapter, da, is based on a stored procedure which only returns a set
number of records from tbl1 on Sql Server. These records are updatable. But
I noticed that there is a new row icon * at the last row of the datagrid. I
can tab into that new row. How can I configure datagrid1 (or the dataset -
whichever) so that when I tab to the end of the last row with data it goes
back to the first row and there is no new row in the datagrid? Is there a
property setting in datagrid1 that I need to set? Or do I just have to write
code? I can see writing code to make it go back to the first record - use
currency manager. But how can I prevent the new row icon * from showing up
in datagrid1?

Thanks,
Rich
 
K

Ken Tucker [MVP]

Hi,

ds.Tables("tbl1").defaultview.allownew=false

Ken
----------------------
I fill a dataset like this:

dim da As New SqlDataAdapter, ds As New Dataset, conn As New SqlConnection
....
da.Fill(ds, "tbl1")
datagrid1.DataSource = ds
datagrid1.DataMember = "tbl1"

When the form containing datagrid1 displays, all the records are there. The
dataAdapter, da, is based on a stored procedure which only returns a set
number of records from tbl1 on Sql Server. These records are updatable.
But
I noticed that there is a new row icon * at the last row of the datagrid. I
can tab into that new row. How can I configure datagrid1 (or the dataset -
whichever) so that when I tab to the end of the last row with data it goes
back to the first row and there is no new row in the datagrid? Is there a
property setting in datagrid1 that I need to set? Or do I just have to
write
code? I can see writing code to make it go back to the first record - use
currency manager. But how can I prevent the new row icon * from showing up
in datagrid1?

Thanks,
Rich
 
G

Guest

Thanks very much for your reply. I tried placing your code as follolws:

Private Sub Form1_Load(...)
....
da.Fill(ds, "tbl1")
ds.Tables("tbl1").defaultview.allownew=false
datagrid1.DataSource = ds
datagrid1.DataMember = "tbl1"
--and also tried placing after
ds.Tables("tbl1").defaultview.allownew=false

But I still have the ability to add new records. The problem with this is
with the da.UpdateCommand. If I add new records by accident and try to
update the dataset, I get an error, that I need an insert command. Your code
seems logical, but I think I am missing something preliminary to it. Maybe I
need to set a property in the property sheet of the datagrid? I am using a
datagrid control from the toolbox.

Thanks again for your reply.

Rich
 
G

Guest

OK. The trick that worked for me was to use a dataview as the datasource for
the datagrid.

Dim dv1 As New Dataview(ds.Tables("tbl1")
dv1.AllowNew = False
dgr1.Datasource = dv1

Now I don't have the * new record symbol. Your suggestion works perfectly!
Thanks very much for your help.
 
C

Cor Ligthert

OK. The trick that worked for me was to use a dataview as the datasource
for
the datagrid.

Dim dv1 As New Dataview(ds.Tables("tbl1")
dv1.AllowNew = False
dgr1.Datasource = dv1

Now I don't have the * new record symbol. Your suggestion works
perfectly!
Thanks very much for your help.
However the sample that Ken gave you was in my opinion slightly better. And
then of course
\\\
dgr1.Datasource = ds.Tables("tbl1").defaultview
///
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