Conditional Formatting on DisplayMember of GridViewComboBoxColumn rather than the ValueMember

4 posts, 0 answers
  1. Matt
    Matt avatar
    10 posts
    Member since:
    Sep 2010

    Posted 27 Apr 2015 Link to this post

    I cannot believe I couldn't find a question about this anywhere... Which usually means I'm missing something extremely simple. I have a RadGridView and I add a column via the following code:

    // Generate Status column
    GridViewComboBoxColumn Status = new GridViewComboBoxColumn();
    Status.Name = "Status";
    Status.HeaderText = "Status";
    Status.FieldName = "Status";
    Status.DataSource = ListsMain.Tables[4];
    Status.DisplayMember = ListsMain.Tables[4].Columns["name"].Caption;
    Status.ValueMember = ListsMain.Tables[4].Columns["id"].Caption;
    Status.DisplayMemberSort = true;
     
    radgridview_troubletickets.Columns.Add(Status);

     

    I really like the conditional formatting option available in the prebuilt form that the RadGridView offers. However, if I want to put a condition on the GridViewComboBoxColumn I added, the condition is based on the ValueMember. This makes no sense to the user, because the value member is just a key from the database, so the end user would never have any idea what the value member is. There has to be an easy way to base the condition on the text selected in the RadViewComoBoxColumn instead of the value of the cell.

     Any advice would be GREATLY appreciated.

  2. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 30 Apr 2015 Link to this post

    Hello Matt,

    Thank you for writing.

    By default, ConditionalFormattingObjects consider the cell value when evaluating the condition. However, you can create a derivative of the ConditionalFormattingObject and override its Evaluate method where the returned result determines whether the condition is met. Thus, you can check the cell's text instead of value. Here is a sample code snippet which result is illustrated on the attached screenshot:
    private void Form1_Load(object sender, EventArgs e)
    {
        this.productsTableAdapter.Fill(this.nwindDataSet.Products);
     
        GridViewComboBoxColumn comboColumn = new GridViewComboBoxColumn("Product");
        comboColumn.DataSource = this.productsBindingSource;
        comboColumn.ValueMember = "ProductID";
        comboColumn.DisplayMember = "ProductName";
        comboColumn.FilteringMode = GridViewFilteringMode.DisplayMember;
        this.radGridView1.Columns.Add(comboColumn);
        this.radGridView1.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;
     
        Random rand = new Random();
        for (int i = 0; i < 30; i++)
        {
            this.radGridView1.Rows.Add(rand.Next(1, 30));
        }
        CustomConditionalFormattingObject obj = new CustomConditionalFormattingObject("MyCondition", ConditionTypes.Contains, "w", "", false);
        obj.CellBackColor = Color.SkyBlue;
        obj.CellForeColor = Color.Red;
        obj.TextAlignment = ContentAlignment.MiddleRight;
        this.radGridView1.Columns["Product"].ConditionalFormattingObjectList.Add(obj);
    }
     
    public class CustomConditionalFormattingObject:ConditionalFormattingObject
    {
        private string p1;
        private ConditionTypes conditionTypes;
        private string p2;
        private string p3;
        private bool p4;
     
        public CustomConditionalFormattingObject(string p1, ConditionTypes conditionTypes, string p2, string p3, bool p4)
            : base(p1,conditionTypes,p2,p3,p4)
        {
        }
     
        public override bool Evaluate(GridViewRowInfo row, GridViewColumn column)
        {
            GridViewComboBoxColumn comboColumn = column as GridViewComboBoxColumn;
            if (comboColumn == null)
            {
                return base.Evaluate(row, column);
            }
            object actualValue = row.Cells[column.Name].Value;
            if (actualValue == null)
            {
                return base.Evaluate(row, column);
            }
            string lookUpValue = comboColumn.GetLookupValue(actualValue).ToString();
            switch (this.ConditionType)
            {
                case ConditionTypes.StartsWith:
                case ConditionTypes.EndsWith:
                case ConditionTypes.Contains:
                case ConditionTypes.DoesNotContain:
                    return StringCompare(lookUpValue, this.TValue1, this.ConditionType);
                default:
                    return false;
            }
        }
     
        private bool StringCompare(object cellValue, string expression, ConditionTypes condition)
        {
            if ((cellValue is DBNull) || (cellValue == null))
                return expression == null;
     
            if (expression == null)
                return cellValue == null;
     
            string stringCellValue = cellValue.ToString();
            string compareExpression = expression;
            if (!this.CaseSensitive)
            {
                stringCellValue = stringCellValue.ToLower();
                compareExpression = compareExpression.ToLower();
            }
     
            bool result = false;
     
            switch (condition)
            {
                case ConditionTypes.Contains:
                    result = stringCellValue.Contains(compareExpression);
                    break;
                case ConditionTypes.DoesNotContain:
                    result = !stringCellValue.Contains(compareExpression);
                    break;
                case ConditionTypes.EndsWith:
                    result = stringCellValue.EndsWith(compareExpression);
                    break;
                case ConditionTypes.StartsWith:
                    result = stringCellValue.StartsWith(compareExpression);
                    break;
            }
     
            return result;
        }
    }

    I hope this information helps. Should you have further questions, I would be glad to help.
     
    Regards,
    Dess
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Matt
    Matt avatar
    10 posts
    Member since:
    Sep 2010

    Posted 30 Apr 2015 in reply to Dess Link to this post

    Thanks for the response! Will this only be possible if I create the conditional formatting objects via code, or can this work with the conditional formatting GUI that is built in that allows end users to define these at runtime?
  5. Dess
    Admin
    Dess avatar
    1609 posts

    Posted 05 May 2015 Link to this post

    Hello Matt,

    Thank you for writing back.

    The introduced approach from my previous reply is a clear solution to achieve the desired behavior. It can be applied even when using the conditional formatting GUI. Note that you can subscribe to the GridViewColumn.ConditionalFormattingObjectList.CollectionChanged event and replace the default ConditionalFormattingObject with the custom one. However, it is important to unsubscribe from the GridViewColumn.ConditionalFormattingObjectList.CollectionChanged event in the event handler when manipulating the GridViewColumn.ConditionalFormattingObjectList collection and subscribe to it again when you are ready with adding the custom ConditionalFormattingObject.

    I hope this information helps. If you have any additional questions, please let me know.

    Regards,
    Dess
    Telerik
     

    See What's Next in App Development. Register for TelerikNEXT.

     
Back to Top