Solved

VB.Net: Creating dynamic link to data source via a DataGridView control

Posted on 2014-01-03
9
605 Views
Last Modified: 2014-01-06
Within VB.Net, I am using a DataGridView, to which I am a newbie.  The DataGridView is connected to an Microsoft-Access dataset via a binding source, table adapter, and Dataset.  The running program consists of a single line of actual code that the program itself automatically writes to the Form_Load event.

Public Class _DataGrid_Control
    Private Sub _DataGrid_Control_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Data_DefinitionDataSet2.tbl_Role_Outfield' table. You can move, or remove it, as needed.
        Me.Tbl_Role_OutfieldTableAdapter.Fill(Me.Data_DefinitionDataSet2.tbl_Role_Outfield)
    End Sub
End Class

Open in new window

What shows up is an insular dataset.  If I change a value on the control, the native Access data remains unchanged.  I would like to make the connection dynamic, so that changes to control values reflect in the datasource.  How may I do this?  Thanks, ~Peter Ferber
0
Comment
Question by:PeterFrb
  • 5
  • 4
9 Comments
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
If you don't have the standard navigation control that is usually added.
try:
Make a save button add code behind click event.
Me.Data_DefinitionDataSet2.endedit()
Me.Tbl_Role_OutfieldTableAdapter.Update(Me.Data_DefinitionDataSet2.tbl_Role_Outfield)
0
 

Author Comment

by:PeterFrb
Comment Utility
What is the standard navigation control?  I likely have it but am unaware of its existence.

The dataset control does not have an EndEdit method associated with it.  For further clarity, I've included a screenshot of my screen in design mode, with the three data controls shown at the bottom. You have to see an enlargement at the bottom of the screenshot to see clearly the names of the data controls.  ~Peter

See the bottom of the screen for the names of the data-bound controls
0
 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
Take a look at my screenshot
ScreenCapture.jpg
0
 

Author Comment

by:PeterFrb
Comment Utility
Yes, thank you.  I have actually watched a YouTube video on this subject (thanks to http://www.youtube.com/watch?v=9CvQiPc3uSs).  Specifically, I have exploited the DataSource screen to automatically create a datagrid, including the "Save" button, as you have depicted in your screenshot, all of which represents significant progress from where I was.  The catch, still, however, is the insular quality of the dataset.  When I click the "Save" button, the change is reflected the next time I run the application, but it is clearly detached from the database itself.  The purpose of this is to update the Access source, not just the internal source contained within VB.Net.  The program, entirely written by the application, now contains the following code:

Public Class DataGrid_Control_Form
    Private Sub DataGrid_Control_Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Data_DefinitionDataSet.tbl_Role_Outfield' table. You can move, or remove it, as needed.
        Me.Tbl_Role_OutfieldTableAdapter.Fill(Me.Data_DefinitionDataSet.tbl_Role_Outfield)
    End Sub

    Private Sub Tbl_Role_OutfieldBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tbl_Role_OutfieldBindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Tbl_Role_OutfieldBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.Data_DefinitionDataSet)
    End Sub
End Class

Open in new window

I've made it three-quarters around the field, and committing these changes to the data source will take me across the finish line!  Thanks for the support thus far, and let us now bring this home.

Best, ~Peter Ferber
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 12

Expert Comment

by:ktaczala
Comment Utility
The DataAdapter serves as a bridge between a DataSet and a data source for retrieving and saving data. The DataAdapter provides this bridge by mapping Fill, which changes the data in the DataSet to match the data in the data source, and Update, which changes the data in the data source to match the data in the DataSet.

You say the source doesn't reflect the changes?  How are you looking at the source?  Do you have it open somewhere else?
0
 

Author Comment

by:PeterFrb
Comment Utility
No.  The Access file is closed at the time that I'm running the program, and the only code written is that which you see in my code listing above.  The UpdateAll clearly succeeds in updating the dataset within VB.Net, for when I close the application and reopen it, the changes are reflected within VB only.  When I open up Access, however, and look at the corresponding dataset that VB updated, the source remains unchanged.  I can actually demonstrate this directly with a screenshot.  I changed one value "Field_Order" from 200 to 2000 within the VB.Net dataset, closed and reopened, and I see the value persists.  Alongside that, I have the original Access table open, and it shows the original value of 200.  The change did not transfer over to the source.  This is as explicit an exasmple as I can muster.  ~Peter Ferber

Note "200" - "2000" discrepancy in VB.Net source and original data table
0
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
Comment Utility
In solution explorer; select your database, check the properties.
Make sure "Copy to Output Directory" is set to "copy if newer"
0
 

Author Comment

by:PeterFrb
Comment Utility
I am not in the office, so I will have to wait on trying this out; but before I do, I have a question.  When I make an edit to the table, it is saved, and the problem comes when VB.Net "reads" the data in order to populate the control.  From what I can see, it is not reading from the external source at all, but from its own insular dataset.  

The problem, as I see it, is twofold.  1) When the "Fill" process works, it only truly fills from Access the first time, when the connection is made.  If it "Filled" from Access, then the field in the control would revert from 2000 back to 200: the contents within Access.  That doesn't happen.  2) When I change the value in the control, it has no connection to the Access source.  If it did, then when I looked back at the Access value, I would see the 2000 value that I set in the control.  That doesn't happen.  Before I even try your proposed solution, please explain to me how changing the property of the file solves either of those problems.

Thanks, ~Peter Ferber
0
 

Author Closing Comment

by:PeterFrb
Comment Utility
My apologies: I misunderstood your answer.  I thought you were referring to Explorer and the definition of the file; and it was only when I got back to the office that I noticed that the Database connection has a property called "Copy to output Directory".  There are so many moving parts, it's difficult to track exactly to what one is referring.  A little more detail would be appreciated: the only reason I didn't give an "A" grade.  ~Peter
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now