WPF Datagrid获取选定的单元格值

Abd*_*rif 13 c# wpf datagrid

我想在datagrid中获取所选单元格的值,请任何人告诉如何执行此操作.我使用了SelectedCell改变了事件,我怎么能这样做?

dataGrid1.CurrentCell
Run Code Online (Sandbox Code Playgroud)

小智 15

当我遇到这个问题时,我这样接近它:我创建了一个DataRowView,抓住了列索引,然后在行中使用了它ItemArray

DataRowView dataRow = (DataRowView)dataGrid1.SelectedItem;
int index = dataGrid1.CurrentCell.Column.DisplayIndex;
string cellValue = dataRow.Row.ItemArray[index].ToString();
Run Code Online (Sandbox Code Playgroud)

  • 这仅在`SelectionUnit`设置为`DataGridSelectionUnit.FullRow`时有效 (2认同)
  • 这有一个错误。如果用户移动网格中的列,则 DisplayIndex 可能会更改。您想要的是绑定,而不是显示索引。此代码将在列移动后继续存在:`var columnName = ((Binding)((DataGridBoundColumn)dataGrid1.CurrentCell.Column).Binding).Path.Path; var cellValue = dataRow.Row[列名];` (2认同)

She*_*dan 13

请参阅MSDN上的DataGrid类页面.从该页面:

选择

默认情况下,当用户单击DataGrid中的单元格时,将选择整行,并且用户可以选择多行.您可以设置SelectionMode属性以指定用户是否可以选择单元格,完整行或两者.设置SelectionUnit属性以指定是选择多行还是单元格,还是仅选择单行或单元格.

您可以获取有关从SelectedCells属性中选择的单元格的信息.您可以在SelectedCellsChanged事件的SelectedCellsChangedEventArgs中获取有关已更改选择的单元格的信息.调用SelectAllCells或UnselectAllCells方法以编程方式选择或取消选择所有单元格.有关更多信息,请参阅DataGrid控件中的默认键盘和鼠标行为.

我已经为您添加了相关属性的链接,但我现在已经没时间了,所以我希望您可以按照链接获取解决方案.

  • 从您提供给SO的所有内容来看,这很棒,但这并不是真正的答案,而是“在这里查找”的答案。 (2认同)

Rus*_*oni 13

如果您只选择一个单元格,则获取此选定的单元格内容

var cellInfo = dataGrid1.SelectedCells[0];

var content = cellInfo.Column.GetCellContent(cellInfo.Item);
Run Code Online (Sandbox Code Playgroud)

这里的内容将是您选择的单元格值

如果您选择多个单元格,那么您可以这样做

var cellInfos = dataGrid1.SelectedCells;

var list1 = new List<string>();

foreach (DataGridCellInfo cellInfo in cellInfos)
{
    if (cellInfo.IsValid)
    {
        //GetCellContent returns FrameworkElement
        var content= cellInfo.Column.GetCellContent(cellInfo.Item); 

        //Need to add the extra lines of code below to get desired output

        //get the datacontext from FrameworkElement and typecast to DataRowView
        var row = (DataRowView)content.DataContext;

        //ItemArray returns an object array with single element
        object[] obj = row.Row.ItemArray;

        //store the obj array in a list or Arraylist for later use
        list1.Add(obj[0].ToString());
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 8

I'm extending the solution by Rushi to following (which solved the puzzle for me)

var cellInfo = Grid1.SelectedCells[0];
var content = (cellInfo.Column.GetCellContent(cellInfo.Item) as TextBlock).Text;
Run Code Online (Sandbox Code Playgroud)


Cha*_*ton 5

如果SelectionUnit="Cell"尝试这样:

    string cellValue = GetSelectedCellValue();
Run Code Online (Sandbox Code Playgroud)

哪里:

    public string GetSelectedCellValue()
    {
        DataGridCellInfo cellInfo = MyDataGrid.SelectedCells[0];
        if (cellInfo == null) return null;

        DataGridBoundColumn column = cellInfo.Column as DataGridBoundColumn;
        if (column == null) return null;

        FrameworkElement element = new FrameworkElement() { DataContext = cellInfo.Item };
        BindingOperations.SetBinding(element, TagProperty, column.Binding);

        return element.Tag.ToString();
    }
Run Code Online (Sandbox Code Playgroud)

好像不应该那么复杂,我知道...

编辑:这似乎不适用于DataGridTemplateColumn类型列。如果您的行由一个自定义类组成,并且已分配了一个排序成员路径,则也可以尝试以下操作:

    public string GetSelectedCellValue()
    {
        DataGridCellInfo cells = MyDataGrid.SelectedCells[0];

        YourRowClass item = cells.Item as YourRowClass;
        string columnName = cells.Column.SortMemberPath;

        if (item == null || columnName == null) return null;

        object result = item.GetType().GetProperty(columnName).GetValue(item, null);

        if (result == null) return null;

        return result.ToString();
    }
Run Code Online (Sandbox Code Playgroud)