C# 组合框绑定和第一个空值
我可能遇到了一个小问题,但找不到解决方法...
我在 winform 上有一个组合框,并且该组合框绑定到数据表中的列。 该列(保留打印机名称)允许为空值。我希望组合框显示第一个值字符串“默认”,然后显示打印机列表。但我不希望“默认”字符串存储在数据表中,只是空。
cmbDefaultPrinter.DataSource = this.availablePrinters;
cmbDefaultPrinter.DisplayMember = "Display";
cmbDefaultPrinter.ValueMember = "Value";
cmbDefaultPrinter.DataBindings.Add(new Binding("Text", ctr.ds.Tables[t.toTable], "printer"));
其中 availablePrinters 是此类的列表:
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(null, "Default Printer");
我做错了什么?
I got probably a small problem but can't find workaround...
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"));
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;
}
}
and first element in availablePrinters is: myPrinter(null, "Default printer");
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一些选项:
给定一个 myPrinter 对象的集合,例如 List 或绑定的 DataTable,您可以简单地插入默认值作为集合的第一个元素。然后,当您进行数据绑定时,所有当前元素都将被丢弃,并重新加载新元素,并将空白元素放在顶部。
您还可以通过重写 OnDataBound 来自定义数据绑定。添加完DataTable 的所有元素后,进入并手动插入默认值。
您还可以放弃默认的 DataBind 行为,并自行填充 ComboBox。在“延迟加载”组合框的情况下,我已经这样做了很多次(我首先设置一个项目,即记录的当前值,然后当他们想要下拉列表时,我会检索值并完全填充列表)。它涉及更多一点;您基本上需要使用 Items.Add() 来滚动自己的数据绑定行为。但是,这并不困难,并且它为您提供了更大的灵活性,包括添加数据源中没有的项目的能力。
编辑: 在这种情况下(当你想要 NULL 时会写入一个空字符串),我会将以下内容放入扩展方法库中,然后在comboBox.SelectedValue( 的末尾粘贴对它的调用) )当您检索它以粘贴到域对象中时:
基本上,Web 控件在设置文本、值或其他显示的数据属性时将 null 转换为空字符串,因此它们可以简化其内部工作。但是,它们不会转换回 null,因此如果您想要 null 而不是空字符串,则必须自己进行转换。正如上面的方法所示,这非常简单。
Some options:
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.
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.
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.
EDIT: 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:
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.