[RESOLVED] VB.net/WPF:Return value in second column of data set based on value pulled from first
Ok title sounds confusing, so let me post my code first:
Code:
Private Sub ASPCustSelection_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:" & "\;Extended Properties='text;HDR=Yes'"
CommandText = "select * from aspcustomers.txt"
conn = New System.Data.OleDb.OleDbConnection(ConnectionString)
Command = New System.Data.OleDb.OleDbCommand(CommandText, conn)
conn.Open()
da = New OleDbDataAdapter(CommandText, conn)
ds = New DataSet
' fill dataset
da.Fill(ds, "ASPCustomers")
For Count = 0 To ds.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(ds.Tables(0).Rows(Count)(0))
Next
This loads the contents of a CSV file into a dataset, formatted as such:
Quote:
2281jufr97,Allied Technology
5246ydxk84,ASC DET1
and then fills a combo box with the "Company name" pulled from that list. What I need to do however, is pull the company CODE, the number/letter combination, and then pass that as a variable to another function later on.
I thought I had it when i put this code in as a test:
Code:
TextBox1.Text = (ds.Tables(0).columns(ComboBox1.SelectedIndex).ToString)
but after the second value it would crash, saying it couldn't find column "2".
How can i use the selected index from the combo box to pull the matching company code?
EDIT: I fixed it..I needed the following code in my "selection changed" event:
Quote:
TextBox1.Text = (ds.Tables(0).Rows(ComboBox1.SelectedIndex)).Item(1)
This works :)
Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu
You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this:
vb Code:
Me.ComboBox1.DisplayMember = "NameOfColumnToDisplay"
Me.ComboBox1.ValueMember = "NameOfIDColumn"
Me.ComboBox1.DataSource = ds.Tables("ASPCustomers")
Now, when the user makes a selection from the ComboBox, you get the corresponding ID from the control's SelectedValue property.
I've not really used WPF so I'm not sure how it works there but I'd imagine that it's similar. By the way, this site has a forum dedicated to WPF so you should ask such questions there. I've already asked the mods to move this thread.
Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu
Quote:
Originally Posted by
jmcilhinney
You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this:
vb Code:
Me.ComboBox1.DisplayMember = "NameOfColumnToDisplay"
Me.ComboBox1.ValueMember = "NameOfIDColumn"
Me.ComboBox1.DataSource = ds.Tables("ASPCustomers")
Now, when the user makes a selection from the ComboBox, you get the corresponding ID from the control's SelectedValue property.
I've not really used WPF so I'm not sure how it works there but I'd imagine that it's similar. By the way, this site has a forum dedicated to WPF so you should ask such questions there. I've already asked the mods to move this thread.
thanks for the info. I'm not manually adding lines, the code I pasted as a fix will scan each line of the csv file into a dataset and then read the contents into a combo box. if theres 20 lines in the csv, it'll make 20 row. It's dynamic. i changed the order of my code around a bit, but it still works.
Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu
Quote:
Originally Posted by
evanrich
I'm not manually adding lines
Yes, you are. That's exactly what this is:
Code:
For Count = 0 To ds.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(ds.Tables(0).Rows(Count)(0))
Next
You're looping through the rows in the DataTable and manually extracting a value from the row and inserting it into the ComboBox. If you just use data-binding then all that is done for you.
Re: [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pu