tags:

views:

212

answers:

2

Hi,

I have a datagrid with customer data such as ID, name, email, phone etc. When I select a row (with a button or selectionchanged) I want to store that row's columns in variables like

dim email as string dim name as string email = dgCustomers.theselectedrow.theselectedcell name = dgCustomers.theselectedrow.theselectedcell

If I have a datatable with only one row I know I can get column data with: dim email as string = CustomerDataTableInstance.rows(0).Item("Email")

I don't know how to get the selected row number though when I have several rows and the user clicks one/uses the keyboard.

The datagrid is bound to a datatable, not a class with object collections.

Any help is appreciated!

+1  A: 

Did you try the SelectedItem or SelectedIndex properties?

I don't know much about using datatables, but I suspect you can get some kind of row object that represents a row from the datatable, by using SelectedItem, which you can then use to get each of the columns you want--which is just like binding to a collection of objects works.

If that doesn't work, try the SelectedIndex property. I'm pretty sure it exists, but I might be wrong since I never use it--I always just bind my data to SelectedItem.

Benny Jobigan
A: 

I wanted to make a comment but couldn't fit the text I want to write so I have to post it under answers instead.

Hi Benny, Of course, I should have tried the most logic thing first but got too distracted by other problems.

I get the selected rowindex with Dim RowIndex as Integer RowIndex = dgCustomers.SelectedIndex I'm not sure where I should put the code though

I have a method for getting the customer list from a dataset via a web service method Private Sub CustomerDataTable() CustomerDataTableInstance = CustomerService.GetCustomerList me.dgCustomer.DataContext = CustomerDataTableInstance end sub

which I call window_loaded Call CustomerDataTable() then Private Sub dgCustomer_SelectionChanged(Byval sender as Object, ByVal e As System.Windows.Controls.SelectionChangedEventArgs) Handles dg Customer.SelectionChanged Dim RowIndex as Integer RowIndex = dgCustomer.SelectedIndex End Sub

but where do I put the dim email as string email = CustomerDataTableInstance.Rows(RowIndex).Item("Email")?

Peter
The problem I have is to reference the CustomerRowIndex to the CustomerDataTableInstance in the sub CustomerDataTable()
Peter
without seeing your code and knowing what you're trying to do in your program, i can't say for sure, but normally you'd put the code for reading the data of the selected row in the `SelectionChanged` event handler. If you are reading the data directly from the underlying DataTable, then assuming RowIndex from the datagrid matches the row you want in the DataTable, you can put `CustomerDataTableInstance.Rows(RowIndex).Item("Email")` anywhere you need to use that data. You did make `RowIndex` a class-level variable, not a method-level variable, right?
Benny Jobigan