获取单元格值RadGrid Wpf

时间:2018-08-03 08:10:37

标签: wpf

我使用此代码从DataGrid Wpf获取单元格值。但这在RadGridView Telerik Wpf中不起作用。

 object item = Grv_Users.SelectedItem;
 if (item == null) { return; }
 string CellID = (Grv_Users.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;

1 个答案:

答案 0 :(得分:0)

对于您在RadGridView中使用的选择单位,它有所不同。

  • 对于SelectionUnit = Row

    var yourItem = gridName.SelectedItem 
    
  • 对于SelectionUnit = Cell

    var yourItem = gridName.SelectedCells.FirstOrDefault().Item
    
上面的

代码分别返回选定行或单元格后面的对象。如果要获取单元格(而不是对象),可以这样做:

   var senderElement = e.OriginalSource as FrameworkElement;
   var cell = senderElement.ParentOfType<GridViewCell>();

或用于行:

   var senderElement = e.OriginalSource as FrameworkElement;
   var row = senderElement.ParentOfType<GridViewRow>();

如果要以编程方式选择单元格,则应进行以下操作:

 var cellInfo = new GridViewCellInfo(object, columnName, gridName);

                if (!gridName.SelectedCells.Contains(cellInfo))
                {
                    gridName.SelectedCells.Add(cellInfo);
                }