简体   繁体   English

C#,空数据源,但仍然是列表框中的一项

[英]C#, Empty Datasource, but still an item in the listbox

I have an listbox with a datasource, which is an table from an ADO.net schema. 我有一个带有数据源的列表框,它是来自ADO.net模式的表。 It all works great, but when there is no item in DataSource, there still is a row in the listbox: 一切都很好,但是当DataSource中没有项目时,列表框中仍然存在一行:

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

How can I prevent this item being added. 如何防止添加此项目。

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

The this.adressenSource var is a BindingSource this.adressenSource var是一个BindingSource

You can Add a condition before binding your Source : 您可以在绑定Source之前添加条件:

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

That happened to me too. 这也发生在我身上。

Within my application I also bound a date to a datetimepicker. 在我的应用程序中,我还将日期绑定到datetimepicker。 But when there is no datasource left in the binding source the datetimepicker would receive the value null. 但是,当绑定源中没有剩余数据源时,datetimepicker将接收值null。 And that is not possible. 那是不可能的。

To prevent that I added my personal "binder" in between that accepts nullable values and forwards the current date instead. 为了防止这种情况,我在两者之间添加了我的个人“活页夹”,该“活页夹”接受可为空的值并转发当前日期。

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());
    }
}

So I could do the databinding: 所以我可以做数据绑定:

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: That also gave me the possibility to show the date as date setting of the executing machine. PS:这也使我有可能将日期显示为执行机器的日期设置。

Hope that helps some lost souls out there. 希望可以帮助一些迷失的灵魂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM