Devexpress GridView conditional cell formatting

Odys picture Odys · May 14, 2012 · Viewed 7.7k times · Source

There is a gridView with Items to buy.

Group
 - Checkbox|Item Description

And there is a maximum of items that can be bought on each group.

I want to change the appearance of all not selected rows, when the max is reached (per group).

example:

Select 1 item from each group

Group 1

  • [ ] Item 1
  • [ ] Item 2

Group 2

  • [ ] Item 3
  • [ ] Item 4
  • [ ] Item 5

After selection

Group 1

  • [x] Item 1
  • [ ] Item 2

Group 2

  • [ ] Item 3
  • [x] Item 4
  • [ ] Item 5

After the max amount of items on each group is checked, I want to alter the appearance of the rest of the items.

I have a group summary for the first column. My problem is that I don't know how to trigger the appearance change of all cells. Is it correct to count selected items on each cell-leave event or is there a better way to accomplish this?

Answer

Grzegorz W picture Grzegorz W · May 15, 2012

I created Devexpress template with GridControl. Person class was created for me. I changed it a little for this example.

public class Person {

    public Person(string firstName, string secondName) {
        this.FirstName = firstName;
        this.SecondName = secondName;
        this.Comments = String.Empty;
    }
    public Person(string firstName, string secondName, string comments)
        : this(firstName, secondName) {
            this.Comments = comments;
    }

    public bool Selected
    {
        get;
        set;
    }

    public bool Blocked
    {
        get;
        set;
    }

    public string FirstName
    {
        get;
        set;
    }
    public string SecondName
    {
        get;
        set;
    }
    public string Comments
    {
        get;
        set;
    }
}

My grid looks like this:

My grid looks like this

And I achived Your functionality with code:

public partial class Form1 : XtraForm
{
    int max = 2;

    public Form1()
    {
        InitializeComponent();
        InitGrid();

    }
    List<Person> gridDataList = new List<Person>();
    void InitGrid()
    {
        gridDataList.Add(new Person("John", "Smith"));
        gridDataList.Add(new Person("Gabriel", "Smith"));
        gridDataList.Add(new Person("Ashley", "Smith", "some comment"));
        gridDataList.Add(new Person("Adrian", "Smith", "some comment"));
        gridDataList.Add(new Person("Gabriella", "Smith", "some comment"));
        gridDataList.Add(new Person("John", "Forester"));
        gridDataList.Add(new Person("Gabriel", "Forester"));
        gridDataList.Add(new Person("Ashley", "Forester", "some comment"));
        gridDataList.Add(new Person("Adrian", "Forester", "some comment"));
        gridDataList.Add(new Person("Gabriella", "Forester", "some comment"));
        bindingSource1.DataSource = gridDataList;
    }

    private void gridView1_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
    {
        int parentHandle = gridView1.GetParentRowHandle(e.RowHandle);
        int count = gridView1.GetChildRowCount(parentHandle);
        int childHandle = -1;
        int nCount = 0;
        for (int i = 0; i < count; i++)
        {
            childHandle = gridView1.GetChildRowHandle(parentHandle, i);
            Person p = gridView1.GetRow(childHandle) as Person;
            if (p != null)
            {
                p.Blocked = false;
                if (p.Selected)
                {
                    nCount++;
                }
            }
        }
        if (nCount == max)
        {
            for (int i = 0; i < count; i++)
            {
                childHandle = gridView1.GetChildRowHandle(parentHandle, i);
                Person p = gridView1.GetRow(childHandle) as Person;
                if (p != null && !p.Selected)
                {
                    p.Blocked = true;
                }
            }
        }
        // to redraw grid
        gridView1.RefreshData();
    }

    private void richedSelected_EditValueChanged(object sender, EventArgs e)
    {
        gridView1.PostEditor();
    }

    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
    {
        Person p = gridView1.GetRow(e.RowHandle) as Person;
        if (p != null && p.Blocked)
        {
            e.Appearance.ForeColor = Color.White;
        }
    }

    private void richedSelected_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
    {
        Person p = gridView1.GetRow(gridView1.FocusedRowHandle) as Person;
        if (p != null && p.Blocked)
        {
            e.Cancel = true;
        }
    }

}

This is of course simplified implementation. Just to get You on the right track.

Elements from designer:

    private DevExpress.XtraGrid.GridControl gridControl;
    private DevExpress.XtraGrid.Views.Grid.GridView gridView1;
    private System.Windows.Forms.BindingSource bindingSource1;
    private DevExpress.XtraGrid.Columns.GridColumn colFirstName;
    private DevExpress.XtraGrid.Columns.GridColumn colSecondName;
    private DevExpress.XtraGrid.Columns.GridColumn colComments;
    private DevExpress.XtraGrid.Columns.GridColumn colSelected;
    private DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit richedSelected;

If You find any better solution please let me know.