c#组合框绑定和第一个null值 [英] c# combobox binding and first null value

查看:167
本文介绍了c#组合框绑定和第一个null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一个小问题,但找不到解决方法...

I got probably a small problem but can't find workaround...

我在winform上有一个组合框,这个组合框绑定到一个列一个数据表。
此列(保留打印机名称)是允许的空值。我想要组合框显示第一个值字符串默认,然后打印机列表。但我不想默认字符串存储在datatable,只是null。

I have a combobox on a winform, and this combobox has binding to a column in a datatable. This column (keeps printer name) is null value allowed. I want the combobox to display a first value string "default" and then the printers list. But I don't want "default" string to be stored in datatable, just null.

cmbDefaultPrinter.DataSource = this.availablePrinters;
cmbDefaultPrinter.DisplayMember = "Display";
cmbDefaultPrinter.ValueMember = "Value";

cmbDefaultPrinter.DataBindings.Add(new Binding("Text", ctr.ds.Tables[t.toTable], "printer"));

其中availablePrinters是此类的列表:

where availablePrinters is a list of this class:

class myPrinters
{
    public string Value { get; set; }
    public string Display { get; set; }

    public myPrinters(string value, string display)
    {
        this.Value = value;
        this.Display = display;
    }
}

availablePrinters中的第一个元素是:myPrinter 默认打印机);

and first element in availablePrinters is: myPrinter(null, "Default printer");

我做错了什么?

推荐答案

p>一些选项:

给定一个myPrinter对象的集合,如List或绑定的DataTable,你可以简单地插入默认值作为集合的第一个元素。

Given a collection of myPrinter objects, like a List or a bound DataTable, you can simply insert the default value as the first element of the collection. Then, when you DataBind, all the current elements are discarded, and the new ones are reloaded with the blank element on top.

你也可以通过覆盖OnDataBound来自定义数据绑定。在添加所有DataTable的元素之后,进入并手动插入默认值。

You can also customize the databind by overriding OnDataBound. After all the DataTable's elements are added, go in and manually insert the default value.

您也可以放弃默认的DataBind行为,并自己填充ComboBox。我在延迟加载组合框的情况下做了这么多次(其中我首先设置一个项目是记录的当前值,然后当他们想要删除列表时,我去检索值并完全填充列表)。这是一个更多的参与;你需要使用Items.Add()基本上滚动自己的数据绑定行为。但是,这不是那么困难,它给你更多的灵活性,包括添加不在DataSource中的项目的能力。

You can also forego the default DataBind behavior, and populate the ComboBox yourself. I've done this many times in the case of "lazy-loading" comboboxes (where I first set one item that is the current value for the record, then when they want to drop the list down I go retrieve the values and fully populate the list). It's a little more involved; you need to basically roll your own data binding behavior using Items.Add(). But, it's not that difficult and it gives you more flexibility, including the ability to add items that aren't in the DataSource.

编辑:在这种情况下(当你想要NULL时,写一个空字符串),我会把以下内容放到一个扩展方法库中,然后在comboBox.SelectedValue()的结尾处调用它,当你检索它

In that case (an empty string is written when you want NULL), I would throw the following into an extension method library, and then stick a call to it on the end of comboBox.SelectedValue() when you're retrieving it to stick into your domain object:

public static string NullIfEmpty(this string input)
{
    return String.IsNullOrEmpty(input) ? null : input;
}

基本上,web控件将null设置为空字符串,其他显示的数据属性,因此他们可以简化其内部工作。但是,他们不会转换回null,所以如果你想要null而不是一个空字符串,你必须自己进行转换。因为上面的方法显示很容易。

Basically, web controls convert null to an empty string when setting text, value or other displayed data properties, so they can simplify their internal workings. But, they don't convert back to null, so if you want null instead of an empty string, you have to make the conversion yourself. As the above method shows it's pretty easy.

这篇关于c#组合框绑定和第一个null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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