开发者

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


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?


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


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜