• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 579
  • Last Modified:

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

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
JeffreyDurham
Asked:
JeffreyDurham
  • 2
  • 2
1 Solution
 
riyazthadCommented:
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
 
JeffreyDurhamAuthor Commented:
>> 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
 
JeffreyDurhamAuthor Commented:
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
 
riyazthadCommented:
I was little busy. I thought of pointing you same error and and location.

Anyway you could find it.

Good.
0
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now