Search code examples
c#wpfdatagridcell

Access cell data in a DataGrid


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


Solution

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