2013-08-20 73 views
2

有人可能會建議如何做到這一點?在DataGridView中編輯整行(不只是一個單元格)

目前我有:

  1. DataGridView中有四列 - 文本1 | Text2 | EditButton | SaveButton
  2. 當我在文本1單擊它變成可編輯的字段,我可以 改變其值
  3. 然後我嘗試編輯文本2,但在此單元格中點擊保存我的文本1

變化問題是:當我嘗試編輯第二個字段(Text2)第一個(Text1)失去焦點時,退出編輯模式並保存我做出的更改,同時我想同時將所有更改保存在同一行中。

我想要什麼來實現:

  1. 我按EditButton和所有的細胞變成可編輯的,所以我可以改變行中的任意單元格的值
  2. 更改文本1的值,然後文本2
  3. 的價值
  4. 而且,只有當我按下SaveButton卻派員進行更改

的問題是:如何使所有單元保持在一排在編輯模式下,直到我按特定按鈕?

+1

此鏈接可能會有幫助:http://msdn.microsoft.com/en-US/library/4esb49b4(v=VS.80).aspx – davidsbro

+0

@davidsbro:我沒有任何數據源 - 我只是將一些數據從一個窗口到另一個使用套接字,所以與數據源的例子它不是我想要的 – Anonymous

回答

0

好吧,我知道這可能看起來有點凌亂,但這似乎最簡單的解決方案,我可以拿出來 - 當網格將要編輯模式時,在每個只讀單元上顯示TextBox:

public void DisplayEditors(DataGridView grid, DataGridViewRow row) 
     { 
      foreach (DataGridViewCell cell in row.Cells) 
      { 
       if (cell.ReadOnly == false) 
       { 
        var place = grid.GetCellDisplayRectangle(cell.ColumnIndex, cell.RowIndex, true); 
        var name = string.Format("EDITOR-{0}-{1}", cell.ColumnIndex, cell.RowIndex); 
        var editor = grid.Controls.Find(name, false).FirstOrDefault(); 

        if (editor == null) 
        { 
         editor = new TextBox(); 

         (editor as TextBox).Name = name; 

         grid.Controls.Add(editor); 
        } 
        else 
        { 
         editor.Show(); 
        } 

        editor.Size = place.Size; 
        editor.Location = place.Location; 
        editor.Text = Convert.ToString(cell.Value); 
       } 
      } 
     } 
2

也許你可以使用自定義的DataGridView這樣

public class CustomDGV : DataGridView 
{ 
    private object _cellValue; 
    private Dictionary<int, object[]> _pendingChanges; 

    public CustomDGV() 
    { 
     _pendingChanges = new Dictionary<int, object[]>(); 
    } 

    protected override void OnCellBeginEdit(DataGridViewCellCancelEventArgs e) 
    { 
     // Save the value of the cell before edit 
     _cellValue = this[e.ColumnIndex, e.RowIndex].Value; 

     // If there's already a pending change for that cell, display the edited value 
     if (_pendingChanges.ContainsKey(e.RowIndex)) 
     { 
      this[e.ColumnIndex, e.RowIndex].Value = _pendingChanges[e.RowIndex][e.ColumnIndex]; 
     } 

     base.OnCellBeginEdit(e); 
    } 

    protected override void OnCellEndEdit(DataGridViewCellEventArgs e) 
    { 
     // Adds the edited value of the cell into a dictionary 
     if (!_pendingChanges.ContainsKey(e.RowIndex)) 
     { 
      _pendingChanges.Add(e.RowIndex, new object[this.ColumnCount]); 
     } 

     _pendingChanges[e.RowIndex][e.ColumnIndex] = this[e.ColumnIndex, e.RowIndex].Value; 

     // Display the "old" value 
     this[e.ColumnIndex, e.RowIndex].Value = _cellValue; 
    } 

    public void SavePendingChanges(int rowIndex) 
    { 
     if (_pendingChanges.ContainsKey(rowIndex)) 
     { 
      // Gets the pending changes for that row 
      var rowData = _pendingChanges[rowIndex]; 
      // Update every cell that's been edited 
      for(int i = 0; i < rowData.Length; i++) 
      { 
       if (rowData[i] != null) 
        this[i, rowIndex].Value = rowData[i]; 
      } 
      // Removes the pending changes from the dictionary once it's saved 
      _pendingChanges.Remove(rowIndex); 
     } 
    } 
} 

而且在CellContentClick你可以調用SavePendingChanges()

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if (e.ColumnIndex > -1 && e.RowIndex > -1) 
    { 
     if (e.ColumnIndex == 3) // Save button 
     { 
      dataGridView1.SavePendingChanges(e.RowIndex); 
     } 
    } 
} 
+0

+1的建議,雖然它不是我的情況,因爲我每秒更新網格中的值,所以保持字典中的更改是不夠的對我來說,因爲我還需要在網格處於編輯模式時隱藏更新值(對於用戶來說,即使他已經編輯它,但看到該值仍然在變化) – Anonymous

相關問題