AllFallD0wn
AllFallD0wn

Reputation: 551

WPF - Get data in first column of a selected row

I have a DataGrid for search results, that I want the user to be able to click on a row, and it load that customer's details. The first index of the row (index position 0) has the ID in, so once I get the selected row it will be very simple, however, I'm having trouble extracting this information. Is there a way to do something like:

string ID = myGrid.selectedRow[0].ToString();

I already have the selectionChanged event programmed and triggering, I just can't seem to get the data out..

Upvotes: 0

Views: 4311

Answers (2)

Bob
Bob

Reputation: 21

There is really a very simple way to do this using SelectedIndex.

int i = yourgrid.SelectedIndex;
DataRowView v = (DataRowView)yourgrid.Items[i];  // this give you access to the row
string s = (string)v[0];  // this gives you the value in column 0.

You can also do: string s = (string)v["columnname"];
this protects you from the user moving the column around

Upvotes: 1

Tigran
Tigran

Reputation: 62246

I see the tag WPF, that means that you are using DataBinding, that means that you have a ModelView or at least Model. Having this architecture, especially in WPF, never and ever read the data from UI, read it from the bound data-model.

Upvotes: 2

Related Questions