I am having a problem disabling the automatic add row functionality on a Datagrid that is displaying related child records (from a dataset relation). First I am selecting Customers and Orders from the Northwind database and setting a relation between them in the dataset.
da = New OleDbDataAdapter("SELECT * FROM Customers", cnn)
da = New OleDbDataAdapter("SELECT * FROM Orders", cnn)
Then I bind the data to my controls on the form:
Me.txtCompany.DataBindings.Add("Text", myDataSet, "Customers.CompanyName")
Me.txtCustID.DataBindings.Add("Text", myDataSet, "Customers.CustomerID")
Me.txtContact.DataBindings.Add("Text", myDataSet, "Customers.ContactName")
Then I grab references to the Currency Managers into some form variables:
Me.CmOrders = DirectCast(Me.grdOrdersGrid.BindingContext(Me.grdOrdersGrid.DataSource, Me.grdOrdersGrid.DataMember), CurrencyManager)
Me.CmCustomer = DirectCast(Me.BindingContext(myDataSet, "Customers"), CurrencyManager)
Then I set AllowNew to false on the Datagrid's DataView:
DirectCast(Me.CmOrders.List, DataView).AllowNew = False
Okay! Great that all works fine. The asterik (*) at the bottom of the grid does not display and I am not able to add records to the child. UNTIL....
I have buttons on the form for navigating through the Customers (Master) by setting the position on the customer Currency Manager. For instance, when I click the Next button on my form I issue the following:
Me.CmCustomer.Position += 1
It works well, the customer controls show the new record and the datagrid updates to show the related records. BUT.. the asterik is back! The datagrid now allows automatic adds again! I set breakpoints and look at the AllowNew property and it is still set to False. I even tried setting it again and again after the position change on the customer.. still no luck!
Okay I figured it out! I found out that the child data view is recreated every time the position is changed in the parent data view so I added an Event Handler on my Customer Currency Manager to reset the DataView AllowNew property to false:
Private Sub CmCustomer_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmCustomer.PositionChanged
Private Sub SetOrdersView()
Me.DvOrders = DirectCast(Me.CmOrders.List, DataView)
Me.DvOrders.AllowNew = False
Thanks anyways! :-)
CHchinaAugust 2, 2003 08:27 PM
Hi, i have a problem that how can i read a picture file from the openfileDialog to the Picturebox. which class I can use?Streamread or filestream?
JNJohnny NguyenSeptember 8, 2003 09:26 AM
I'm also working on this sample Customer-Orders detail and I could not make the AddNew function to not add a new row to the datagrid ( the asterisk). Can you post all the code that you have so I can take a look at them. And also do you know how to add a checkbox or textbox to both the master and the detail grids?
Thanks a lot