我会尽力清楚..在这里我正在研究数据网格。在datagrid中,一列是可编辑的,因为我将DataGridTextColumn传递给它,并在用户向其输入数据并写回数据库时保存数据。我正在使用datagrid_celleditending事件保存到数据库,我也使用datagridcelleditendingeventargs来执行此操作。这样做并且工作正常。我已经添加了新的功能,这样用户不会在datagrid中输入数据,而是输入一个文本框,这个文本框将在datagrid上给出,并且输入数据到单元格我正在与datagrid单元格同步,我可以在其中查看数据(就像excel上面有一个大条,你可以在那里写数据,也可以在datagrid中看到)。我这样做是通过使用textbox_keyup事件。现在我想触发datagrid_celleditending事件args的事件。我尝试使用路由事件实现,但它无法正常工作。
希望我不要混淆,请帮助..这是一大堆代码..
private void dataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataRowView rowView = e.Row.Item as DataRowView;
rowBeingEdited = rowView;
//Here my logic to write to database will come
.........
.........
}
//当我在datagrid中更改时,上面的事件工作正常我想在文本框中输入不是datagrid的一部分的数据时触发这个
private void tb_Comments_KeyUp(object sender, KeyEventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.Row["Comments"] = tb_Comments.Text;
//here i wanted to go to the above event and fire it..how can i do it??
}
}
答案 0 :(得分:0)
DataGrid的SelectionUnit设置为什么?这适用于SelectionUnit =“CellOrRowHeader”,否则最后必须稍微更改一些内容。无论如何,下面的代码将一个单元格置于编辑模式,然后提交它以激活CellEditEnding事件。
public void EnterCellEditEndingForCell(DataGridColumn column, DataRowView dataRowView)
{
int selectedRowIndex = -1;
// c_dataGrid.Items.IndexOf(dataRowView)
// sometimes break and return -1.
for (int i = 0; i < c_dataGrid.Items.Count; i++)
{
DataRowView rowView = c_dataGrid.Items[i] as DataRowView;
if (rowView == dataRowView)
{
selectedRowIndex = i;
break;
}
}
int selectedColumnIndex = c_dataGrid.Columns.IndexOf(column);
if (selectedRowIndex >= 0 && selectedColumnIndex >= 0)
{
DataGridCell dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
if (dataGridCell == null)
{
c_dataGrid.ScrollIntoView(dataRowView);
dataGridCell = DataGridHelper.GetCell(c_dataGrid, selectedRowIndex, selectedColumnIndex);
}
dataGridCell.BringIntoView();
dataGridCell.Focus();
c_dataGrid.SelectedCells.Clear();
DataGridCellInfo info = new DataGridCellInfo(dataGridCell);
c_dataGrid.SelectedCells.Add(info);
c_dataGrid.BeginEdit();
c_dataGrid.CommitEdit(DataGridEditingUnit.Cell, true);
}
}
DataGridHelper.cs,如果你没有它的实现
static class DataGridHelper
{
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;
}
static public DataGridCell GetCell(DataGrid dg, int row, int column)
{
DataGridRow rowContainer = GetRow(dg, row);
if (rowContainer != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);
// try to get the cell but it may possibly be virtualized
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
if (cell == null)
{
// now try to bring into view and retreive the cell
dg.ScrollIntoView(rowContainer, dg.Columns[column]);
cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
}
return cell;
}
return null;
}
static public DataGridRow GetRow(DataGrid dg, int index)
{
DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
if (row == null)
{
// may be virtualized, bring into view and try again
dg.ScrollIntoView(dg.Items[index]);
row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(index);
}
return row;
}
}
<强>更新强> 如果您只选择了一个单元格(听起来像这样),那么就可以了。
private void tb_Comments_KeyUp(object sender, KeyEventArgs e)
{
if (rowBeingEdited != null)
{
rowBeingEdited.Row["Comments"] = tb_Comments.Text;
DataGridColumn columnBeingEdited= GetActiveDataGridColumn();
EnterCellEditEndingForCell(columnBeingEdited, rowBeingEdited);
}
}
private DataGridColumn GetActiveDataGridColumn()
{
if (c_dataGrid.SelectedCells.Count != 1)
{
return null;
}
return c_dataGrid.SelectedCells[0].Column;
}