views:

708

answers:

2

Hello,

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();

Thanks

+1  A: 

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?

Neal
Can't access the Value attribute, I get this error on compileError 2 'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
wonea
You're right there isn't a 'Value' property for SelectedItem. I changed the above code to reflect this. I don't have access to a compiler to test anything, but I think SelectedItems is what you want to use. This is going to select the item (Row, Object, Whatever) and you will access it based on what Type it is.
Neal
@wonea: What type of object are being selected? Are they they some special class, is it a List, what is it's Type?
Neal
Your second example should not be accessed as a collection. It is a single item.
galford13x
Sorry, getting wires crossed. Just I've tried the code you reproduced, but I get the namespace, function, then column headerthefrontend.ManageFormats+theformatsI populate the datagrid using an inumerated class.
wonea
Selection mode is set to single
wonea
I think more information is needed. Saying it is an enumerated class doesn't mean anything. If your getting the namespace, function etc... then I would expect that the objects ToString() method is using the inherited from object() default and you are getting what you supposed to be getting. We need more information on what you are wanting to do and what objects are being selected. Need more code.
galford13x
Okay, I want the contents-Text of the cell that has been selected.I can get almost there, but I understand why it's not working. When I expand the code dataGrid1.SelectedItems[0] the following can be seen through the context menus; - dataGrid1.SelectedItems Count = 1 -------[0] {thefrontend.Windows1.theformats} -----------theformat "ogg"
wonea
I would expect the dataGrid1.SelectedItems.Count = 1 if your selection mode is single.
galford13x
A: 

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();
wonea