Improve company productivity with a Business Account.Sign Up

x
?
Solved

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

Posted on 2014-01-03
9
Medium Priority
?
647 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Watch the video to know how one can repair corrupt Exchange OST file effortlessly and convert OST emails to MS Outlook PST file format by using Kernel for OST to PST converter tool. It can convert OST to MSG, MBOX, EML to access them. It can migrate…
Watch the software video of Kernel Import PST to Office 365 tools which can easily import PST and OST files to Office 365 for bulk mailboxes. The process of migration is simple and user can map source and destination mailboxes and easily import data…

584 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