2014-01-24 47 views
2

我有一個帶有數據源的列表框,這是一個ADO.net模式的表。 這一切都非常順利,但是當在數據源中沒有項目,還有在ListBox中的一行:添加C#空數據源,但仍然是列表框中的項目

System.Collection .Generic.HashSet`1[namespace.class] 

如何防止這個項目。

lbAdressen.DataSource = this.adressenSource; 
lbAdressen.DisplayMember = "DisplayName"; 
lbAdressen.ValueMember = "Id"; 

this.adressenSourcevarBindingSource

回答

1

你可以結合你的源之前添加一個條件:

if (this.adressenSource != null && this.adressenSource.Count() > 0) 
{ 
lbAdressen.DataSource = this.adressenSource; 
lbAdressen.DisplayMember = "DisplayName"; 
lbAdressen.ValueMember = "Id"; 
} 
+0

嗯,我不喜歡這個解決方案,因爲我的數據源可以改變。那麼可能會有另一種解決方案? – WillemT

+0

你是什麼意思你的數據源可能會改變?您要使用的每個數據源都可以在之前進行檢查。 –

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:這也給了我展示的日期作爲執行機器的日期設置的可能性。

希望能幫助那些失落的靈魂在那裏。

相關問題