Bastiaan
Bastiaan

Reputation: 736

dataGridView bind to List - Convert bool to Image

I have a BindingList of Items that is bound to my dataGridView. The Item class is like this;

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

Then I have the Bindinglist & the dataGridView;

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

I want the bool Active to be shown on the dataGridView as an Checked image when it is true, otherwise display nothing. A button on top of the dataGridView allows users to mark a row as Active.

Currently the dataGridView shows a checkbox. How can I have a correct binding from a bool in the item object to an image in the dataGridView?

Upvotes: 3

Views: 676

Answers (1)

Bastiaan
Bastiaan

Reputation: 736

Fixed it, I changed the item class to hold the image instead of trying to translate the bool in the binding;

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

Upvotes: 3

Related Questions