【问题标题】:dataGridView bind to List - Convert bool to ImagedataGridView 绑定到列表 - 将布尔转换为图像
【发布时间】:2015-05-16 16:36:37
【问题描述】:

我有一个绑定到我的 dataGridView 的 BindingList。 Item 类是这样的;

public class Item : INotifyPropertyChanged
{
    private string _Name;
    private bool _Active;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Name
    {
        get { return _Name; }
        set {
            _Name = value;
            this.NotifyPropertyChanged("Name");
        }
    }

    public bool Active
    {
        get { return _Active; }
        set {
            _Active = value;
            this.NotifyPropertyChanged("Active");
        }
    }

    private void NotifyPropertyChanged(string name)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

然后我有 Bindinglist 和 dataGridView;

BindingList<Item> ItemList = new BindingList<Item>();
dataGridView1.DataSource = ItemList;

当它为真时,我希望 bool Active 在 dataGridView 上显示为 Checked 图像,否则不显示任何内容。 dataGridView 顶部的按钮允许用户将行标记为活动。

目前 dataGridView 显示一个复选框。如何将项目对象中的布尔值正确绑定到 dataGridView 中的图像?

【问题讨论】:

    标签: c# winforms data-binding datagridview


    【解决方案1】:

    修复它,我将项目类更改为保存图像,而不是尝试在绑定中翻译 bool;

        public Image CheckImage
        {
            get
            {
                if (Active)
                    return Properties.Resources.check;
                else
                    return null;
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-09
      • 1970-01-01
      • 1970-01-01
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 2018-04-20
      相关资源
      最近更新 更多