4

How can I determine SelectedCell's Value In DataGrid? (WPF)

My DataGrid has 9 coloums and 5 rows and I want to know the Value of clicked row[0]'s Value.

I used this code in Windows Form:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    var a = dataGridView1[e.ColumnIndex, e.RowIndex].Value;
}

but I don't know an equivalent code in wpf.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
Shane
  • 128
  • 1
  • 3
  • 15

5 Answers5

3

Determining a selected cell's value is more of a WinForms thing. WPF is designed to work differently; your UI is meant to be separated from logic. The DataGrid thus becomes an instrument for presentation, not something to be poked and prodded for values.

Instead, with WPF, you want to deal with the objects you have bound to the grid themselves, independent of how they are displayed. Forget the grid - just find the object that is currently "selected" by the user out of a list of bound objects.

The SelectedItem is a property on the grid itself and thanks to WPF's superior binding mechanisms, you can bind this value to a property on a ViewModel via XAML:

ItemsSource="{Binding Orders, Mode=OneWay}" 
SelectedItem="{Binding SelectedOrder, Mode=TwoWay}"

When the user selects an item in the grid, the two-way binding will update the SelectedItem property on the ViewModel with the value of that object in that row.

In that way, you don't even have to deal with the knowledge of the grid or the UI.

I hope that makes sense. I know it's a different approach and a different way of thinking coming from WinForms.

Chris Holmes
  • 11,444
  • 12
  • 50
  • 64
3

You should use DataGrid_SelectedCellsChanged event.

    private void dataGrid_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        foreach (var item in e.AddedCells)
        {
            var col = item.Column as DataGridColumn;
            var fc = col.GetCellContent(item.Item);

            if (fc is CheckBox)
            {
                Debug.WriteLine("Values" + (fc as CheckBox).IsChecked);
            }
            else if(fc is TextBlock)
            {
                Debug.WriteLine("Values" + (fc as TextBlock).Text);
            }
            //// Like this for all available types of cells
        }
    }

HTH

Prince Ashitaka
  • 8,623
  • 12
  • 48
  • 71
  • THX a lot Avatar. i want just value of datagrid [selectedrow][first colomn]! i mean that if i clicked on the [3rd row,5th col] it gives me [3rd row, first col] – Shane Oct 01 '10 at 14:22
1

I found a solution posted by others in another thread in stackoverflow: WPF Toolkit DataGrid SelectionChanged Getting Cell Value

Try it.

Community
  • 1
  • 1
soRekE
  • 21
  • 1
0
    private void dataGrid1_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
    {
        var item = e.AddedCells[0];
        {
            var col = item.Column as DataGridColumn;
            var fc = col.GetCellContent(item.Item);

            if (fc is CheckBox)
            {

            }
            else if (fc is TextBlock && col.DisplayIndex == 0)
            {
                textBlock1.Text = (fc as TextBlock).Text;
            }

        }
    }
Shane
  • 128
  • 1
  • 3
  • 15
0

sometimes binding to SelectedItem doesn't work (depending how crazy your Model has to be. I have to transpose the model, so everything is upside down and normal defaults don't work all the time. given that,
in dataGrid selectedCellChanged you could access the bound object by:

assuming from previous example of Orders[] where each Order will have an array of SubOrders

foreach (var selectedCell in e.AddedCells)    
{  
    var order = (Order)selectedCell.Item;
    var subOrder = order.SubOrders[selectedCell.Column.DisplayIndex-1];

    var someValue = subOrder.Value;

}

denis morozov
  • 6,236
  • 3
  • 30
  • 45