array bound listbox

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey all,

I was wondering, can I databind my text box to an array, if so what would my
datasource and member look like?

thanks in advance,
rodchar
 
Rodchar,

I made a sample for you with wich I try to show this question and your
previous.
An normal array has no property therfore you can not bind to, so I show it
you with a table how to do it.

Therefore try it and tell me if this fits a little bit the problem.
\\\needs a form with 2 listboxes and one textbox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim countries() As String = {"US", "EU"}
ListBox1.Items.AddRange(countries)
Dim table As New DataTable("Sample")
CreateTable(table)
Dim dv As New DataView(table)
ListBox2.DataSource = dv
dv.Sort = "Name"
ListBox2.DisplayMember = "Name"
TextBox1.DataBindings.Add(New Binding("Text", dv, "Id"))
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
DirectCast(ListBox2.DataSource, DataView).RowFilter = _
"Country = '" & ListBox1.SelectedItem.ToString & "'"
End Sub
Private Sub CreateTable(ByVal Table As DataTable)
Table.Columns.Add("Id")
Table.Columns.Add("Name")
Table.Columns.Add("Country")
For i As Integer = 0 To 7
Dim dr As DataRow = Table.NewRow
dr(0) = i.ToString
Table.Rows.Add(dr)
Next
Table.Rows(0)(1) = "Herfried K. Wagner"
Table.Rows(1)(1) = "Armin Zingler"
Table.Rows(2)(1) = "Ken Tucker"
Table.Rows(3)(1) = "CJ Taylor"
Table.Rows(4)(1) = "Jay B Harlow"
Table.Rows(5)(1) = "Terry Burns"
Table.Rows(6)(1) = "Tom Shelton"
Table.Rows(7)(1) = "Cor Ligthert"
Table.Rows(0)(2) = "EU"
Table.Rows(1)(2) = "EU"
Table.Rows(2)(2) = "US"
Table.Rows(3)(2) = "US"
Table.Rows(4)(2) = "US"
Table.Rows(5)(2) = "EU"
Table.Rows(6)(2) = "US"
Table.Rows(7)(2) = "EU"
End Sub
///

I hope this helps a little bit?

Cor
 
thanks for the snippet, that worked great.

Cor Ligthert said:
Rodchar,

I made a sample for you with wich I try to show this question and your
previous.
An normal array has no property therfore you can not bind to, so I show it
you with a table how to do it.

Therefore try it and tell me if this fits a little bit the problem.
\\\needs a form with 2 listboxes and one textbox
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim countries() As String = {"US", "EU"}
ListBox1.Items.AddRange(countries)
Dim table As New DataTable("Sample")
CreateTable(table)
Dim dv As New DataView(table)
ListBox2.DataSource = dv
dv.Sort = "Name"
ListBox2.DisplayMember = "Name"
TextBox1.DataBindings.Add(New Binding("Text", dv, "Id"))
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender _
As Object, ByVal e As System.EventArgs) Handles
ListBox1.SelectedIndexChanged
DirectCast(ListBox2.DataSource, DataView).RowFilter = _
"Country = '" & ListBox1.SelectedItem.ToString & "'"
End Sub
Private Sub CreateTable(ByVal Table As DataTable)
Table.Columns.Add("Id")
Table.Columns.Add("Name")
Table.Columns.Add("Country")
For i As Integer = 0 To 7
Dim dr As DataRow = Table.NewRow
dr(0) = i.ToString
Table.Rows.Add(dr)
Next
Table.Rows(0)(1) = "Herfried K. Wagner"
Table.Rows(1)(1) = "Armin Zingler"
Table.Rows(2)(1) = "Ken Tucker"
Table.Rows(3)(1) = "CJ Taylor"
Table.Rows(4)(1) = "Jay B Harlow"
Table.Rows(5)(1) = "Terry Burns"
Table.Rows(6)(1) = "Tom Shelton"
Table.Rows(7)(1) = "Cor Ligthert"
Table.Rows(0)(2) = "EU"
Table.Rows(1)(2) = "EU"
Table.Rows(2)(2) = "US"
Table.Rows(3)(2) = "US"
Table.Rows(4)(2) = "US"
Table.Rows(5)(2) = "EU"
Table.Rows(6)(2) = "US"
Table.Rows(7)(2) = "EU"
End Sub
///

I hope this helps a little bit?

Cor
 

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

Similar Threads

listbox frenzy 6
2 arrays into 1 2
is there a way to do this 4
database records into an array 2
array in dataset 4
Array question 3
arrays of integers 5
An array and a datagrid 9

Back
Top