wonea
wonea

Reputation: 4969

WPF Datagrid Get Selected Item

How do I get the selected item in a WPF datagrid? Tried the following, with no luck;

dataGrid1.CurrentCell.Item.ToString();
string[] strsplit = dataGrid1.SelectedValue.ToString().Split('+');
dataGrid1.SelectedCells[0].Item.ToString();
dataGrid1.CurrentItem.ToString();

dataGrid1.CurrentCell.Item.ToString();
dataGrid1.CurrentCell.Item.ToString();

Upvotes: 1

Views: 17929

Answers (3)

Andre
Andre

Reputation: 11

DataRowView myRow = (DataRowView)dataGrid.CurrentCell.Item;
string myvalue = Convert.ToInt32(linha.Row.ItemArray[0].ToString());

Upvotes: 1

wonea
wonea

Reputation: 4969

Found a way of creating an object based on the line. Then it's possible to access the field within the datagrid directly.

   theformats lineobject = (theformats)groups_dataGrid1.CurrentCell.Item;
   string linetext = lineobject.theformat.ToString();

Upvotes: 1

Neal
Neal

Reputation: 599

I'm not sure I fully understand your example code above.

What is the first statement supposed to do? alone it wont do anything.

To get the selected value have you tried.

var myValue = dataGrid1.SelectedItems[0].ToString();  // I'm not sure what type you expecting It looks like a string.

Edit: What Selection Mode is the DataGrid set to? If it is extended then I would expect the above to work. If set to single mode.

var myValue = dataGrid1.SelectedItem[0].ToString();

Edit2: What type of object are in the DataGrid? What are you selecting?

Upvotes: 3

Related Questions