Simple ListViewItem questions

  • Thread starter Thread starter Burak
  • Start date Start date
B

Burak

Hello,

I have two quick questions about adding a
ListViewItem to windows forms combobox and listbox.

1) When I try to add a listview item to a combobox
using the following code

lstFrgnTables.DataSource = ds.Tables(0)
lstFrgnTables.DisplayMember = "TABLE_NAME"
lstFrgnTables.ValueMember = "TABLE_NAME"

Dim item = New System.Windows.Forms.ListViewItem("--
select table -- ", 0)
lstFrgnTables.Items.Add(item).ToString()

I get the following error

"Cannot modify the items collection when the datasource
property is set."

If i add this item before the data source is set, it
just gets overwritten after the data source is set.

How can I fix this?

2) When I add a list view item to a listbox using the
following code

Dim strSelect As String

strSelect = lstFrgnKeys.SelectedValue
Dim item = New
System.Windows.Forms.ListViewItem(strSelect)
lstForeignKeys.Items.Add(item)

the item gets added but it appears as

ListViewItem: {<value>}

How can I just have the <value> appear?

Thank you,

Burak
 
Hi Burak,

You have to add it to the underlaying table.

That is in this case the
ds.tables(0)

And than to answer (very rough written) your next question too.
\\\
dim dr as ds.tables(0).newrow
dr(myfields) = myItems
dr(mykeyfields) = myitems
ds.tables(0).rows.add(dr)
////
I hope this helps,

Cor
 
Add the avlue to the underlying datasource.Since your display and
valuemebers are the same field..

Dim dro as DataRow = ds.Tables(0).NewRow
dro(0) = "--
select table -- "
dro(1) = 0
ds.Tables(0).Rows.Add(dro)

Then do the databind.

2)

lstForeignKeys.Items.Add("<value>")
 
Back
Top