本文介绍了从Datagrid单元格获取字符串值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我成功获取了所选行和我想要的单元格信息。
但它看起来像System.Windows.Controls.DataGridCell:ABCDEFG
我只需要ABCDEFG部分作为字符串变量使用。
我已经尝试了所有的铸造和转换方法没有成功...
错误1无法将类型'System.Windows.Controls.DataGridCell'隐式转换为'string'
任何人都可以协助请
大班使用的扩展方法:
private void dataGrid1_SelectionChanged(object sender,SelectionChangedEventArgs e)
{
var selectedRow = dataGrid1.GetSelectedRow();
var columnCellSC = dataGrid1.GetCell(selectedRow,0);
//这就是我需要在剩余的代码中使用返回值作为字符串
}
附加类:
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Windows.Media;使用System.Windows.Controls
;
使用System.Windows.Controls.Primitives;
使用System.Threading.Tasks;
使用System.Data;
名称空间DataGridHelpers
{
///< summary>
/// DataGrid的扩展方法
///< / summary>
公共静态类DataGridHelper
{
///< summary>
///获取元素的可视子元素
///< / summary>
///< typeparam name =T>预期类型< / typeparam>
///< param name =parent>预期元素的父级< / param>
///< returns>可视子项< / returns>
public static T GetVisualChild< T>(Visual parent)其中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为T;
if(child == null)
{
child = GetVisualChild< T>(v);
}
if(child!= null)
{
break;
}
}
返回孩子;
}
///< summary>
///获取DataGrid
///< / summary>的指定单元格
///< param name =dataGrid1> DataGrid实例< / param>
///< param name =row>单元格的行< / param>
///< param name =column>单元格的列索引< / param>
///< returns> DataGrid的单元格< / returns>
public static DataGridCell GetCell(this DataGrid dataGrid1,DataGridRow row,int column)
{
if(row!= null)
{
DataGridCellsPresenter presenter = GetVisualChild< DataGridCellsPresenter> ;(行);
if(presenter == null)
{
dataGrid1.ScrollIntoView(row,dataGrid1.Columns [column]);
presenter = GetVisualChild< DataGridCellsPresenter>(row);
}
DataGridCell cell =(DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
返回单元格;
}
返回null;
}
///< summary>
///获取DataGrid
///< / summary>的指定单元格
///< param name =grid> DataGrid实例< / param>
///< param name =row>单元格的行索引< / param>
///< param name =column>单元格的列索引< / param>
///< returns> DataGrid的单元格< / returns>
public static DataGridCell GetCell(this DataGrid grid,int row,int column)
{
DataGridRow rowContainer = grid.GetRow(row);
返回grid.GetCell(rowContainer,column);
}
///< summary>
///获取DataGrid的指定行
///< / summary>
///< param name =grid> DataGrid实例< / param>
///< param name =index>行的索引< / param>
///< returns> DataGrid的一行< / returns>
public static DataGridRow GetRow(this DataGrid grid,int index)
{
DataGridRow row =(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
if(row == null)
{
//可以虚拟化,进入视图并重试。
grid.UpdateLayout();
grid.ScrollIntoView(grid.Items [index]);
row =(DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
}
返回行;
}
///< summary>
///获取DataGrid的选定行
///< / summary>
///< param name =grid> DataGrid实例< / param>
///< returns>< / returns>
public static DataGridRow GetSelectedRow(this DataGrid grid)
{
return(DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
}
}
}
解决方案
I successfully get the row selected and the cell info I want.
But it looks like this "System.Windows.Controls.DataGridCell: ABCDEFG"
I only need the ABCDEFG part to use as a string variable.
I’ve tried all the casting and conversion methods with no success…
Error 1 Cannot implicitly convert type 'System.Windows.Controls.DataGridCell' to 'string'
can anyone assist please
Extension methods used from main class:
private void dataGrid1_SelectionChanged(object sender, SelectionChangedEventArgs e) { var selectedRow = dataGrid1.GetSelectedRow(); var columnCellSC = dataGrid1.GetCell(selectedRow, 0); //THIS IS WHERE I NEED TO BE ABLE TO USE THE RETURNED VALUES AS STRINGS IN THE REST OF MY CODE }
ADDITIONAL CLASS:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Media; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Threading.Tasks; using System.Data; namespace DataGridHelpers { /// <summary> /// Extension methods for DataGrid /// </summary> public static class DataGridHelper { /// <summary> /// Gets the visual child of an element /// </summary> /// <typeparam name="T">Expected type</typeparam> /// <param name="parent">The parent of the expected element</param> /// <returns>A visual child</returns> public 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; } /// <summary> /// Gets the specified cell of the DataGrid /// </summary> /// <param name="dataGrid1">The DataGrid instance</param> /// <param name="row">The row of the cell</param> /// <param name="column">The column index of the cell</param> /// <returns>A cell of the DataGrid</returns> public static DataGridCell GetCell(this DataGrid dataGrid1, DataGridRow row, int column) { if (row != null) { DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row); if (presenter == null) { dataGrid1.ScrollIntoView(row, dataGrid1.Columns[column]); presenter = GetVisualChild<DataGridCellsPresenter>(row); } DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column); return cell; } return null; } /// <summary> /// Gets the specified cell of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <param name="row">The row index of the cell</param> /// <param name="column">The column index of the cell</param> /// <returns>A cell of the DataGrid</returns> public static DataGridCell GetCell(this DataGrid grid, int row, int column) { DataGridRow rowContainer = grid.GetRow(row); return grid.GetCell(rowContainer, column); } /// <summary> /// Gets the specified row of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <param name="index">The index of the row</param> /// <returns>A row of the DataGrid</returns> public static DataGridRow GetRow(this DataGrid grid, int index) { DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); if (row == null) { // May be virtualized, bring into view and try again. grid.UpdateLayout(); grid.ScrollIntoView(grid.Items[index]); row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index); } return row; } /// <summary> /// Gets the selected row of the DataGrid /// </summary> /// <param name="grid">The DataGrid instance</param> /// <returns></returns> public static DataGridRow GetSelectedRow(this DataGrid grid) { return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem); } } }
解决方案
这篇关于从Datagrid单元格获取字符串值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!