0

我一直在寻找某种机制,该机制允许检测 a 的任何时候DataGridViewRow发生DataGridView变化,一旦DataGridView没有直接的方法来做到这一点。我已经做了这个实现

public partial class MyDatagrid : DataGridView
{
    public event EventHandler<RowChangingArgs> RowUpdating; 
    public MyDatagrid()
    {
        InitializeComponent();
        this.CellBeginEdit += OnCellBeginEdit;
    }

    private DataGridViewRow oldRow;
    private int currentRow;
    private void OnCellBeginEdit(object sender, DataGridViewCellCancelEventArgs args)
    {
        if(oldRow == null || currentRow != args.RowIndex)
        {
            if(currentRow != args.RowIndex && oldRow != null)
            {
                var newRow = this.Rows[args.RowIndex];
                foreach (var cell in oldRow.Cells)
                {
                    foreach (var cell1 in newRow.Cells.Cast<object>().Where(cell1 => !cell.Equals(cell1)))
                    {
                        if(RowUpdating!= null)
                           RowUpdating.Invoke(this, new RowChangingArgs { OldRow = oldRow, NewRow = newRow});
                        oldRow.Dispose();
                        goto called;
                    }
                }
            }
            called:
            oldRow = this.Rows[args.RowIndex].Clone() as DataGridViewRow;
            currentRow = args.RowIndex;
        }
    }

    public class RowChangingArgs : EventArgs
    {
        public DataGridViewRow OldRow { get; set; }
        public DataGridViewRow NewRow { get; set; }
    }
}

示例:
用户编辑第 1 行和第 1 列,通过编辑同一行的 n 列来保持。用户开始编辑其他行。带有第 1 行旧内容和第 1 行新内容的触发事件。

这是这样做的好方法,还是我错过了什么?

4

3 回答 3

1

你能试试这个:

    public string origData { get; set; }


    private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
    {
        if (e.ColumnIndex == 1)
            origData = dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString().Trim(); //Get the original data
    }

    private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
    {
        if (dataGridView1.IsCurrentCellDirty)
        {
            if (e.ColumnIndex == 1)
            {
                if (origData != dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue.ToString().Trim()) //If not equal to original data will trigger
                {
                    //Do stuff
                }
            }
        }
    }
于 2013-02-18T18:04:48.377 回答
1

另一种选择...如果您的数据来自“DataTable”或“DataView”(例如来自后端的查询结果),则网格的绑定数据源可能是 DataView。

如果是这种情况,您总是可以通过它的 columnChanging 或 columnChanged 事件直接从表控件管理中的事件处理程序中获得它……无论您要查找的是哪个(更改前/更改后的值)。

如果是这样,在表级别,在为网格填充查询和填充数据之后,您可以...

YourDataTable.ColumnChanging += MyColumnChanging;

然后有一个方法...

private void MyColumnChanging(object sender, DataColumnChangeEventArgs e)
{
   // just to enforce column name representation, forcing to lower
   string colName = e.Column.ColumnName.ToLower();

   // e.Row has the row that had the change for you to work with, validate, etc...

   switch (colName)
   {
      case "yourcolumnfieldx":
         doSomething;
         break;

      case "anotherfield":
         doSomethingElse;
         break;
   }
}
于 2013-02-18T18:34:12.867 回答
0

Well you can use This which is more faster than other. Use CellEndEdit Event of The DataGridView

        private void datagridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            updateCounter();
        }
于 2019-07-10T18:35:08.437 回答