|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Binding an Array to a Combo BoxI've loaded a two-dimensional array (168 by 28) into memory as AcctArray. {Dim AcctArray (500,28) as string...} The AcctArray is loaded from a Quickbooks table, so there's no intrinsic dataset to assign as datasource.... I want to have a combo table (or list table or whatever), currently named cbAccounts, that can scroll through and maybe select items in the array. I don't want to display all elements, just columns 1,2,6, and 13... How do I construct this? A combo box is supposed to have as its datasource an "array", but I can't seem to accomplish it. I can do cbAccounts.Items.Add(AcctArray(j,1) & " " & AcctArray(j,2) & " " & Acctarray(j,6) & " " & AcctArray(j,13)) but that doesn't accomplish my objectives... Can someone give me a nudge? Thanx in advance -- Jim Shaffer You're on the right track. The datasource must implement IList.
As you concatenate the items you want to display from the array, use a loop to add each resulting string to an ArrayList. Then set the Combobox datasource to the arraylist. When you select an item from the dropdown, the selectedindex property will correspond to the second dimension index of your original array, assuming you use each item in the original array. You might also look into the displaymember and valuemember properties of the ComboBox. If you wanted to use them you might use a Datatable instead of an ArrayList. You would then assign those two properties to the appropriate fields. This approach allows you to use the SelectedValue property of the ComboBox. www.charlesfarriersoftware.com Show quoteHide quote "Jim Shaffer" wrote: > Perhaps I have the wrong construct, or misunderstand arrays in vb (2003).... > I've loaded a two-dimensional array (168 by 28) into memory as AcctArray. > {Dim AcctArray (500,28) as string...} > > The AcctArray is loaded from a Quickbooks table, so there's no intrinsic > dataset to assign as datasource.... > > I want to have a combo table (or list table or whatever), currently named > cbAccounts, that can scroll through and maybe select items in the array. I > don't want to display all elements, just columns 1,2,6, and 13... > > How do I construct this? A combo box is supposed to have as its datasource > an "array", but I can't seem to accomplish it. I can do > > cbAccounts.Items.Add(AcctArray(j,1) & " " & AcctArray(j,2) & " " & > Acctarray(j,6) & " " & AcctArray(j,13)) > > but that doesn't accomplish my objectives... > > > Can someone give me a nudge? > > Thanx in advance > > -- > Jim Shaffer > > > Clarification:
Actually, when you are concatenating, you are probably already in a nested loop...That's the loop I meant. Just stash the concatenation result in a string, then add the string to the arraylist... MyArrayList.Add(MyString) The real key here is using an object that implements IList. Show quoteHide quote "Jim Shaffer" wrote: > Perhaps I have the wrong construct, or misunderstand arrays in vb (2003).... > I've loaded a two-dimensional array (168 by 28) into memory as AcctArray. > {Dim AcctArray (500,28) as string...} > > The AcctArray is loaded from a Quickbooks table, so there's no intrinsic > dataset to assign as datasource.... > > I want to have a combo table (or list table or whatever), currently named > cbAccounts, that can scroll through and maybe select items in the array. I > don't want to display all elements, just columns 1,2,6, and 13... > > How do I construct this? A combo box is supposed to have as its datasource > an "array", but I can't seem to accomplish it. I can do > > cbAccounts.Items.Add(AcctArray(j,1) & " " & AcctArray(j,2) & " " & > Acctarray(j,6) & " " & AcctArray(j,13)) > > but that doesn't accomplish my objectives... > > > Can someone give me a nudge? > > Thanx in advance > > -- > Jim Shaffer > > > Jim,
That usefull however terrible combobox have 2 methods for binding and one method to add items. It is the adding of items the binding from the textboxpart (what is not your option) the binding using the datasource to an Ilist array You go now for adding items from the array to the arraylist from the combobox. That is not directly binding. What you can do is creating an array with objects. Those you can bind using the datasource to the combobox. You can get the information by casting it to a datarowview. Because this I find more work than needed, I make in this kind of situations just a datatable and bind it using the defaultview to that combobox. That cost at least 10 times less time to do. Just my thought, Cor Jim,
In addition to the other comments. | How do I construct this? A combo box is supposed to have as its datasource I've bound to single dimension arrays without any problems, I have not tried | an "array", but I can't seem to accomplish it. I can do binding to either 2 dimensional arrays or jagged/ragged arrays before. I will see if I can find any information on binding to a 2 dimensional array. In the meantime, you could always convert the array into a DataTable. Something like: Const rows As Integer = 0 Const columns As Integer = 1 Dim table As DataTable For rowIndex As Integer = acctArray.GetLowerBound(rows) To acctArray.GetUpperBound(rows) Dim row As DataRow = table.NewRow For columnIndex As Integer = acctArray.GetLowerBound(columns) To acctArray.GetUpperBound(columns) row(columnIndex) = acctArray(rowindex, columnindex) Next table.Rows.Add(row) Next Which assumes that "table" has the same number of columns as your array. Hope this helps Jay Show quoteHide quote "Jim Shaffer" <use***@shafferassoc.com> wrote in message news:jOqdnXBEB-4iwsHfRVn-3g@comcast.com... | Perhaps I have the wrong construct, or misunderstand arrays in vb (2003).... | I've loaded a two-dimensional array (168 by 28) into memory as AcctArray. | {Dim AcctArray (500,28) as string...} | | The AcctArray is loaded from a Quickbooks table, so there's no intrinsic | dataset to assign as datasource.... | | I want to have a combo table (or list table or whatever), currently named | cbAccounts, that can scroll through and maybe select items in the array. I | don't want to display all elements, just columns 1,2,6, and 13... | | How do I construct this? A combo box is supposed to have as its datasource | an "array", but I can't seem to accomplish it. I can do | | cbAccounts.Items.Add(AcctArray(j,1) & " " & AcctArray(j,2) & " " & | Acctarray(j,6) & " " & AcctArray(j,13)) | | but that doesn't accomplish my objectives... | | | Can someone give me a nudge? | | Thanx in advance | | -- | Jim Shaffer | | Jim,
I was playing with this a little. Is this Windows Forms or Web Forms? In Windows Forms, you can bind a list control (ListBox, ComboBox, DataGrid) to a one-dimensional array, however you cannot bind to a two-dimensional array. Which is understandable, as the list controls use IList to support binding. Array implements IList. If you use IList from a one-dimensional array you get the list of numbers. If you use IList from a two-dimensional array you get a single list of numbers & not the rows & columns... Try the following: Dim values(,) As Integer = {{11, 12}, {21, 22}, {31, 32}} Dim list As IList = values For Each item As Object In list Debug.WriteLine(item) Next What values are printed? The short of it you cannot bind to a two-dimensional array. I would recommend you convert the two-dimensional array to either a one-dimensional array, an ArrayList or a DataTable... Hope this helps Jay Show quoteHide quote "Jim Shaffer" <use***@shafferassoc.com> wrote in message news:jOqdnXBEB-4iwsHfRVn-3g@comcast.com... | Perhaps I have the wrong construct, or misunderstand arrays in vb (2003).... | I've loaded a two-dimensional array (168 by 28) into memory as AcctArray. | {Dim AcctArray (500,28) as string...} | | The AcctArray is loaded from a Quickbooks table, so there's no intrinsic | dataset to assign as datasource.... | | I want to have a combo table (or list table or whatever), currently named | cbAccounts, that can scroll through and maybe select items in the array. I | don't want to display all elements, just columns 1,2,6, and 13... | | How do I construct this? A combo box is supposed to have as its datasource | an "array", but I can't seem to accomplish it. I can do | | cbAccounts.Items.Add(AcctArray(j,1) & " " & AcctArray(j,2) & " " & | Acctarray(j,6) & " " & AcctArray(j,13)) | | but that doesn't accomplish my objectives... | | | Can someone give me a nudge? | | Thanx in advance | | -- | Jim Shaffer | |
Translate C# to VB.net?
Splash window XmlTextReader not getting all Elements SqlDataReader accessed only by field ordinals? Singleton Pattern for Database Access --- Leave Open or Close Connection Combobox and DataSource "Add Reference" hosed--doesn't display the dialog in VS.NET PDF to Picturebox Newbie: substrings VB.net Datagrid Parent Row |
|||||||||||||||||||||||