Think is should be simple but carn`t find info:(

  • Thread starter Thread starter MadCrazyNewbie
  • Start date Start date
M

MadCrazyNewbie

Hey Group,

Im Sure this is simple but I carn`t seem to find any info anywhere:(

I have 2 x Forms: Form 1 has a DataConnecter and several DataAdapters on it,
all bound together in my DataSet.

On My Table(Faxs) I have a relationship to another Table(MeterReadings), on
my Form I have a button called "Meter Reading".

How do I get it so that when I select a row from my Database in my datagrid
on my Form1, and click Meter Reading, it loads up Form2 with a Datagrid that
only shows me the meter Readings for the Fax Row I selected on Form 1? If
you get what I mean

Any Links Are help would be greatly appriciated.

Many Thanks
MCN
 
Please use both groups (or all groups in the header so you don't mulitpost)
I answered this over in ADO.NET and I believe I had a more complete answer
to the same problem previously but i don't seem to see it right now.

DataRow dro = DataTable.Rows[dataGrid.SelectedRowIndex];

then pass dro to your new form. You can either create a property in the new
form, or create an overloaded constructor or function that takes a datarow
as a param and then bind to it.

HTH,

Bill

www.devbuzz.com
www.knowdotnet.com
 
Hi Mad,

I was busy telling you the same as William did about the multipost, there is
no problem when you crosspost (send one message to more newsgroups) than
others can see if the answer you did get in the adonet group was right for
you.

Because I was busy and with another approach than William I send this also.

You said you had a made a datarelation and I hope binded the maintable using
setdatabinding to the dataset. Than you can do in my opinion something as:

\\\
Dim frm As New Form2
frm.DataGrid1.SetDataBinding(ds, _
"Maintable.DatarelationName")
frm.ShowDialog()
///

I hope this helps?
Cor
 
Cor,

Sorry I don`t understand what you mean bind the main table using
setdatabings?

Here is my code:

Private Sub frmMainMenuPasswords_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
cmDsFax = BindingContext(dsFax, "Faxs")
cmDsPhotoCopiers = BindingContext(dsPhotocopiers, "PhotoCopiers")
mlLoading = True
'Start of Fill Datasets
Try
dsFax.EnforceConstraints = False
dsPhotocopiers.EnforceConstraints = True
Try
Me.odcFaxCopiers.Open()
'Fill Fax Copier Datasets
Me.odaDepartments.Fill(dsFax)
Me.odaSites.Fill(dsFax)
Me.odaFaxs.Fill(dsFax)
'Fill PhotoCopier Datasets
Me.odaDepartments.Fill(dsPhotocopiers)
Me.odaSites.Fill(dsPhotocopiers)
Me.odaPhotoCopiers.Fill(dsPhotocopiers)
Catch fillException As System.Exception
Throw fillException
Finally
Me.odcFaxCopiers.Close()
End Try
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
Finally
mlLoading = False
End Try
'End of Fill Datasets
End Sub

'Start of Position Changed Code - Faxs
Private Sub cmdsFax_PositionChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles cmDsFax.PositionChanged
dsFax_PositionChanged()
End Sub
Private Sub cboFaxsDepartment_SelectedIndexChanged(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
cboFaxsDepartment.SelectedIndexChanged
Me.BindingContext(dsFax, "Faxs").EndCurrentEdit()
If Me.BindingContext(dsFax, "Faxs").Position <> -1 And Not mlLoading
Then
dsFax.Faxs.Rows(Me.BindingContext(dsFax,
"Faxs").Position).Item("DepartmentID") = Me.cboFaxsDepartment.SelectedValue
End If
End Sub
Private Sub cbofaxsSite_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles cboFaxsSite.SelectedIndexChanged
Me.BindingContext(dsFax, "Faxs").EndCurrentEdit()
If Me.BindingContext(dsFax, "Faxs").Position <> -1 And Not mlLoading
Then
dsFax.Faxs.Rows(Me.BindingContext(dsFax,
"Faxs").Position).Item("SitesID") = Me.cboFaxsDepartment.SelectedValue
End If
End Sub
Private Sub dsFax_PositionChanged()
Me.BindingContext(dsFax, "Faxs").EndCurrentEdit()
If Me.BindingContext(dsFax, "Faxs").Position <> -1 And Not mlLoading
Then
Me.cboFaxsDepartment.SelectedValue =
dsFax.Faxs.Rows(Me.BindingContext(dsFax,
"Faxs").Position).Item("DepartmentID")
Me.cboFaxsSite.SelectedValue = dsFax.Faxs.Rows(Me.BindingContext(dsFax,
"Faxs").Position).Item("SitesID")
End If
End Sub

Many Again Thanks
MCN

'End of Position Changed Code - Faxs
 
Hi Mad,

I do not see how the code has to do with the question.

I see nothing about a datagrid on form1 eiter on form1 or a relation to the
table "meterreading"

This is your text
---------------------------
On My Table(Faxs) I have a relationship to another Table(MeterReadings), on
my Form I have a button called "Meter Reading".

How do I get it so that when I select a row from my Database in my datagrid
on my Form1, and click Meter Reading, it loads up Form2 with a Datagrid that
only shows me the meter Readings for the Fax Row I selected on Form 1? If
you get what I mean
 
He's got a valid point Si.

Follow Williams advice, its correct.

But to add...

Set a property on Form 2 for your key/index whatever you want to have the
forms data "based" off of..

so.. if the key in Faxes was idFax set a property in form 2 called idFax or
whatever you want..

Then...

.... williams stuff here.

.... find the row

dim myForm as Form2

myForm2.idFax = [therow id you found or whatever you want to set]

myform2.show()

in the property, do whatever you need to do to populate the grid

hope it helps,
cj
 
Hi CJ,

When there is a relation created, than that code that I did provide works as
a charm with a datagrid and a dataset.

However with those comboboxes I go for a solution just providing the
selectedindex to the form and the dataset in a sub (that are only
references). Has the same simplisity.

Cor
 
Cor,

Not arguing your method. Just going off experience of working with Simon
and actually seeing a vast majority of the program (if not all of it) within
the past week.

Given his level of VB.NET, current undersatnding of different objects etc..
this just seemed to be the best approach for him.

No offense meant to either of you.

-CJ
 
Hi CJ,

As forever I did understand what you wrote in your earlier message of
course, however this was what Bill wrote and and I am afraid that Simon and
I are on the same level, because I do not understand this. (I did not want
to write this but for the sake of Simon you forced me to that).

In this thread
--------------------
DataRow dro = DataTable.Rows[dataGrid.SelectedRowIndex];

then pass dro to your new form. You can either create a property in the new
form, or create an overloaded constructor or function that takes a datarow
as a param and then bind to it.
---------------------
And this in the ADONET group which I do not understand either
---------------------
Private _RowHolder As DataRow
Public Property RowHolder() As DataRow
Get
Return _RowHolder
End Get
Set(ByVal Value As DataRow)
_RowHolder = Value
End Set
End Property
Public Sub New(ByVal _Holder As DataRow)
Me.RowHolder = _Holder
End Sub

Then, no form1 use Dim frm as New
SecondGridForm(DataTableName.Rows[DataGrid.CurrentRowIndex])
frm.ShowDialog ' or .Show() depending on your needs
Then, once it's set you can do whatever you want on frm. Howver, if you
need the value you'll have it via frm.RowHolder as long as you remember to
reference on the second form with me.RowHolder
---------------------

Maybe you can explain this to us something deeper in the context of the
question from Simon?

(To make it something clearer, I can bind a table (or mostly an Ilist) as a
dataasource to any control however how to bind a datarow to a complex
control that do I not know).

Cor
 
Hey CJ/Cor/William,

Hope you could advise me a little again?

Now i`ve got in my Button Click Event on my first form(frmFaxs):

Dim LoadFaxMeterReadings As frmFaxMeterReadings()
LoadFaxMeterReadings.FaxID(BindingContext(dsFax, "Faxs").Position)
LoadFaxMeterReadings.Show()

(The Above tells me FAXID & Show are not members of System.Array)

On My Second form I have:

Public Property RowHolder() As DataRow
Get
Return RowHolder
End Get
Set(ByVal Value As DataRow)
RowHolder = Value
End Set
End Property

Public Sub New(ByVal Holder As DataRow)
Me.RowHolder = Holder
End Sub

Private Sub frmFaxMeterReadings_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
mlLoading = True
'Start of Fill Datasets
Try
dsFaxMeterReadings.EnforceConstraints = False
Try
Me.odcFaxMeterReadings.Open()
'Fill Arciris Roles Datasets
Me.odaFaxMeterReadings.Fill(dsFaxMeterReadings)
Catch fillException As System.Exception
Throw fillException
Finally
Me.odcFaxMeterReadings.Close()
End Try
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
Finally
mlLoading = False
End Try
'End of Fill Datasets
End Sub

Im I understanding this or am I going about this totally wrongs?

Many Thanks
MCN



Cor Ligthert said:
Hi CJ,

As forever I did understand what you wrote in your earlier message of
course, however this was what Bill wrote and and I am afraid that Simon and
I are on the same level, because I do not understand this. (I did not want
to write this but for the sake of Simon you forced me to that).

In this thread
--------------------
DataRow dro = DataTable.Rows[dataGrid.SelectedRowIndex];

then pass dro to your new form. You can either create a property in the new
form, or create an overloaded constructor or function that takes a datarow
as a param and then bind to it.
---------------------
And this in the ADONET group which I do not understand either
---------------------
Private _RowHolder As DataRow
Public Property RowHolder() As DataRow
Get
Return _RowHolder
End Get
Set(ByVal Value As DataRow)
_RowHolder = Value
End Set
End Property
Public Sub New(ByVal _Holder As DataRow)
Me.RowHolder = _Holder
End Sub

Then, no form1 use Dim frm as New
SecondGridForm(DataTableName.Rows[DataGrid.CurrentRowIndex])
frm.ShowDialog ' or .Show() depending on your needs
Then, once it's set you can do whatever you want on frm. Howver, if you
need the value you'll have it via frm.RowHolder as long as you remember to
reference on the second form with me.RowHolder
---------------------

Maybe you can explain this to us something deeper in the context of the
question from Simon?

(To make it something clearer, I can bind a table (or mostly an Ilist) as a
dataasource to any control however how to bind a datarow to a complex
control that do I not know).

Cor
 
Hi Mad,

With the change to get angry messages from CJ and Bill, I give you my third
way to go, now I see how far you are. All is done in line and are the rows
without the >, deleted rows you have to check yourself. All typed in here by
hand and not in the designer so there can be typos. However in my opinion
this should go.

Dim LoadFaxMeterReadings As New frmFaxMeterReadings()
LoadFaxMeterReadings.FaxId= *The value from the combobox or the datagrid, I
do not know what it is now.* so if this stays a problem message that.
LoadFaxMeterReadings.ShowdialogIn frmFaxMeterReading you add after the desinger created code

Public FaxId as string

You have probably to change your selection string, and I when you are using
SQL server
Then selection string has to be something like this
"SELECT * FROM Mytable WHERE Faxs = @Faxs"
Public Sub New(ByVal Holder As DataRow)
Me.RowHolder = Holder
End Sub

Private Sub frmFaxMeterReadings_Load(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles MyBase.Load
mlLoading = True

odaFaxMeterReadings.SelectCommand.Parameters.Add(New SqlParameter("@Fax",
SqlDbType.Char, 30)).Value = FaxId
*** you have to set the right type and the lenght in this***********
'Start of Fill Datasets
Try
dsFaxMeterReadings.EnforceConstraints = False
Try
Me.odcFaxMeterReadings.Open()
'Fill Arciris Roles Datasets
Me.odaFaxMeterReadings.Fill(dsFaxMeterReadings)
Catch fillException As System.Exception
Throw fillException
Finally
Me.odcFaxMeterReadings.Close()
End Try
Catch eLoad As System.Exception
System.Windows.Forms.MessageBox.Show(eLoad.Message)
Finally
mlLoading = False
End Try
'End of Fill Datasets

dim dv as new dataview(dsFaxMeterReadings)
datagrid1.datasource = dv

I hope this helps and hope as well on not to hard messages from CJ and Bill.

Cor
 
Cor,

Sorry but i don`t understand what goes here:
LoadFaxMeterReadings.FaxId= *The value from the combobox or the datagrid,
I do not know what it is now*

also would I still use:
odaFaxMeterReadings.SelectCommand.Parameters.Add(New SqlParameter("@Fax",
SqlDbType.Char, 30)).Value = FaxId

Even though im using Access?

Many Thanks
MCN
 
Hi Mad,

I try to explain it in words.

What we are doing now is create an public variable in your second form.
That has to be the Id of the fax as it is selected in: And here I am not
sure if that is a combobox on your form1 or a datagrid, when I know that I
can tell something more.

Than we open the form2 and give the FaxId to that.
In that form2 you are reading the Faxdataset I saw, however we limit it to
only the faxId you did want.

The OleDB needs some litle changes. the Select string becomes for OleDB
(access) something as.
"SELECT * FROM Mytable WHERE Fax = ?"

And the parameter becomes (if you have set an imports to system.IO.oledb)
odaFaxMeterReadings.SelectCommand.Parameters.Add(New OleDbParameter("Fax",
OleDbType.Char)).Value = FaxId

http://msdn.microsoft.com/library/d...eDbOleDbParameterCollectionClassAddTopic2.asp

However tell me about where we get that FaxId?

Cor
 
Hi Cor, sorry again about my other post my post still hasn`t show up on
outlook:( anyways.

On my Faxs Form (Form1) i have a datagrid and serveral comboboxs, however I
want the user to selct the row via the datagird. once they have selected the
row they click on the FaxMeterReadings button to take them to
FaxMeterReadings (Form2)

Thanks Again
MCN
 
Ok

Then you need something as this in your button event on form1

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim frm2 As New Form2
frm2.faxId = Me.DataGrid1.Item(DataGrid1.CurrentRowIndex, 0).ToString
frm2.ShowDialog()
End Sub

The 0 you see is the columnindex from the datagrid where the faxId is
located.
So that is one of the things you have to change.

Now it should work.
And when not tell what happens.

Cor
 
Cor,

It tells me that FaxID is not a Member of FaxMeterReadingsForm

What do I need to set as FaxID? CJ said to set a property on the Form for my
Key/Index? whats does this need to be?

Many Thanks Again
MCN
 
Cor,

Another Thing I have on my FaxMeterReadingTable:

MeterReadingID(PrimaryKey), FaxID(Lookup to FaxID(PrimaryKey of My Fax
Table), Date, & Reading.

I think i`ve built the table right as my Parent Table is Faxs and my Child
Table is FaxMeterReadings (Belive i`ve got it right in a One-Two-Many
Relationship)

Just thought id better check My Database Was right first

Thanks Again
MCN
 
Did you do this as I wrote?

In FaxMeterReadingForm you add after the desinger created code

Public FaxId as string

Cor
 
Hi Simon,

I think that you can better first make this to an end and than look how to
optimize it.
You are almost there that there will be a table displayed.

Cor
 
Sorry Cor,

I misted that one:( Right cool seems to load up my form now:) only thing is
it dosn`t display the meter reading for just that fax I selected, instead is
fills my DataGrid with All Details from All Faxs:(

Any Ideas?

Many Thanks
MCN
 
Back
Top