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

Load Column Based On Condition

Hi,

Is it possible to load a column in the grid based on a form control.

i.e. If the checkbox is checked then the grid loads the extra column?

5 Replies

SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 3, 2017 08:56 AM UTC

Hi Customer,  
 
We can add or remove any number of columns in the Grid using the columns method of the Grid. Refer to the following code example and API Reference. 
 
@using (Html.BeginForm("FormTwo", "Form", FormMethod.Post)) 
{ 
    
    @Html.EJ().CheckBox("chk").ClientSideEvents(events => events.Change("change")); 
} 
 
 
 
@(Html.EJ().Grid<object>("Grid") 
        .Columns(col => 
        { 
            col.Field("OrderID").Add(); 
               . ..  
        }) 
) 
 
 
<script> 
    function change(args) { 
        if (args.checkState == "check") 
            $("#Grid").ejGrid("columns", [{ field: "OrderDate" }], "add"); 
        else 
            $("#Grid").ejGrid("columns", [{ field: "OrderDate" }], "remove"); 
    } 
</script> 
 
 

 
 
In the above code example, based on the checkbox state, columns in the Grid will be added/removed. 
 
Regards,  
Seeni Sakthi Kumar S. 



AS Avi Segal April 3, 2017 03:58 PM UTC

Thanks for that.

How can I add the other row parameters? See example below.

col.Field("SKU").HeaderText("SKU").Width(100).DataSource(ViewData("StockItems")).EditType(EditingType.Dropdown).AllowEditing(True).TextAlign(TextAlign.Center).Add()


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 4, 2017 10:06 AM UTC

Hi Customer,  
 
We can include all the parameters of the columns such field, headerText, editType in the columns method as an array of object. Refer to the following code example.  
 
@using (Html.BeginForm("FormTwo", "Form", FormMethod.Post)) 
{ 
 
    @Html.EJ().CheckBox("chk").ClientSideEvents(events => events.Change("change")); 
} 
 
 
 
@(Html.EJ().Grid<object>("Grid") 
        .Columns(col => 
        { 
            col.Field("OrderID").Add(); 
               . . . 
                     . . . 
        }) 
) 
 
 
<script> 
    var dropData = @Html.Raw(Json.Encode(ViewData["StockItems"])); 
    function change(args) { 
        if (args.checkState == "check") 
            //include columns as array of Object 
            $("#Grid").ejGrid("columns", [ 
                { 
                    field: "SKU", headerText: "SKU", width: 100, 
                    dataSource: dropData, editType: ej.Grid.EditingType.Dropdown, 
                    allowEditing: true, textAlign: ej.TextAlign.Right 
                } 
            ], "add"); 
        else 
            $("#Grid").ejGrid("columns", [{ field: "SKU" }], "remove"); 
    } 
 
</script> 
 
 
 
 
Regards,  
Seeni Sakthi Kumar S. 



AS Avi Segal April 4, 2017 10:30 AM UTC

Perfect! 

Thanks,

Avi


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team April 5, 2017 05:25 AM UTC

Hi Customer,  
 
Thanks for the update. 
 
We are happy to hear that your requirement has been resolved. Please get back to us, if you require further assistance on this. 
 
Regards,  
Seeni Sakthi Kumar S. 


Loader.
Live Chat Icon For mobile
Up arrow icon