Solved

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

Posted on 2014-01-03
9
616 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 12

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 12

Expert Comment

by:ktaczala
ID: 39755206
Take a look at my screenshot
ScreenCapture.jpg
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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 12

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 12

Accepted Solution

by:
ktaczala earned 500 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

751 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