WPF Toolkit DataGrid SelectionChanged Getting Cell Value

后端 未结 5 836
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 00:13

Please help me, Im trying to get the value of Cell[0] from the selected row in a SelectionChangedEvent.

I am only managing to get lots of different Microsoft.Windows

相关标签:
5条回答
  • 2020-12-09 00:54

    Less code, and it works.

    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            DataGrid dataGrid = sender as DataGrid;
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromIndex(dataGrid.SelectedIndex);
            DataGridCell RowColumn = dataGrid.Columns[ColumnIndex].GetCellContent(row).Parent as DataGridCell;
            string CellValue = ((TextBlock)RowColumn.Content).Text;
        }
    

    ColumnIndex is the index of the column you want to know.

    0 讨论(0)
  • 2020-12-09 01:05

    pls, check if code below would work for you:

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid dataGrid = sender as DataGrid;
        if (e.AddedItems!=null && e.AddedItems.Count>0)
        {
            // find row for the first selected item
            DataGridRow row = (DataGridRow)dataGrid.ItemContainerGenerator.ContainerFromItem(e.AddedItems[0]);
            if (row != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
                // find grid cell object for the cell with index 0
                DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(0) as DataGridCell;
                if (cell != null)
                {
                    Console.WriteLine(((TextBlock)cell.Content).Text);
                }
            }
        }
    }
    
    static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null) child = GetVisualChild<T>(v);
            if (child != null) break;
        }
        return child;
    }
    

    hope this helps, regards

    0 讨论(0)
  • 2020-12-09 01:06

    Since you are using the "SelectionChanged", you can use the sender as a Data Grid:

    DataGrid dataGrid = sender as DataGrid;
    DataRowView rowView = dataGrid.SelectedItem as DataRowView;
    string myCellValue = rowView.Row[0].ToString(); /* 1st Column on selected Row */
    

    I tried the answers posted here and were good, but gave me problems when started to hide columns in the DataGrid. This one works for me even when hiding columns. Hope it works for you too.

    0 讨论(0)
  • 2020-12-09 01:17

    This will give you the current selected row in the DataGrid in WPF:-

    DataRow dtr = ((System.Data.DataRowView)(DataGrid1.SelectedValue)).Row;
    

    Now to get the cell value just write dtr[0], dtr["ID"], etc.

    0 讨论(0)
  • 2020-12-09 01:17
    private void datagrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        DataGrid _DataGrid = sender as DataGrid;
    
        string strEID = _DataGrid.SelectedCells[0].Item.ToString(); 
    }
    
    0 讨论(0)
提交回复
热议问题