日期:2014-05-20  浏览次数:20743 次

关于捕捉Datagrid一个事件。
C#   winform

datagrid

    ...   Col1     Col2   ...
            12         0

当在Col2中录入值时,如果其值比对应Col1的值大,则提示,且焦点停留在该Col2中,等待用户的其他输入。



------解决方案--------------------
这个问题说来复杂了....
你要做的好这个事情,要从DataGridColumnStyle或直接从DataGridTextBoxColumn继承一个新的列类型,然后重写Commit方法以达到在编辑单元格的时候对用户输入的值精确的处理.
不是一两句代码就写的好的.

如果要是没有要求那么高,那么可以在DataGrid.CurrentCellChanged 事件 来处理这个事情.
------解决方案--------------------
这个你用js代码处理 好一点
------解决方案--------------------
举个例子,CurrentCellChanged事件
private void Handle_CurrentCellChanged(object sender, System.EventArgs e)
{
newCurrentRow = dataGrid1.CurrentCell.RowNumber;
newCurrentCol = dataGrid1.CurrentCell.ColumnNumber;
string newText = dataGrid1[oldCurrentRow, oldCurrentCol].ToString();
if( !IsValidValue(oldCurrentRow, oldCurrentCol, newText))
{
MessageBox.Show( "Entry Error ");
dataGrid1.CurrentCell = new DataGridCell(oldCurrentRow, oldCurrentCol);

}
oldCurrentRow = newCurrentRow;
oldCurrentCol = newCurrentCol;
}
------解决方案--------------------
IsValidValue函数就可以判断这个col2的值的关系了。应该可以实现