的DataGridViewComboBoxCell空值 [英] DataGridViewComboBoxCell Empty Value

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

问题描述

我有一个DGV有几个组合框列。这些列绑定到基本都是在数据库中查找项目对象。我想提供一个空白选项为我的用户到它的应用设置这些查询项目为NULL。我尝试添加一个空项列表,让用户选择空值的各种方法,但它似乎并没有工作。

有没有做到这一点,让我进入我的列表绑定到控制,并且还允许用户输入一个空值,而不必preSS按Ctrl + 0最佳方法是什么?

 公共类RootItem
    公众可覆盖物业查询
    公众可覆盖物业类型为String
    公众可覆盖属性激活布尔
末级

公共类LookupItem
    公众可覆盖物业编号为整数= -1
    公众可覆盖属性的缩写为String =
    公众可覆盖属性名称的String =
    公众可覆盖属性描述的String =

    公共重写功能的ToString()作为字符串
        返回缩写
    端功能

    公众可重写只读属性自
        得到
            返回我
        最终获取
    高端物业
末级

我的数据绑定
'...
私人rootItems作为IList的(的RootItem)= session.QueryOver(RootItem的)的.List

私人查找作为IList的(中LookupItem)= session.QueryOver(中LookupItem)的.List

随着LookupItemColumn
    .DataSource = lookupItems
    .DataPropertyName = PropUtil.GetName(中RootItem)(功能(X)x.LookupItem)
    .DisplayMember = PropUtil.GetName(中LookupItem)(功能(X)x.Abbreviation)
    .ValueMember = PropUtil.GetName(中LookupItem)(功能(X)x.Self)
结束与

myDgv.DataSource =新SortableBindingList(中RootItem)(rootItems)
 

解决方案

我解决这个问题的方法是有所有我被绑定到组合框实现LookupItem接口的项目。

有了这个,我把每一个项目,我想结合并增加了一个虚拟的项目。该名单

然后,我处理的 CellValueChanged 事件,并使用了一些反射来检测,如果该列正在编辑是一个ComboBoxColumn,如果是的话 - 它会得到的属性列是必然,并找出如果它现在的值设置为是我的虚拟物品(我用一个空的ID来确定这一点)。如果是空的项目,它设置该属性(再次使用反射)为Null值。通过这种方式,用户可以方便地设置属性为null,而我的对象仍然保存到数据库正确。它也不会太困难,添加更多的列,因为它只是实现LookupItem接口。

I have a DGV with a few combo box columns. These columns are bound to objects that are basically lookup items in a database. I'd like to provide a blank option for my users to set these lookup items to NULL where it's applicable. I've tried various methods of adding a Null item to the list to allow the user to select the empty value, but it does not seem to work.

Is there a best method to do this that will allow me to bind my list to the control, and also allow the user to enter a null value without having to press Ctrl+0?

Public Class RootItem
    Public Overridable Property Lookup
    Public Overridable Property Type as String
    Public Overridable Property Active as Boolean
End Class

Public Class LookupItem
    Public Overridable Property ID As Integer = -1
    Public Overridable Property Abbreviation As String = ""
    Public Overridable Property Name As String = ""
    Public Overridable Property Description As String = ""

    Public Overrides Function ToString() As String
        Return Abbreviation
    End Function

    Public Overridable ReadOnly Property Self
        Get
            Return Me
        End Get
    End Property
End Class

' My databinding
' ...
private rootItems as IList(of RootItem) = session.QueryOver(of RootItem).List

Private lookups As IList(Of LookupItem) = session.QueryOver(Of LookupItem).List

With LookupItemColumn
    .DataSource = lookupItems
    .DataPropertyName = PropUtil.GetName(Of RootItem)(Function(x) x.LookupItem)
    .DisplayMember = PropUtil.GetName(Of LookupItem)(Function(x) x.Abbreviation)
    .ValueMember = PropUtil.GetName(Of LookupItem)(Function(x) x.Self)
End With

myDgv.DataSource = New SortableBindingList(Of RootItem)(rootItems)

解决方案

My solution to this problem was to have all of the items that I was binding to ComboBoxes implement a LookupItem interface.

With this, I took each of the lists of items that I wanted to bind to and added a "dummy" item.

Then, I handled the CellValueChanged event and used some reflection to detect if the column being edited was a ComboBoxColumn, and if so - it would get the property that the column was bound to, and find out if the value it was now set to was my Dummy item (I determined this by using an empty ID). If it is the empty item, it sets the value of that property (using reflection again) to Null. This way, users can easily set properties to null, and my objects are still persisted to the database properly. It's also not too difficult to add more columns because it just has to implement the LookupItem interface.

这篇关于的DataGridViewComboBoxCell空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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