Help with Data bound ListView

S

Simon Verona

I'm trying to create a dataview in code and bind it to a combobox using the code below.

When it gets to setting the txtLookup.ValueMember="id" in the code an error message of "unable to case dataviewrow to string" appears... and I can't work out why! Can anybody help?

Thanks.. Simon

=== Code ====

dt = New DataTable

ds = New DataSet

dv = New DataView

dt.TableName = "Data"

dt.Columns.Add("Id", System.Type.GetType("System.String"), "")

dt.Columns.Add("Description", System.Type.GetType("System.String"), "")


Dim dr As DataRow

For cnt = 1 To dct

If jtmp.Extract(m_LookupAmc, cnt).StartsWith("*") = False Then

dr = dt.NewRow

dr.Item("Id") = CStr(cnt)

dr.Item("Description") = CStr(jtmp.Extract(m_LookupAmc, cnt))

dt.Rows.Add(dr)

End If

Next cnt

ds.Tables.Add(dt)

dv = ds.Tables("Data").DefaultView

dv.Sort = "Description"

txtLookup.DataSource = dv

txtLookup.DisplayMember = "Description"

txtLookup.ValueMember = "Id" '<<<<< It crashes out here!!
 
S

Simon Verona

oops.. spelling mistake... Error should "cast" not "case" 'dataviewrow' to 'string'..
I'm trying to create a dataview in code and bind it to a combobox using the code below.

When it gets to setting the txtLookup.ValueMember="id" in the code an error message of "unable to case dataviewrow to string" appears... and I can't work out why! Can anybody help?

Thanks.. Simon

=== Code ====

dt = New DataTable

ds = New DataSet

dv = New DataView

dt.TableName = "Data"

dt.Columns.Add("Id", System.Type.GetType("System.String"), "")

dt.Columns.Add("Description", System.Type.GetType("System.String"), "")


Dim dr As DataRow

For cnt = 1 To dct

If jtmp.Extract(m_LookupAmc, cnt).StartsWith("*") = False Then

dr = dt.NewRow

dr.Item("Id") = CStr(cnt)

dr.Item("Description") = CStr(jtmp.Extract(m_LookupAmc, cnt))

dt.Rows.Add(dr)

End If

Next cnt

ds.Tables.Add(dt)

dv = ds.Tables("Data").DefaultView

dv.Sort = "Description"

txtLookup.DataSource = dv

txtLookup.DisplayMember = "Description"

txtLookup.ValueMember = "Id" '<<<<< It crashes out here!!
 
S

Simon Verona

Problem solved!!

Curiously I found that setting the datasource after setting the ValueMember and DataMember seems to do the trick - I'm not sure why!!

Regards
Simon
oops.. spelling mistake... Error should "cast" not "case" 'dataviewrow' to 'string'..
I'm trying to create a dataview in code and bind it to a combobox using the code below.

When it gets to setting the txtLookup.ValueMember="id" in the code an error message of "unable to case dataviewrow to string" appears... and I can't work out why! Can anybody help?

Thanks.. Simon

=== Code ====

dt = New DataTable

ds = New DataSet

dv = New DataView

dt.TableName = "Data"

dt.Columns.Add("Id", System.Type.GetType("System.String"), "")

dt.Columns.Add("Description", System.Type.GetType("System.String"), "")


Dim dr As DataRow

For cnt = 1 To dct

If jtmp.Extract(m_LookupAmc, cnt).StartsWith("*") = False Then

dr = dt.NewRow

dr.Item("Id") = CStr(cnt)

dr.Item("Description") = CStr(jtmp.Extract(m_LookupAmc, cnt))

dt.Rows.Add(dr)

End If

Next cnt

ds.Tables.Add(dt)

dv = ds.Tables("Data").DefaultView

dv.Sort = "Description"

txtLookup.DataSource = dv

txtLookup.DisplayMember = "Description"

txtLookup.ValueMember = "Id" '<<<<< It crashes out here!!
 

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