473,408 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Databinding - How does it work, and where do I start?

36
I have a database in Access 2007, and a GUI in VB.NET 3.5. Where should I go to start learning about databinding, and how to have the front end manipulate the database?

I would love to find some learning resources for coding and concepts that is more concise/condensed than random forums, Google, and the MSDN developer references.

Most of these questions will be about databinding, datasets, querying, etc. The several general questions I have to start the learning process are:
  1. Is there a way to move the Access file without needing to manually make a new connection string? (ie, how to change the connection string, or reload the database to allow communication with new tables and queries.)
  2. How do I go about querying the database dataset adapter to get a sub-set of data?
  3. How do I send changes back to the database, to update a set of tables based on foriegn keys and unique IDs?
  4. How do I find the ID of a selected data grid view row?
  5. The list view looks nicer than the data grid view. Would it have the same functionality as the data grid view, or is that too hard for a newbie?

Thanks, please don't hesitate to direct me elsewhere for answers if something has already been covered.

Chris
Dec 19 '08 #1
2 2819
G'Day,
I don't use data binding anymore. I find it more flexible to manually control the data being displayed. This is more tricky as you need to manually keep track of position in the data rows and handle end of records etc. However, to answer some of your questions:

1. Not sure. I don't think so. Maybe have an options form where the user can browse for the file if they move it (otherwise, have a default value).
2. You could try running an SQL query on the database and either loading this into another dataset or clearing the existing dataset and reloading it.
3. If you're using databinding, any changes you make should be automatically updated in the database (not sure if this is still the case with later versions of VB.NET).
4. You could add another column with a width of 0 and place the row ID in this. Then you just need to get the selected column and read the data from it.This is how I do it.
5. With the list view, you'll need to handle everything manually. This can be tricky but once again it probably provides you with the greatest flexibility. It would be rather difficult for a newbie.

I hope this helps.

Cheers
Dec 23 '08 #2
Infog
36
What I've learned so far:
Databinding using the automatic binding in VB.NET is a mess. It is simpler, in a way, to just query the database yourself, save that to a dataset/table somewhere, and referance the stored table whenever you need data.

1. To move the Access database and still have the .NET program connect:
Use My.Settings.whatever in order to store the filepath.
Expand|Select|Wrap|Line Numbers
  1.     'Change database location
  2.     Private Sub btnDbChange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDbChange.Click
  3.         'I have no idea... Where did I even find this code online??
  4.         Dim fdlg As OpenFileDialog = New OpenFileDialog()
  5.         fdlg.Title = "Select the database"
  6.         fdlg.InitialDirectory = "c:\"
  7.         fdlg.Filter = "Access 2007 database files (*.accdb)|*.accdb"
  8.         fdlg.FilterIndex = 1
  9.         fdlg.RestoreDirectory = True
  10.         If fdlg.ShowDialog() = DialogResult.OK Then
  11.             Me.txtDbLoca.Text = fdlg.FileName
  12.         End If
  13.     End Sub
2. How to query an Access database to get a table?
Expand|Select|Wrap|Line Numbers
  1.     Public Function FillFromAccess(ByVal QueryString As String, ByVal FillTable As String) As DataSet
  2.         'Get data from a local Access database
  3.         Dim OLEConnString As String
  4.         OLEConnString = "Provider=" & My.Settings.Connection_AccessProvider & ";" _
  5.             & "Data Source=" & My.Settings.Connection_Location & ";"
  6.  
  7.         Dim DBConn As New OleDbConnection(OLEConnString)
  8.         Dim DBCommand As OleDbDataAdapter
  9.         Dim dataset_Access As New DataSet
  10.  
  11.         'Pull a table out of the database.
  12.         DBCommand = New OleDb.OleDbDataAdapter(QueryString, DBConn)
  13.         Try 'Put that into an object
  14.             DBCommand.Fill(dataset_Access, FillTable)
  15.         Catch ex As Exception
  16.             FillFromAccess = Nothing
  17.             Exit Function
  18.         End Try
  19.  
  20.         'Send the dataset back to the sub that called this function!
  21.         Return dataset_Access
  22.     End Function
3. Update the database when I need to make changes?
Expand|Select|Wrap|Line Numbers
  1. Public Sub SendToAccess(ByVal QueryString As String)
  2.         'Insert, Update, or Delete data in a local Access Database table
  3.  
  4.         'http://www.eggheadcafe.com/community/aspnet/14/70996/insert-into-statement-ol.aspx
  5.         Dim DBCommand As New OleDb.OleDbCommand(QueryString, New OleDb.OleDbConnection("Provider=" & My.Settings.Connection_AccessProvider & ";" & "Data Source=" & My.Settings.Connection_Location & ";"))
  6.  
  7.         DBCommand.Connection.Open()
  8.         Try
  9.             DBCommand.ExecuteNonQuery()
  10.         Catch ex As Exception
  11.         End Try
  12.         DBCommand.Connection.Close()
  13.         DBCommand.Dispose()
  14.         DBCommand = Nothing
  15.     End Sub
4. How do I find the ID of a record when it is in a DataGridView?
Expand|Select|Wrap|Line Numbers
  1. '* Find if a row is selected
  2.         'http://msdn.microsoft.com/en-us/library/x8x9zk5a.aspx
  3.         Dim selectedRowCount As Integer = Me.dgvTasks.Rows.GetRowCount(DataGridViewElementStates.Selected)
  4.         If selectedRowCount <= 0 Then Exit Sub
  5.  
  6.         ' Task ID
  7.         Try
  8.             intTaskID = Me.dgvTasks.SelectedRows(0).Cells.Item("ID").Value
  9.         Catch ex As Exception
  10.         End Try
5. Why not use the ListView instead of the DataGridView?
Well... a ListView requires you to do more coding to make it look and behave correctly. AND - you can change a dgv to look like a ListView. Look for the properties AutoSizeColumnsMode, GridColor, and SelectionMode. If you put this code in a public module, you will be able to change all DataGridViews to look the same, with one line of code for each, when it's form loads:
Expand|Select|Wrap|Line Numbers
  1.     Public Sub dgvRowColors(ByVal dgv As DataGridView)
  2.         'http://www.dotnetspider.com/resources/23026-GridView-FAQ-s.aspx
  3.         With dgv
  4.             .RowsDefaultCellStyle.BackColor = Color.White
  5.             .AlternatingRowsDefaultCellStyle.BackColor = Color.LightGray
  6.             .GridColor = Color.Gray
  7.         End With
  8.     End Sub
That's all! I hope that someone finds this and puts it to use. Please leave a comment here if this helped you.
Feb 27 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: Tim Jarvis | last post by:
Hi, I have an object that I am binding to a text box, this object exposes a boolean field, and I have implemented a format event handler and a parse event handler for the binding object, where I...
4
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox),...
2
by: Mr Newbie | last post by:
Ive got the basic idea behind DataBinding Expressions, but I have a couple of questions, Ok its four actually :) Q1.) It would appear that if you use Page.DataBind that all child controls and...
8
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
5
by: Peter M. | last post by:
I'm struggling with combobox databinding with something I consider a bug... I'm binding my combobox to an array of structs. The struct exposes two public properties, ID and Name, to be used as...
5
by: Mark R. Dawson | last post by:
Hi all, I may be missing something with how databinding works but I have bound a datasource to a control and everything is great, the control updates to reflect the state of my datasource when I...
9
by: Nathan Sokalski | last post by:
I have a very simple UserControl which contains an Image and a Label, which I use to display an image with a caption. I am using this control inside a DataList, setting the two Public variables...
1
by: Owen Blacker | last post by:
I've spent loads of time Googling to try to work this one out and I'm sure it's something obvious. I get an InvalidOperationException reading "Databinding methods such as Eval(), XPath(), and...
4
by: mohaaron | last post by:
This seems like it should be simple to do but for some reason I have been unable to make it work. I would like to databind a SqlDataSource to a GridView during the click event of a button. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.