Frage

Seems like a really simple thing, but all of the research and everything I have tried just doesn't seem to work. I am using VS2013 and .Net 4.5

I have a 2 column WPF DataGrid that isn't using databinding. The grid is already populated with data from a SQLCE table and I want to select a row and return as a string the data in the cell of column 0 of the selected row.

I can see that the data is there in the debug watch window

I just don't know how to access that value so that I can use it.

    private void dgPickList_MouseDoubleClick( object sender, MouseButtonEventArgs e )
    {
        selectItem();
        this.Close();
    }

    private void selectItem()
    {
        _pickedItem = dgPickList.SelectedItem.ToString();
    }

This code obviously doesn't work so well but it does let me see the cell data in a Non-Public members _values field.

I really appreciate any help.

Jeff

War es hilfreich?

Lösung 2

use DataGrid.SelectedCells instead. This will return a collection of selected cells in which you can find the column you need to read.

eg:

var celldata=dataGrid1.SelectedCells[0];
var val=celldata.Column.GetCellContent(celldata.Item);

Andere Tipps

Cast the SelectedItem back to the original type you populated the grid with.

For example, if you use a DataTable, cast to a DataRow and grab the first column (here I'm assuming it's an integer type that represents a unique ID):

var id = ((DataRow)dgPickList.SelectedItem).Field<int>(0);

If you're using a collection of a custom class, something like this should work:

var id = ((MyClass)dgPickList.SelectedItem).Id;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top