We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

MVC 3 : Conditional Formating not working

Hello Experts,

When we have added ConditionFormat on view side (not from controller). In this, when page loaded first time at that time grid is display based on the condition but when we are doing paging at that time grid is not displaying based on the condition.

Code :
var dictionary = new Dictionary();
dictionary.Add("style", "background-color:#E29100");


.ConditionalFormats(condition =>
{
condition.Add(new GridConditionalFormatDescriptor
{
Name = "TfsTransfer",
Conditions = new System.Collections.ObjectModel.Collection>
{
new GridDataCondition(c => c.TransferredToTfs) { ConditionType = GridDataConditionType.Equals, Value = "Yes" }
},
Cell = new GridCellFormatter { HtmlAttributes = dictionary }
});
})



Thanks
Alok Shah



3 Replies

ES Eswari S Syncfusion Team December 1, 2011 12:13 PM UTC

Hi Alok,

Thank you for using Syncfusion products.

Query #1: when we are doing paging at that time grid is not displaying based on the condition.

If you are using a paging/sorting action, rebind the ConditionalFormats property again to Grid in the post action

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Index(PagingParams args)
{
IEnumerable data = new NorthwindDataContext().Orders.ToList();
ActionResult result = data.GridActions();

// rebind the action again

var engine = result as GridHtmlActionResult;
engine.GridModel.ConditionalFormats = this.ConditionFormats;
return engine;
}

// conditions created using GridDataCondition and add the condition to GridConditionalFormatDescriptor.

private Collection> ConditionFormats
{
get
{
GridDataCondition condition3 = new GridDataCondition(c => c.OrderID) { ConditionType = GridDataConditionType.Equals, Value = 10250 };
GridConditionalFormatDescriptor rowFormat = new GridConditionalFormatDescriptor();
rowFormat.Name = "condition1";
rowFormat.Conditions.Add(condition3);
rowFormat.Cell.HtmlAttributes["style"] = "background-color:#E29100";

Collection> cf = new Collection>();
cf.Add(rowFormat);
return cf;
}
}

Please find the sample from the following link :

http://www.syncfusion.com/downloads/Support/DirectTrac/86477/Sample-900465145.zip

Please let us know if you need any further assistance.

Regards,
Eswari.S







AD Administrator Syncfusion Team December 1, 2011 01:17 PM UTC

Hi Eswari,

Thanks for your help.

I know that on paging, POST method will call but I already mentioned conditional format in the view than why it's not reflecting.

Thanks
Alok Shah



ES Eswari S Syncfusion Team December 2, 2011 06:35 AM UTC

Hi Alok,

Thanks for your update.

While adding the conditional formatting in view , it will not reflect in post actions. In this scenario even if we have added the conditions in view , again we need to add the conditional formatting’s in controller after post actions which is used to rebind the formatting’s. Since we suggest you to use properties model formatting(controller) to avoid this.

We have prepared the sample in both Builder(view) and properties model(controller) conditional formatting. Please find the sample from the following link:

Builder Sample :

http://www.syncfusion.com/downloads/Support/DirectTrac/86477/Sample-900465145.zip

Steps :

#1: Call the Conditional formatting in view side :

<%=Html.Syncfusion().Grid("OrdersGrid")
. . . .
. . . . .
.ConditionalFormats(condition =>
{
var dictionary = new Dictionary();
dictionary.Add("style", "background-color:#E29100");
condition.Add(new GridConditionalFormatDescriptor
{
Name = "TfsTransfer",

Conditions = new System.Collections.ObjectModel.Collection>
{
new GridDataCondition(c => c.OrderID) { ConditionType = GridDataConditionType.Equals, Value = 10249 }
},

Cell = new GridCellFormatter { HtmlAttributes = dictionary }
%>

[AcceptVerbs(HttpVerbs.Post)]

public ActionResult Index(PagingParams args)
{
IEnumerable data = new NorthwindDataContext().Orders.ToList();
ActionResult result = data.GridActions();

// rebind the action again

var engine = result as GridHtmlActionResult;
engine.GridModel.ConditionalFormats = this.ConditionFormats;
return engine;
}

// conditions created using GridDataCondition and add the condition to GridConditionalFormatDescriptor.

private Collection ConditionFormats
{
get
{
GridDataCondition condition3 = new GridDataCondition(c => c.OrderID) { ConditionType = GridDataConditionType.Equals, Value = 10250 };
GridConditionalFormatDescriptor rowFormat = new GridConditionalFormatDescriptor();
rowFormat.Name = "condition1";
rowFormat.Conditions.Add(condition3);
rowFormat.Cell.HtmlAttributes["style"] = "background-color:#E29100";

Collection cf = new Collection();
cf.Add(rowFormat);
return cf;
}
}

Conditional formatting using Properties model(controller ):

http://www.syncfusion.com/downloads/Support/DirectTrac/88155/Sample775746893.zip

Steps :

1. Create conditions using GridDataCondition and add the condition to GridConditionalFormatDescriptor.

2. Add GridConditionalFormatDescriptor to GridPropertiesModel using ConditionalFormats property

public ActionResult Index()
{
GridPropertiesModel model =new GridPropertiesModel();
model.ConditionalFormats = this.ConditionFormats;
ViewData["OrdersGrid"] = model; // OrdersGrid is the Grid ID
var data = new NorthwindDataContext().Orders.ToList();
return View(data);
}

3. If you are using a paging/sorting action, rebind the ConditionalFormats property again to Grid in the post action


[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(PagingParams args)
{
IEnumerable data = new NorthwindDataContext().Orders.ToList();
ActionResult result = data.GridActions< Order>();
var engineSource = result as GridHtmlActionResult;
engineSource.GridModel.ConditionalFormats = this.ConditionFormats;
return engineSource;
}

Please try this and let us know if you have any other queries.

Regards,
Eswari.S





Loader.
Live Chat Icon For mobile
Up arrow icon