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

datagridview的dataerror事件如何定位cell?

2014-01-28 
datagridview的dataerror事件如何定位是哪个cell出现问题 比如加载某个数据的时候,我希望能在datagridview

datagridview的dataerror事件如何定位是哪个cell出现问题
比如加载某个数据的时候,我希望能在datagridview定位到这个cell,显示红色.
我在dataerror里面用
DataGridView1.CurrentCell.Style.BackColor = Color.Red
不能实现.

谢谢各位提供建议!


以下是msdn里面的dataerror代码

 

VB.NET code
Visual Basic 复制程式码 Private Sub DataGridView1_DataError(ByVal sender As Object, _ByVal e As DataGridViewDataErrorEventArgs) _Handles DataGridView1.DataError    MessageBox.Show("Error happened " _        & e.Context.ToString())    If (e.Context = DataGridViewDataErrorContexts.Commit) _        Then        MessageBox.Show("Commit error")    End If    If (e.Context = DataGridViewDataErrorContexts _        .CurrentCellChange) Then        MessageBox.Show("Cell change")    End If    If (e.Context = DataGridViewDataErrorContexts.Parsing) _        Then        MessageBox.Show("parsing error")    End If    If (e.Context = _        DataGridViewDataErrorContexts.LeaveControl) Then        MessageBox.Show("leave control error")    End If    If (TypeOf (e.Exception) Is ConstraintException) Then        Dim view As DataGridView = CType(sender, DataGridView)        view.Rows(e.RowIndex).ErrorText = "an error"        view.Rows(e.RowIndex).Cells(e.ColumnIndex) _            .ErrorText = "an error"        e.ThrowException = False    End IfEnd Sub 



------解决方法--------------------------------------------------------
ByVal e As DataGridViewDataErrorEventArgs这个事件的参数e里面有两个属性:

e.ColumnIndex, e.RowIndex

估计就是列和行的Index,

dataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor =  = Color.Red
------解决方法--------------------------------------------------------
 

VB.NET code
    Private Sub DataGridView1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles DataGridView1.DataError        DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).Style.BackColor = Color.Red    End Sub
        

热点排行