currencyManager not binding to datagrid - why?

G

Guest

Hello,

I have a datagrid (dgr1) on a form and I'm trying to bind a currencyManager
Object (cma) to it and print the current row position. But all I get for
cma.Position is 0, 0, 0 for any row in dgr1. dgr1 populates correctly and
when I first set cma - it shows the correct number of rows in the table
(tbl1). In my case there are 14 rows for real, and that is what cma shows.
But when I click a row on dgr1 - cma.position just displays 0. cma is not
displaying the correct row number. Is there a property I need to set? Here
is the code I am using: dgr1 gets its data from Sql Server.

Dim conn As New SqlConnection, dA1, dA2 As SqlDataAdapter
Dim ds As New DataSet, dT1 As DataTable
Dim cmdSelTbl1 As SqlCommand, cma As CurrencyManage
------------------------------------------------------------------------------------
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Server=winxp;UID=sa;PWD=;Database=dbContacts"

Try
conn.ConnectionString = strConn
cmdSelTbl1 = New SqlCommand
cmdSelTbl1.CommandType = CommandType.Text
cmdSelTbl1.CommandText = "Select * from tbl1"
cmdSelTbl1.Connection = conn
conn.Open()
dA1 = New SqlDataAdapter
dA1.SelectCommand = cmdSelTbl1
ds.Clear()
dA1.Fill(ds, "tbl1")
cma = CType(BindingContext(ds.Tables("tbl1")), CurrencyManager)
Console.WriteLine("cma.Count is " & cma.Count)
cma.Position = 0

dgr1.DataSource = ds
dgr1.DataMember = "tbl1"
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Sub dgr1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Handles dgr1.Click
Console.WriteLine(cma.Position)
End Sub

Is there some property I need to set my datagrid? How do I correctly bind
cma with the data Table?

Thanks,
Rich
 
B

Bart Mermuys

Hi,

Rich said:
Hello,

I have a datagrid (dgr1) on a form and I'm trying to bind a
currencyManager
Object (cma) to it and print the current row position. But all I get for
cma.Position is 0, 0, 0 for any row in dgr1. dgr1 populates correctly and
when I first set cma - it shows the correct number of rows in the table
(tbl1). In my case there are 14 rows for real, and that is what cma
shows.
But when I click a row on dgr1 - cma.position just displays 0. cma is not
displaying the correct row number. Is there a property I need to set?
Here
is the code I am using: dgr1 gets its data from Sql Server.

Dim conn As New SqlConnection, dA1, dA2 As SqlDataAdapter
Dim ds As New DataSet, dT1 As DataTable
Dim cmdSelTbl1 As SqlCommand, cma As CurrencyManager
------------------------------------------------------------------------------------
Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String = "Server=winxp;UID=sa;PWD=;Database=dbContacts"

Try
conn.ConnectionString = strConn
cmdSelTbl1 = New SqlCommand
cmdSelTbl1.CommandType = CommandType.Text
cmdSelTbl1.CommandText = "Select * from tbl1"
cmdSelTbl1.Connection = conn
conn.Open()
dA1 = New SqlDataAdapter
dA1.SelectCommand = cmdSelTbl1
ds.Clear()
dA1.Fill(ds, "tbl1")
cma = CType(BindingContext(ds.Tables("tbl1")), CurrencyManager)

Try:
cma = CType(BindingContext(ds, "tbl1"), CurrencyManager )

I needs to be exactly like you bind the grid, using a DataSet and TableName,
see your binding code below.

HTH,
Greetings
 
G

Guest

Thank you very much. That worked perfectly! I changed

dgr1.DataSource = ds

to

dgr1.DataSource = ds.Tables("tbl1")

Now currencymanager works fine.

Thanks for your help.

Rich
 

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