PDA

Click to See Complete Forum and Search --> : [RESOLVED] VB.net/WPF:Return value in second column of data set based on value pulled from first


evanrich
Dec 7th, 2009, 06:58 PM
Ok title sounds confusing, so let me post my code first:

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:

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:
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:
TextBox1.Text = (ds.Tables(0).Rows(ComboBox1.SelectedIndex)).Item(1)

This works :)

jmcilhinney
Dec 7th, 2009, 07:06 PM
You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this: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.

evanrich
Dec 7th, 2009, 08:16 PM
You shouldn't add the data to the ComboBox manually, but rather use data-binding. In WinForms you'd do this: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.

jmcilhinney
Dec 7th, 2009, 09:23 PM
I'm not manually adding linesYes, you are. That's exactly what this is: For Count = 0 To ds.Tables(0).Rows.Count - 1
ComboBox1.Items.Add(ds.Tables(0).Rows(Count)(0))


NextYou'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.

mendhak
Dec 8th, 2009, 02:20 AM
Moved to WPF.