Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Want to bind two forms (datagrid::singleRecView) to the same data(source?)(?); Please Help!

Posted on 2006-11-27
4
Medium Priority
?
575 Views
Last Modified: 2008-01-09
Dear Experts,

Ok, I've got this program, which I am writing for my company - and it's got a form with a datagrid. This is how I bind my db to the datagrid in the first form:

Imports System.Data.OleDb

Public Class formDataGrid
    Private Const gHiddenFields = "GUID"
    Public bindingSource1 As New BindingSource
    Public dbAdapter = New OleDb.OleDbDataAdapter()
    Public dtResults = New DataTable("Results")

    Public Sub bindTable()
        dodatagridinit()

        Dim con As New OleDbConnection(mydb.myDbVars.myConnectStr)
        dbAdapter = New OleDb.OleDbDataAdapter(mydb.myDbVars.myQueryStr, con)
        Dim cb As New OleDbCommandBuilder(dbAdapter)

        dbAdapter.Fill(dtResults)
        'Me.lblTotal.Text = "Total: " &
        Me.bindingSource1.DataSource = dtResults
        Me.BindingNavigator1.BindingSource = bindingSource1
        Me.mydatagrid.DataSource = bindingSource1
        Me.mydatagrid.AutoResizeColumns()
        hideInvisColumns()
    End Sub
    'other unrelated code..
    '..
End Class

This is the values of those two vars:
mydb.myDbVars.myConnectStr = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;;Data Source=C:\Documents and Settings\Michael\Desktop\UI Programs\UI prog - Home Sales\UI&ML&HSdb.mdb"
mydb.myDbVars.myQueryStr = "SELECT * FROM myRSSelection;"

Anyways.. Now that I got that to work, I'm trying to create a second form, which is a single-record view of like the selected field in the datagrid. Both forms have a bindingNavigator. I'm trying to do two things..

      1) Tie the second binding navigator into the first so that moving one moves the other
      2) Bind the individual fields to the record displayed in the datagrid (currently, at runtime)

This is the code I'm trying to use that is not working.

Public Class SingleRecView
    Private Sub form_load()
        setData()
        '  setdatafields()
    End Sub

    Private Sub form_activate()
        setData()
        ' setdatafields()
    End Sub

    Private Sub setData()
        'Me.BindingNavigator1.DataBindings.Add(formDataGrid.dtResults)
        Me.BindingNavigator1.BindingSource = formDataGrid.bindingSource1
    End Sub

    Private Sub setdatafields()
        'formDataGrid.dtResults()
        Me.fldFileNum.DataBindings.Add(New Binding("text", formDataGrid.bindingSource1, mydb.myDbVars.myTableName & ".FILENUM"))
        Me.fldCity.DataBindings.Add(New Binding("Text", Me.BindingNavigator1, "CITY"))
        Me.fldAddress.DataBindings.Add(New Binding("Text", formDataGrid.dtResults, "ADDRESS"))
        'Adds rows to dataset.
        'OleDbDataAdapter1.Fill(ds)
        'Add a binding object to the controls.
        'Set Text as the data-bound property, and set ds as the data source.
        'TableName.ColumnName specifies the data member.
        'BirthDate.DataBindings.Add(New Binding("Text", ds, "Employees.BirthDate"))
        'LastName.DataBindings.Add(New Binding("Text", ds, "Employees.LastName"))
    End Sub
End Class

Any help on this matter would be greatly appreciated.
Thanks in advance! ~Michael
0
Comment
Question by:JeffreyDurham
[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
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:riyazthad
ID: 18021183
You didnt mention error details.

If it is not working, try this

change

Me.fldFileNum.DataBindings.Add(New Binding("text", formDataGrid.bindingSource1, mydb.myDbVars.myTableName & ".FILENUM"))
Me.fldCity.DataBindings.Add(New Binding("Text", Me.BindingNavigator1, "CITY"))
Me.fldAddress.DataBindings.Add(New Binding("Text", formDataGrid.dtResults, "ADDRESS"))

to

Me.fldFileNum.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1,"FILENUM"))
Me.fldCity.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "CITY"))
Me.fldAddress.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "ADDRESS"))



0
 

Author Comment

by:JeffreyDurham
ID: 18021508
>> You didnt mention error details.

Oh, ok. Yeah, I did miss that. Ok, the issue that is occurring is that the bindingNavigator doesn't seem to initialize, I can't figure out how to properly do this; but what I mean by that is that it remains unenabled, doesn't show the recordCount, none of the buttons work. So I think that I'm not correctly tying it to the other form.

And the other issue was that none of the fields in the singleRecView form appeared to be tied to my datatable either (from the datagrid form). They were all just blank.
0
 

Author Comment

by:JeffreyDurham
ID: 18031640
Ok, I was able to fix this one myself. This was the error and changes I made in detail to fix it.

>> Private Sub form_load()
>>         setData()
>>         '  setdatafields()
>>     End Sub

>>     Private Sub form_activate()
>>         setData()
 >>        ' setdatafields()
>>     End Sub
Neither of the above two subs were called because they didn't have the right syntax and the sender and event args and all that, plus it was missing the 'Handles ..'

So, then I took all my vars involved in the first form for databinding and made them public. Then I tied the bindingNavigator to the same one as the first form, and set individual bindings on all the fields I wanted to display.

Here is the fixed code for both forms..

[SingleRecView Form]

Private Sub form_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Stop
        setData()
        setdatafields()
    End Sub

    Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        ''DON'T FORGET TO kill all globals
        SaveToDB()
        formDataGrid.Focus()
        formDataGrid.mydatagrid.Refresh()
    End Sub

    Private Sub form_activate()
        setData()
        setdatafields()
    End Sub

    Private Sub SaveToDB()
        SRVBindingSource.EndEdit()
        SRVdbAdapter.update(SRVdtResults)
    End Sub

    Private Sub setData()
        'Me.BindingNavigator1.DataBindings.Add(formDataGrid.dtResults)
        Dim specRecQuery$ = " WHERE FILENUM = """ & gFileNumber & """"

        'SRVdbAdapter = New OleDb.OleDbDataAdapter(Replace(mydb.myDbVars.myQueryStr, ";", "") & specRecQuery & ";", formDataGrid.myConnection)
        'myCommandBuilder = New OleDbCommandBuilder(SRVdbAdapter)
        'SRVdbAdapter.fill(SRVdtResults)
        '' Me.SRVBindingSource.DataSource = formDataGrid.dtResults 'SRVdtResults
        Me.BindingNavigator1.BindingSource = formDataGrid.bindingSource1 'SRVBindingSource '.DataSource
    End Sub

    Private Sub setdatafields()
        Me.fldFileNum.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "FILENUM"))
        Me.fldCity.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "CITY"))
        Me.fldAddress.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "ADDRESS"))
        Me.fldComments.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "COMMENTS"))
        Me.fldCounty.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "COUNTY"))
        Me.fldDate.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "DATE"))
        Me.fldDown.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "DOWN"))
        Me.fldFirstName.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "FIRSTNAME"))
        Me.fldHomeType.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "HOMETYPE"))
        Me.fldLastName.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "LASTNAME"))
        Me.fldNew.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "NEW"))
        Me.fldParcelNum.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "PARCELNUM"))
        Me.fldPhone.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "PHONE"))
        Me.fldPlus4.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "PLUS4"))
        Me.fldRental.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "RENTAL"))
        Me.fldReport.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "REPORT"))
        Me.fldSalePrice.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "SALEPRICE"))
        Me.fldState.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "STATE"))
        Me.fldUpdate.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "UPDATE"))
        Me.fldZip.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "ZIP"))
        'special binding for checkbox
        Me.btnSelected.DataBindings.Add(New Binding("Checked", formDataGrid.bindingSource1, "SELECTED"))
        'special binding for dateTimePickers
        On Error Resume Next
        Me.fldAction.DataBindings.Add(New Binding("Text", formDataGrid.bindingSource1, "ACTION"))
        'Me.dtPickAction.DataBindings.Add(New Binding("Value", formDataGrid.bindingSource1, "ACTION", True, DataSourceUpdateMode.OnPropertyChanged, 0))
    End Sub

[DataGridView Form]

Public Class formDataGrid
    Private Const gHiddenFields = "GUID"
    'Public DataAdapter As OleDbDataAdapter
    Public bindingSource1 As New BindingSource
    Public dbAdapter As New OleDb.OleDbDataAdapter() ' = New OleDb.OleDbDataAdapter()
    Public dtResults = New DataTable("Results")
    Public myConnection As New OleDbConnection(mydb.myDbVars.myConnectStr)
    Public mycb As New OleDbCommandBuilder

Public Sub bindTable()
        dodatagridinit()

        'Dim con As New OleDbConnection(mydb.myDbVars.myConnectStr)
        Dim conn As New OleDbConnection(mydb.myDbVars.myConnectStr)
        Dim myquerystr$ = CStr(mydb.myDbVars.myQueryStr)
        'myConnection.ConnectionString = myquerystr
        dbAdapter = New OleDb.OleDbDataAdapter(myquerystr, conn)
        mycb = New OleDbCommandBuilder(dbAdapter)
        ''original''    Dim cb As New OleDbCommandBuilder(dbAdapter)

        dbAdapter.Fill(dtResults)
        myConnection = conn
        'Me.lblTotal.Text = "Total: " &

        myConnection.Open()
        Me.bindingSource1.DataSource = dtResults
        Me.BindingNavigator1.BindingSource = bindingSource1
        Me.mydatagrid.DataSource = bindingSource1
        Me.mydatagrid.AutoResizeColumns()
        hideInvisColumns()
    End Sub
'..
0
 
LVL 6

Accepted Solution

by:
riyazthad earned 1000 total points
ID: 18039164
I was little busy. I thought of pointing you same error and and location.

Anyway you could find it.

Good.
0

Featured Post

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!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

604 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