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 columnGridViewComboBoxColumn 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.
7 Answers, 1 is accepted
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.
Dess
Telerik
See What's Next in App Development. Register for TelerikNEXT.
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.
Hello,
would it be possible to have a sample code for the CollectionChanged event handler for replacing the default ConditionalFormattingObject with the custom one in order to have it working with the conditional formatting GUI?
Thanks
Luca
You can find attached a sample project demonstrating how to replace the default ConditionalFormattingObject with a custom one.
I hope this information helps. If you need any further assistance please don't hesitate to contact me.
Regards,
Dess
Progress Telerik
Hi Dess,
I've applied the code you provided to my project and it worked fine for me. I had to make it more generic in order to add the custom behavior to multiple columns and now it's an easy to add formatting for any column from the GUI.
Thanks a lot!
Luca
I am glad that the provided sample project was useful for your scenario. Indeed, it doesn't provide a general solution but it demonstrates a sample approach how you can handle the adding of a ConditionalFormattingObject and replace it with a custom one. Feel free to customize and extend it in a way to achieve the exact goal that you have.
If you need any further assistance please don't hesitate to contact me.
Regards,
Dess
Progress Telerik
