Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-01-03
9
Medium Priority
?
632 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 13

Expert Comment

by:ktaczala
ID: 39755064
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
ID: 39755168
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 13

Expert Comment

by:ktaczala
ID: 39755206
Take a look at my screenshot
ScreenCapture.jpg
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:PeterFrb
ID: 39755428
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
 
LVL 13

Expert Comment

by:ktaczala
ID: 39755463
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
ID: 39755479
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 13

Accepted Solution

by:
ktaczala earned 1500 total points
ID: 39755490
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
ID: 39757966
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
ID: 39760641
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

916 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