DataBinding to a DataView -- only getting first row

B

Beverley

Hi all,

I'm creating my first "real" Windows Forms application. I'm having some difficulty binding a DataView to my custom control. I can do it just fine if I bind directly to the DataTable, and it behaves as I want. (Multiple controls on the screen, each one bound to a different row in the database.) Essentially, I'm making a datagrid-like interface, but using textboxes, etc.

This code works (code simplified)

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(TCE.db.TheDataset.Personnel), CurrencyManager)
cm.Position = iRow
Next

It works great -- each DataLine (my custom control) is bound to a different row in the Personnel table.

However, when I try to change it to use a dataview instead, which is pointing at the Personnel table, each data line comes out exactly the same -- they are all bound to the first row. The view is being filtered correctly, because when I check the number of rows in the DataView, it is the smaller number that I am expecting.

dv.Table = TCE.db.TheDataset.Personnel
dv.RowFilter = "SubTabID = 1"

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(dv), CurrencyManager)
cm.Position = iRow
Next

Any ideas? Does the Currency Manager not support a Position when connected to a dataview??

Thanks,
Beverley
 
B

Beverley

Okay, I'm getting closer. The way I was attaching my data view to my data table was wrong...

'dv.Table = TCE.db.TheDataset.Personnel 'wrong

dv = TCE.db.TheDataset.Personnel.DefaultView 'right, or at least, better!

So now rather than getting row 1, subtab 1 no matter what subtab I'm on, I get the right subtab, but still only its first row.

My data is something like this (very simplified):
Tab Data
1 Some data on first tab
1 More data on first tab
2 Second tab
3 Third tab
3 More on third
3 Even more on third

When I click Tab 1 now with the above correction, I get this:
1 Some data on first tab
1 Some data on first tab

And Tab 3 gets this:
3 More on third
3 More on third
3 More on third

Do I have to use a DataRowView to do what I want properly? I found an example that mentioned that, but it didn't say why they did it.

Thanks,
Beverley


Hi all,

I'm creating my first "real" Windows Forms application. I'm having some difficulty binding a DataView to my custom control. I can do it just fine if I bind directly to the DataTable, and it behaves as I want. (Multiple controls on the screen, each one bound to a different row in the database.) Essentially, I'm making a datagrid-like interface, but using textboxes, etc.

This code works (code simplified)

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(TCE.db.TheDataset.Personnel), CurrencyManager)
cm.Position = iRow
Next

It works great -- each DataLine (my custom control) is bound to a different row in the Personnel table.

However, when I try to change it to use a dataview instead, which is pointing at the Personnel table, each data line comes out exactly the same -- they are all bound to the first row. The view is being filtered correctly, because when I check the number of rows in the DataView, it is the smaller number that I am expecting.

dv.Table = TCE.db.TheDataset.Personnel
dv.RowFilter = "SubTabID = 1"

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(dv), CurrencyManager)
cm.Position = iRow
Next

Any ideas? Does the Currency Manager not support a Position when connected to a dataview??

Thanks,
Beverley
 
B

Beverley

I'm still getting nowhere with this. Can anyone help me?
Okay, I'm getting closer. The way I was attaching my data view to my data table was wrong...

'dv.Table = TCE.db.TheDataset.Personnel 'wrong

dv = TCE.db.TheDataset.Personnel.DefaultView 'right, or at least, better!

So now rather than getting row 1, subtab 1 no matter what subtab I'm on, I get the right subtab, but still only its first row.

My data is something like this (very simplified):
Tab Data
1 Some data on first tab
1 More data on first tab
2 Second tab
3 More on third
3 Third tab
3 Even more on third

When I click Tab 1 now with the above correction, I get this:
1 Some data on first tab
1 Some data on first tab

And Tab 3 gets this:
3 More on third
3 More on third
3 More on third

Do I have to use a DataRowView to do what I want properly? I found an example that mentioned that, but it didn't say why they did it.

Thanks,
Beverley


Hi all,

I'm creating my first "real" Windows Forms application. I'm having some difficulty binding a DataView to my custom control. I can do it just fine if I bind directly to the DataTable, and it behaves as I want. (Multiple controls on the screen, each one bound to a different row in the database.) Essentially, I'm making a datagrid-like interface, but using textboxes, etc.

This code works (code simplified)

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(TCE.db.TheDataset.Personnel), CurrencyManager)
cm.Position = iRow
Next

It works great -- each DataLine (my custom control) is bound to a different row in the Personnel table.

However, when I try to change it to use a dataview instead, which is pointing at the Personnel table, each data line comes out exactly the same -- they are all bound to the first row. The view is being filtered correctly, because when I check the number of rows in the DataView, it is the smaller number that I am expecting.

dv.Table = TCE.db.TheDataset.Personnel
dv.RowFilter = "SubTabID = 1"

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(dv), CurrencyManager)
cm.Position = iRow
Next

Any ideas? Does the Currency Manager not support a Position when connected to a dataview??

Thanks,
Beverley
 
B

Beverley

I'm starting to suspect that I've set up the binding on my custom UserControl incorrectly. When the control is first created, I bind each of the textboxes, etc, that it contains directly to the datatable. So when I come along with the other code saying what row to get, maybe that's my problem??

I don't know how to do this properly though. I've read the MS BOL documentation about data binding but it doesn't go into enough depth for what I'm trying to do... This is what I've got, is it alright or am I totally screwed up? :)


Some code snips from where I set up the screen, create the controls, and try to get them to go to the right data...

Dim PersonnelRows() As DataRow
PersonnelRows = TCE.db.TheDataset.Personnel.Select("SubTabID = " & CType(iSubTab, String))

'--------------------- and then later

For iRow = 0 To PersonnelRows.Length - 1
If iRow = 0 Then
dlg = MakePersonnelLine(True)
Else
dlg = MakePersonnelLine(False)
End If
Try
'this only gets the first row... why?
Dim cm As CurrencyManager = CType(dlg.BindingContext(PersonnelRows), CurrencyManager)
cm.Position = iRow

Debug.WriteLine(cm.Position.ToString)
Debug.WriteLine(cm.List(cm.Position).subcategory)

Catch e As Exception
MsgBox(e.ToString)
End Try

Next

This is how I create and display an instance of the control....

Private Function MakePersonnelLine(ByVal bShowHeader As Boolean, ByVal TheDataBinding As Object) As DataLine.Generic

Dim dlg As DataLine.Generic
Static y As Integer

dlg = New DataLine.Generic(bShowHeader)
If bShowHeader = True Then
'y = 24 'or 32 to leave a space
y = 0
End If

With dlg
.Visible = False
.BindingContext = New BindingContext

.MyDataBinding = TCE.db.TheDataset.Personnel ' Is this line the cause of my grief?

'set the physical properties
.Parent = Me.pnlData
.Location = New Point(0, y)
.Width = Me.pnlData.Width
.Anchor += AnchorStyles.Right
.Visible = True

y += .Height
End With

Return dlg

End Function

In the UserControl's class.....

Public Property MyDataBinding() As Object
Get
MyDataBinding = txtCategory.DataBindings.Item(0).BindingManagerBase()
End Get
Set(ByVal Value As Object)

With txtCategory.DataBindings
.Clear()
.Add("Text", Value, "Category")
End With

With Me.lblSubCategory.DataBindings
.Clear()
.Add("Text", Value, "SubCategory")
End With

' and so on for all the other textboxes, etc

End Set
End Property


Thanks for reading this far :)

Beverley

I'm still getting nowhere with this. Can anyone help me?
Okay, I'm getting closer. The way I was attaching my data view to my data table was wrong...

'dv.Table = TCE.db.TheDataset.Personnel 'wrong

dv = TCE.db.TheDataset.Personnel.DefaultView 'right, or at least, better!

So now rather than getting row 1, subtab 1 no matter what subtab I'm on, I get the right subtab, but still only its first row.

My data is something like this (very simplified):
Tab Data
1 Some data on first tab
1 More data on first tab
2 Second tab
3 More on third
3 Third tab
3 Even more on third

When I click Tab 1 now with the above correction, I get this:
1 Some data on first tab
1 Some data on first tab

And Tab 3 gets this:
3 More on third
3 More on third
3 More on third

Do I have to use a DataRowView to do what I want properly? I found an example that mentioned that, but it didn't say why they did it.

Thanks,
Beverley


Hi all,

I'm creating my first "real" Windows Forms application. I'm having some difficulty binding a DataView to my custom control. I can do it just fine if I bind directly to the DataTable, and it behaves as I want. (Multiple controls on the screen, each one bound to a different row in the database.) Essentially, I'm making a datagrid-like interface, but using textboxes, etc.

This code works (code simplified)

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(TCE.db.TheDataset.Personnel), CurrencyManager)
cm.Position = iRow
Next

It works great -- each DataLine (my custom control) is bound to a different row in the Personnel table.

However, when I try to change it to use a dataview instead, which is pointing at the Personnel table, each data line comes out exactly the same -- they are all bound to the first row. The view is being filtered correctly, because when I check the number of rows in the DataView, it is the smaller number that I am expecting.

dv.Table = TCE.db.TheDataset.Personnel
dv.RowFilter = "SubTabID = 1"

For iRow = 0 To 2
dlg = new DataLine.Generic
...
Dim cm As CurrencyManager = CType(dlg.BindingContext(dv), CurrencyManager)
cm.Position = iRow
Next

Any ideas? Does the Currency Manager not support a Position when connected to a dataview??

Thanks,
Beverley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top