Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-01-03
9
Medium Priority
?
627 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

688 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