首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

DataGridView_CellEndEdit编辑事件无法更新到预约单元格

2013-11-30 
DataGridView_CellEndEdit编辑事件无法更新到预定单元格!private void FengeDate_CellEndEdit(object send

DataGridView_CellEndEdit编辑事件无法更新到预定单元格!
private void FengeDate_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = FengeDate.Rows[e.RowIndex].Cells[e.ColumnIndex];//编辑的单元格
            int a;
            int b;
            Int32.TryParse(cell.EditedFormattedValue.ToString(),out fengeshuliang);//取得数值
            Int32.TryParse(FengeDate.Rows[e.RowIndex].Cells[14].Value.ToString(),out kefengeshuliang);//取得数值
            if (a> b)//作比较
            {
                MessageBox.Show("分割数量  大于 可分割数量!请重新输入!", "ERROR");
                FengeDate.CurrentCell = cell;
                FengeDate.BeginEdit(true);
            }
        }
大神们,这个小弟做的一个DataGridView_CellEndEdit  结束编辑的判断事件   
如果不符合   光标要留在原单元格    
但是,当我第一次编辑后,点击其他单元格  跳出编辑     
判断失败后
光标是留在了原的单元格,  但第二次编辑后的值跑到我点的 其他单元格 上面了  求指导!! t编辑事件无法更新到预定单元格! DataGrid_CellEndEdit
[解决办法]
定义两个私有变量
private int cellx = -1;
private int celly = -1;

在CellClick里面写:
if (cellx != -1 && celly != -1)
{
   FengeDate.CurrentCell = FengeDate.Rows[celly].Cells[cellx];
   FengeDate.BeginEdit(true);
}

你的代码修改如下:
private void FengeDate_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            DataGridViewCell cell = FengeDate.Rows[e.RowIndex].Cells[e.ColumnIndex];//编辑的单元格
            int a;
            int b;
            Int32.TryParse(cell.EditedFormattedValue.ToString(),out fengeshuliang);//取得数值
            Int32.TryParse(FengeDate.Rows[e.RowIndex].Cells[14].Value.ToString(),out kefengeshuliang);//取得数值
            if (a> b)//作比较
            {
                MessageBox.Show("分割数量  大于 可分割数量!请重新输入!", "ERROR");
                cellx = e.ColumnIndex; celly = e.RowIndex;
            }
            else
            { cellx = -1; celly = -1; }
        }
[解决办法]

引用:
恩,恩...问题解决了   不过我现在用的是SelectionChanged这个方法   其实原理和你这样一样的.
谢啊
如果按了Tab键之后判断输入错误后,输入焦点还是会停留在原来单元格?

热点排行