处理绑定到数据的DataGridView单元 [英] Handling DataGridView Cells Bound to Data

查看:161
本文介绍了处理绑定到数据的DataGridView单元的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过XML(如果文件存在)或数据源(如果文件不存在)在VB.Net中加载DataGridView.如果单元格值为"N/A",我想对该单元格应用单元格样式并将其设置为只读.在用户编辑期间,单元格可能会获得N/A值时,我还会触发某些事件.我可以更直接地控制样式的设置和只读设置,但是我希望以一种没有潜在漏洞"的方式来进行设置,其中"N/A"是默认样式并且可以编辑. br/>
我已经在datagridview中探索了许多不同的单元事件,但是在分配数据源时它们不会触发.我也尝试过使用DataGridViewEditingControlShowingEvent,在其中我DirectCast一个textbox textchanged事件,但这似乎也只在用户编辑时发生.

有人可以帮我想出一种方法,在分配数据源并且单元格中的值="N/A"时,根据单元格值有条件地分配单元格样式和只读属性吗?

I am loading a DataGridView in VB.Net via either XML (if the file exists) or a datasource (if it doesn''t). If a cell value is "N/A", I want to apply a cellstyle to that cell and make it readonly. I also have certain events that are being fired during user edit where the cell may get the N/A value. I''m able to more directly control setting the style and readonly there, but would like to do it in a manner where there are no potential "loop holes", where "N/A" has the default style and is editable.

I''ve explored many different cell events in the datagridview, but they don''t fire when assigning the datasource. I''ve also tried using DataGridViewEditingControlShowingEvent, where I DirectCast a textbox textchanged event, but that seems to only occur on user edits as well.

Can someone help me come up with a way to assign a cellstyle and readonly property conditionally based on cell value upon assigning a datasource and the value in the cell = "N/A"?

推荐答案

尝试类似的方法:
Try some thing like:
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) _
Handles DataGridView1.CellBeginEdit
   If Not IsNumeric(DataGridView1.Item(e.ColumnIndex, e.RowIndex).Value) Then
      e.Cancel = True
   End If
End Sub 


这篇关于处理绑定到数据的DataGridView单元的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆