也許你可以使用自定義的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);
}
}
}
此鏈接可能會有幫助:http://msdn.microsoft.com/en-US/library/4esb49b4(v=VS.80).aspx – davidsbro
@davidsbro:我沒有任何數據源 - 我只是將一些數據從一個窗口到另一個使用套接字,所以與數據源的例子它不是我想要的 – Anonymous