【问题标题】:Devexpress GridView conditional cell formattingDevexpress GridView 条件单元格格式
【发布时间】:2012-05-22 04:26:10
【问题描述】:

有一个带有要购买的物品的gridView。

Group
 - Checkbox|Item Description

每个组最多可以购买的物品。

当达到最大值时(每组),我想更改所有未选择行的外观。

示例:

从每个组中选择一项

第一组

  • [ ] 项目 1
  • [ ] 项目 2

第 2 组

  • [ ] 第 3 项
  • [ ] 项目 4
  • [ ] 项目 5

选择后

第一组

  • [x] 项目 1
  • [ ] 项目 2

第 2 组

  • [ ] 第 3 项
  • [x] 项目 4
  • [ ] 项目 5

检查完每个组的最大项目数量后,我想更改其余项目的外观。

我有第一列的组摘要。我的问题是我不知道如何触发所有单元格的外观变化。在每个单元格离开事件中计算所选项目是否正确或有更好的方法来完成此操作?

【问题讨论】:

    标签: c# winforms gridview devexpress appearance


    【解决方案1】:

    我使用 GridControl 创建了 Devexpress 模板。 Person 类是为我创建的。 我为这个例子做了一点改动。

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

    我的网格如下所示:

    我用代码实现了你的功能:

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

    这当然是简化的实现。只是为了让你走上正轨。

    设计师的元素:

        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;
    

    如果您找到更好的解决方案,请告诉我。

    【讨论】:

      【解决方案2】:

      这是我需要的示例link

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多