我有一个包含数据源的列表框,这是一个来自ADO.net
模式的表。
一切都很好,但是当DataSource中没有项目时,列表框中仍然有一行:
System.Collection .Generic.HashSet`1[namespace.class]
如何阻止添加此项目。
lbAdressen.DataSource = this.adressenSource;
lbAdressen.DisplayMember = "DisplayName";
lbAdressen.ValueMember = "Id";
this.adressenSource
var
是BindingSource
答案 0 :(得分:1)
您可以在绑定源之前添加条件:
if (this.adressenSource != null && this.adressenSource.Count() > 0)
{
lbAdressen.DataSource = this.adressenSource;
lbAdressen.DisplayMember = "DisplayName";
lbAdressen.ValueMember = "Id";
}
答案 1 :(得分:0)
这也发生在我身上。
在我的应用程序中,我还将日期绑定到datetimepicker。但是当绑定源中没有数据源时,datetimepicker将接收值null。这是不可能的。
为了防止我在其中添加我的个人“活页夹”,接受可以为空的值并转发当前日期。
class MyDateBinder : INotifyPropertyChanged, IBindableComponent
{
public MyDateBinder()
{
}
private string customFormat;
public string CustomFormat
{
get
{
return customFormat;
}
set
{
if (customFormat != value)
{
customFormat = value;
if (format == DateTimePickerFormat.Custom)
{
UpdateText();
FirePropertyChanged("Text");
}
}
}
}
private DateTimePickerFormat format = DateTimePickerFormat.Short;
public DateTimePickerFormat Format
{
get
{
return format;
}
set
{
if (format != value)
{
format = value;
UpdateText();
FirePropertyChanged("Text");
}
}
}
private string text;
[Bindable(true)]
public string Text
{
get
{
return text;
}
set
{
if (text != value)
{
text = value;
FirePropertyChanged("Text");
UpdateDate();
FirePropertyChanged("Value");
}
}
}
private DateTime date = DateTime.Now;
[Bindable(true)]
public DateTime? Value
{
get
{
return date;
}
set
{
if (date != value)
{
if (value == null)
{
date = DateTime.Now;
}
else
{
date = Convert.ToDateTime(value);
}
FirePropertyChanged("Value");
UpdateText();
FirePropertyChanged("Text");
}
}
}
private void UpdateText()
{
switch (format)
{
case DateTimePickerFormat.Time:
text = date.TimeOfDay.ToString();
break;
default:
case DateTimePickerFormat.Short:
text = date.ToShortDateString() + " " + date.ToShortTimeString();
break;
case DateTimePickerFormat.Long:
text = date.ToLongDateString() + " " + date.ToLongTimeString();
break;
case DateTimePickerFormat.Custom:
text = date.ToString(customFormat);
break;
}
}
private void UpdateDate()
{
if(!DateTime.TryParseExact(text, CustomFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
date = DateTime.Now;
}
}
private BindingContext bindingContext = null;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden),
EditorBrowsable(EditorBrowsableState.Advanced),
Browsable(false)]
public BindingContext BindingContext
{
get
{
if (null == bindingContext)
{
bindingContext = new BindingContext();
}
return bindingContext;
}
set { bindingContext = value; }
}
private ControlBindingsCollection databindings;
[ParenthesizePropertyName(true),
RefreshProperties(RefreshProperties.All),
DesignerSerializationVisibility(DesignerSerializationVisibility.Content),
Category("Data")]
public ControlBindingsCollection DataBindings
{
get
{
if (null == databindings)
{
databindings = new ControlBindingsCollection(this);
}
return databindings;
}
set { databindings = value; }
}
public event PropertyChangedEventHandler PropertyChanged;
private void FirePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public event EventHandler Disposed;
public ISite Site
{
get
{
return null;
}
set
{
throw new NotImplementedException();
}
}
public void Dispose()
{
if (Disposed != null)
Disposed(this, new EventArgs());
}
}
所以我可以进行数据绑定:
BindingSource _bs = new BindingSource();
_bs.DataSource = _someDataTables;
_bs.DataMember = _someDataMember;
releaseTimeBinder = new MyDateBinder();
releaseTimeBinder.CustomFormat = "yyyy-MM-dd HH:mm:ss";
releaseTimeBinder.Format = DateTimePickerFormat.Custom;
DateTimePicker dtp_datetime.DataBindings.Add("Value", releaseTimeBinder, "Value");
releaseTimeBinder.DataBindings.Add("Text", _bs, "date_time");
PS:这也让我可以将日期显示为执行机器的日期设置。
希望能帮助一些失去灵魂的人。