我在查找DataGrid
中刚编辑过的单元格的行和单元索引时遇到问题。
我正在使用CellEditEnding
事件来了解单元格的编辑时间。
Col1
包含属性DisplayIndex
,这是所选列的索引,但我找不到相同的方式。
private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
}
答案 0 :(得分:4)
我现在已经开始工作了。这就是它的样子:
private void DataGridData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn col1 = e.Column;
DataGridRow row1 = e.Row;
int row_index = ((DataGrid)sender).ItemContainerGenerator.IndexFromContainer(row1);
int col_index = col1.DisplayIndex;
}
答案 1 :(得分:0)
迟到的答案,但对于发现此问题的任何人来说,此代码都可以通过将此事件添加到您的数据网格来实现:
private void dgMAQ_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DependencyObject dep = (DependencyObject)e.EditingElement;
string newText = string.Empty;
//Check if the item being edited is a textbox
if (dep is TextBox)
{
TextBox txt = dep as TextBox;
if (txt.Text != "")
{
newText = txt.Text;
}
else
{
newText = string.Empty;
}
}
//New text is the new text that has been entered into the cell
//Check that the value is what you want it to be
double isDouble = 0;
if (double.TryParse(newText, out isDouble) == true)
{
while ((dep != null) && !(dep is DataGridCell))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
if (dep is DataGridCell)
{
DataGridCell cell = dep as DataGridCell;
// navigate further up the tree
while ((dep != null) && !(dep is DataGridRow))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null)
return;
DataGridRow row = dep as DataGridRow;
int rowIndex = row.GetIndex();
int columnIndex = cell.Column.DisplayIndex;
//Check the column index. Possibly different save options for different columns
if (columnIndex == 3)
{
if (newText != string.Empty)
{
//Do what you want with newtext
}
}
}
答案 2 :(得分:0)
要获取没有'sender'参数的行索引,您可以尝试:
Convert.ToInt32(e.Row.Header)
结合Patryk答案的缩写形式,给出了:
private void DataGridData_CellEditEnding(DataGridCellEditEndingEventArgs e)
{
int col1 = e.Column.DisplayIndex;
int row1 = Convert.ToInt32(e.Row.Header);
}