2010-10-24 61 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

相關問題