2010-10-24 59 views
0

我会尽力澄清..在这里我正在研究一个数据网格。在datagrid中,一列是可编辑的,因为我将DataGridTextColumn传递给它,并在用户向其中输入数据并写回数据库时保存数据。我正在使用datagrid_celleditending事件保存到数据库,我也使用datagridcelleditendingeventargs来做到这一点。这样做并且工作正常。我已经添加了新的功能,而不是在datagrid中输入数据,用户将进入一个文本框,将在datagrid顶部和输入数据进入单元格我将它与datagrid单元格同步,我可以看到它的数据(就像excel上面有一个大条,你可以在那里写数据,也可以在datagrid中看到)。我通过使用textbox_keyup事件来做到这一点。现在我想要触发datagrid_celleditending事件参数的事件。我尝试使用路由事件实施,但它不工作。从一个事件触发另一个事件

希望我没有混淆,请帮助..

这里是一个代码块..

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中更改工作正常,现在我想当我在不是数据网格的一部分的文本框中输入数据时触发此操作

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

什么是您设置为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; 
    } 
} 

UPDATE 如果您选择只有一个单元格(这听起来像的情况下),比你能做到这一点。

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; 
} 
+0

我创建了一个辅助类和介绍的方法EnterCellEditEndingForCell在我的代码..但我需要在XAML或Tb_Keyup方法调用此?我没有清楚地理解你什么时候在datagrid中的选择单元。当我在文本框中输入数据时,它使用“rowBeingEdited.Row [”Comments“] = tb_Comments.Text;在datagrid中写入数据”非常感谢你的帮助.. – prem 2010-10-24 20:39:12

+0

另外我忘了提及我使用WPF,但你创建的帮助文件适用于windows窗体..我可以使用WPF也是如此吗?对不起,我是C#编码的初学者,并试图学习的东西..再次感谢帮助.. – prem 2010-10-24 20:55:56

+0

当您在Xaml中设置DataGrid,什么是SelectionUnit设置?它可以是SelectionUnit =“CellOrRowHeader”,SelectionUnit =“Cell”或SelectionUnit =“FullRow”。这将改变SelectionCells,SelectedItem等的行为。是的,辅助类是WPF,你应该在代码中调用这个方法。我更新了我的示例 – 2010-10-24 21:14:51

相关问题