Add new record in Database using details

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Hi everyone!

I have a small visual studio project that i have a database in. Instead of using the binding navigator - I want buttons to add a record / delete a record / update the database. I am good on everything except I am using details (which is all the text boxes) instead of a datagridview that I drag over from the DataSource window. I know if I was using a datagridview the code will be:

VB.NET:
        TestBindingSource.AddNew()

        Me.Datagridview1.DataSource = Me.TestBindingSource

If I am using "details" and I have all the text boxes would I just replace the "Datagridview1" with something else?

Thanks for your patients and thank you in advanced.

daveofgv
 
I think I may have figured out my own question...

Please let me know if this is correct or not.

I have to take out the below code:

VB.NET:
  Me.Datagridview1.DataSource = Me.TestBindingSource

and only keep:

VB.NET:
TestBindingSource.AddNew()

This added a new record in the primary key so I am assuming this is all I need.

Thanks in advanced.
 
You need to add your data bindings to each control. For each text box, you add your data binding during the form load. "yourTextBox.DataBindings.Add("text", yourBindingSource, "columnName")"

Add the appropriate code to your buttons.
 
Thank you for the reply... On the pane for data sources - you can drag and drop the table to the form and each text box is linked to the data binding... should I still use the above code or is this only if I drag a text box from the toolbox?
 
It works exactly the same way. In the Data Sources window, you can change the type of control(s) used for a particular table from grid view to details view. You can then drag the table onto the form and it will create TextBoxes and the like instead of a DataGridView. It will still create all the other components so it will still set up all the bindings for you. If you don't want the BindingNavigator then just delete it. You can create your own Buttons to invoke the appropriate members of the BindingSource, e.g. AddNew for adding and RemoveCurrent for deleting.

You can also change the type of control created for a column in the Data Sources window. If you need to, you can edit the bindings in the Properties window and you can also add your own controls and configure their bindings manually.

You can set up everything in code if you want to but, if your data access is all in the form anyway, why would you? You're just making life more complicated for yourself.
 
This can be marked resolved.

Yes it can, so you should do that. When you create a thread, you get the opportunity to select a prefix, which you should do EVERY time. If you're asking a question then select Question. When the question has been answered, edit the thread title and change the prefix to Answered.
 
Back
Top