Solved

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

Posted on 2006-11-27
4
563 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
  • 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 250 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now