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
close icon

XML of SfDataGrid serialization to ASP.NET MVC grid

Hi,


So in my project, i have WPF grid in 1 project and MVC grid in another project.
In WPF, i can save the order of columns, width, and its visibility by using Serialization (https://help.syncfusion.com/wpf/grid/serialization-support)

I`m wondering if i can get the setting (column order, width, and visibility) from the xml generated by Serialization and use it to match with MVC grid.


Is there any solution for this ?

5 Replies

VN Vignesh Natarajan Syncfusion Team December 11, 2018 04:43 PM UTC

Hi Trinquier, 
 
Thanks for contacting Syncfusion Support.  
 
Query: “I`m wondering if i can get the setting (column order, width, and visibility) from the xml generated by Serialization and use it to match with MVC grid.” 
 
From your query, we understand that you need to render the Grid column based on XML document. We have achieved your requirement using CustomAdaptor support of ejGrid and updated the column details in the DataBound event of ejGrid using Columns() method. 
 
Refer the below code example 
 
[cshtml] 
 
@(Html.EJ().Grid<object>("Grid") 
    .AllowPaging() 
    .Datasource(ds => 
    { 
        ds.URL("/Grid/UrlDataSource"); 
        ds.DataType("xml"); 
        ds.Offline(true); 
        ds.Adaptor(AdaptorType.UrlAdaptor); 
    }) 
    .AllowSorting() 
    .Columns(col => 
    { 
        col.Field("OrderID").Add(); 
        col.Field("CustomerID").Add(); 
        col.Field("url").Add(); 
    }) 
        .ClientSideEvents(events => events.Load("gridLoad").DataBound("bound")) 
) 
<script> 
    var json 
    function bound(args) {        
        defineColumns(json.Order.ColumnDetails); // call a funciton to define the column details 
    } 
    function gridLoad(args) { 
        this.model.dataSource.adaptor = new xmlAdaptor(); 
    } 
     //for webforms use ej.WebMethodAdaptor 
    var xmlAdaptor = new ej.UrlAdaptor().extend({ 
        //Overriding default processResponse function 
        processResponse: function (data, ds, query, xhr, request, changes) { 
            return processXML(data); 
        } 
    }); 
   
    function processXML(ele) { 
        json = ConvertToJSON(ele);         
        return json.Order.OrderDetails; 
    } 
    function defineColumns(column) { 
        var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID 
        Obj.model.columns = []; // empty the existing column details 
        for (var i = 0; i < column.length; i++) { 
            Obj.model.columns.push({ field: column[i].position, width: parseInt(column[i].width), headerText: column[i].position, visible: column[i].visibility == "true" }) 
        } 
        Obj.columns(Obj.model.columns) // update the column with new value 
    } 
    //Function to convert XML document to JSON object 
    function ConvertToJSON(ele) { 
       .                 .                      .                 .          .  
 
 return json; 
    } 
 
</script> 
 
  
[c#] 
 
 
        [HttpPost] 
        public XDocument UrlDataSource(DataManager dm) 
        { 
            string json = @"<?xml version='1.0' standalone='no'?> 
<Order> 
  <OrderDetails id='1'> 
<OrderID>10248</OrderID> 
<EmpoyeeID>1</EmpoyeeID> 
    <CustomerID>Alan</CustomerID> 
    <url>http://www.google.com</url> 
  </OrderDetails> 
.                             .                     .                         .                       .                       .                   .  
 <ColumnDetails> 
   <position>url</position>    
   <width>20</width>     
   <visibility>true</visibility>      
  </ColumnDetails> 
</Order>"; 
            Response.ContentType = "text/xml"; 
            XmlDocument doc1 = new XmlDocument(); 
            doc1.LoadXml(json); 
            var doc = DocumentToXDocument(doc1);           
            return doc; 
        } 
        private static XDocument DocumentToXDocument(XmlDocument doc) 
        { 
            return XDocument.Parse(doc.OuterXml); 
        } 
 
 
Note: we have hided the EmployeeID column using visible property. 
 
For your convenience we have prepared a sample which can be downloaded from below link 
 
 
Refer our help documentation and Knowledge base document for your reference 
 
 
UG:  
 
 
 
 
Regards, 
Vignesh Natarajan  
 



TV Trinquier Vannick December 12, 2018 03:09 AM UTC

Hi,

Thanks for the response Vignesh.

Apparently solution that you gave is retrieving data from XML document.
my problem here is that, the xml is coming from WPF grid (using Serialize of SfDataGrid).
The XML start like this : 
<?xml version="1.0" encoding="UTF-8"?> <SfDataGrid xmlns="http://schemas.datacontract.org/2004/07/Syncfusion.UI.Xaml.Grid" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
...


So i want to get the columns information of that XML for me to use in MVC (get the columns width percentage, visible columns, order of the columns).

Is there any connection that maybe i can retrieve from there ? and how ?



MP Manivannan Padmanaban Syncfusion Team December 12, 2018 11:14 AM UTC

Hi Trinquier , 

Query: So i want to get the columns information of that XML for me to use in MVC (get the columns width percentage, visible columns, order of the columns). 

From your query we could understand that, you want to get the column details from the XML data. In the previous update, we provided the solution to update the columns details from XML data to MVC grid column by using the columns method in the dataBound event of Grid. Refer the below code example, 

@(Html.EJ().Grid<object>("Grid" 
    … 
    .AllowSorting()  
    .Columns(col =>  
    {  
.. 
    })  
        .ClientSideEvents(events => events.Load("gridLoad").DataBound("bound"))  
)  
<script>  
    var json  
    function bound(args) {         
        defineColumns(json.Order.ColumnDetails); // call a funciton to define the column details  
    }  
    function defineColumns(column) {  
        var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID  
        Obj.model.columns = []; // empty the existing column details  
        for (var i = 0; i < column.length; i++) {  
            Obj.model.columns.push({ field: column[i].position, width: parseInt(column[i].width), headerText: column[i].position, visible: column[i].visibility =="true" })  
        }  
        Obj.columns(Obj.model.columns) // update the column with new value  
    }  
     
    }  
  
</script>  


So, to achieve your requirement we suggest you to map the equivalent column details in the data bound event of grid from the XML data which is received from the WPF grid as like in the above code example. If your facing any issues then, please share the following details. 

  1. Share the complete XML data are you receiving from the WPF grid.
  2. Share the Complete grid code example.

Regards, 
Manivannan Padmanaban. 



TV Trinquier Vannick December 14, 2018 04:54 AM UTC

Hi Manivannan,

I have difficulty to link the structure of WPF SfDataGrid with MVC grid.
I see that the structure of XML that the column visibility is clear (its hard to read the structure of WPF grid). 


I attached the xml and the MVC grid that i wish to implement the column visibility based on that.

Attachment: WPFtoMVCGrid_563f22b2.zip


VN Vignesh Natarajan Syncfusion Team December 15, 2018 01:08 AM UTC

Hi Trinquier, 
 
Query: “I attached the xml and the MVC grid that i wish to implement the column visibility based on that. 
 
We have analyzed the MVC Grid code and XML data. We have modified the previously provided sample to change with based on your XML data. We have achieved your requirement using DataBound event and sent the AJAX post to server to retieve the data from XML file. and in AJAX success we have modified the column visibility.  
 
Refer the below code example 
 
@(Html.EJ().Grid<object>("Grid") 
    .AllowPaging() 
     .Datasource(ds => ds.Json((IEnumerable<object>)ViewBag.dataSource).Adaptor(AdaptorType.RemoteSaveAdaptor)) 
    .AllowSorting() 
    .Columns(col => 
    { 
        col.Field("OrderID").Add(); 
        col.Field("CustomerID").Add(); 
        col.Field("Freight").Add(); 
        col.Field("EmployeeID").Add(); 
    }) 
        .ClientSideEvents(events => events.Load("gridLoad").DataBound("bound")) 
) 
<script> 
    var json 
    function bound(args) { 
        $.ajax({ // ajax post to retireve the xml serialized data in string format   
            url: "/Grid/UrlDataSource", 
            type: "POST", 
            success: function (data) { 
                processXML(data); // to deserialize the string 
                defineColumns(json.SfDataGrid.Columns.GridColumn); // update the column.  
            } 
        })       
    }     
 
    function processXML(ele) { 
        json = ConvertToJSON(ele);         
    } 
    function defineColumns(column) { 
        var Obj = $("#Grid").ejGrid("instance"); // take the Grid instance using Grid ID 
        for(var j=0;j<Obj.model.columns.length;j++){ 
            for (var i = 0; i < column.length; i++) { 
                if (Obj.model.columns[j].field == column[i].HeaderText) 
                    Obj.model.columns[j].visible = column[i].IsHidden != "true"; 
            } 
        } 
        Obj.columns(Obj.model.columns) // update the column with new value 
    } 
    //Function to convert XML document to JSON object 
    function ConvertToJSON(ele) { 
        var json = {}, e, ch; 
        var addItem = function (parent, name, value) { 
            if (!parent[name]) 
                parent[name] = value; 
            else { 
                if (parent[name].constructor != Array) 
                    parent[name] = [parent[name]]; 
                parent[name][parent[name].length] = value; 
            } 
        } 
 
        for (e = 0, ch = ele.childNodes; e < ch.length; e++) { 
            if (ch[e].nodeType == 1) { 
                if (ch[e].childNodes.length == 1 && ch[e].firstChild.nodeType == 3) 
                    addItem(json, ch[e].nodeName, ch[e].firstChild.nodeValue); 
                else if (ch[e].childNodes.length != 0) 
                    addItem(json, ch[e].nodeName, ConvertToJSON(ch[e])); 
            } 
        } 
        return json; 
    } 
     
                                                                      
 
</script> 
 
  
[HttpPost] 
        public XDocument UrlDataSource(DataManager dm) 
        { 
            string json = @"<?xml version='1.0' encoding='UTF-8'?> 
<SfDataGrid xmlns='http://schemas.datacontract.org/2004/07/Syncfusion.UI.Xaml.Grid' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'> 
   <AllowDraggingColumns>true</AllowDraggingColumns> 
   <AllowGrouping>true</AllowGrouping> 
   <AllowResizingColumns>true</AllowResizingColumns> 
   <ColumnSizer>Star</ColumnSizer> 
   <Columns> 
      <GridColumn i:type='GridTextColumn'> 
         <AllowBlankFilters>true</AllowBlankFilters> 
         <AllowFocus>true</AllowFocus> 
         <AllowResizing>false</AllowResizing> 
         <ColumnSizer>Auto</ColumnSizer> 
         <FilterRowCondition>BeginsWith</FilterRowCondition> 
         <FilterRowEditorType>TextBox</FilterRowEditorType> 
         <FilteredFrom>None</FilteredFrom> 
         <GridValidationMode>None</GridValidationMode> 
<IsHidden>false</IsHidden> 
         <HeaderText>OrderID</HeaderText> 
         <HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment> 
         <MappingName>WBS</MappingName> 
         <MaximumWidth>NaN</MaximumWidth> 
         <MinimumWidth>NaN</MinimumWidth> 
         <Width>20.78</Width> 
         <TextWrapping>NoWrap</TextWrapping> 
      </GridColumn> 
      <GridColumn i:type='GridTextColumn'> 
         <AllowBlankFilters>true</AllowBlankFilters> 
         <AllowFocus>true</AllowFocus> 
         <FilterRowCondition>BeginsWith</FilterRowCondition> 
         <FilterRowEditorType>TextBox</FilterRowEditorType> 
         <FilteredFrom>None</FilteredFrom> 
         <GridValidationMode>None</GridValidationMode> 
         <HeaderText>CustomerID</HeaderText> 
         <HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment> 
         <MappingName>Label</MappingName> 
         <MaximumWidth>NaN</MaximumWidth> 
         <MinimumWidth>NaN</MinimumWidth> 
         <Width>161</Width> 
         <TextWrapping>Wrap</TextWrapping> 
      </GridColumn> 
      <GridColumn i:type='GridNumericColumn'> 
         <AllowBlankFilters>true</AllowBlankFilters> 
         <AllowFocus>true</AllowFocus> 
         <FilterRowCondition>Equals</FilterRowCondition> 
         <FilterRowEditorType>Numeric</FilterRowEditorType> 
         <FilteredFrom>None</FilteredFrom> 
         <GridValidationMode>None</GridValidationMode> 
         <HeaderText>Freight</HeaderText> 
         <HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment> 
         <IsHidden>true</IsHidden> 
         <MappingName>Start</MappingName> 
         <MaximumWidth>NaN</MaximumWidth> 
         <MinimumWidth>NaN</MinimumWidth> 
         <TextAlignment>Right</TextAlignment> 
         <Width>NaN</Width> 
         <MaxValue>79228162514264337593543950335</MaxValue> 
         <MinValue>-79228162514264337593543950335</MinValue> 
         <NullText /> 
         <NumberDecimalDigits>2</NumberDecimalDigits> 
         <NumberDecimalSeparator>,</NumberDecimalSeparator> 
         <NumberGroupSeparator>.</NumberGroupSeparator> 
         <NumberGroupSizes xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'> 
            <a:int>0</a:int> 
         </NumberGroupSizes> 
         <NumberNegativePattern>1</NumberNegativePattern> 
      </GridColumn> 
      <GridColumn i:type='GridNumericColumn'> 
         <AllowBlankFilters>true</AllowBlankFilters> 
         <AllowFocus>true</AllowFocus> 
         <FilterRowCondition>Equals</FilterRowCondition> 
         <FilterRowEditorType>Numeric</FilterRowEditorType> 
         <FilteredFrom>None</FilteredFrom> 
         <GridValidationMode>None</GridValidationMode> 
         <HeaderText>EmployeeID</HeaderText> 
         <HorizontalHeaderContentAlignment>Center</HorizontalHeaderContentAlignment> 
         <IsHidden>true</IsHidden> 
         <MappingName>Finish</MappingName> 
         <MaximumWidth>NaN</MaximumWidth> 
         <MinimumWidth>NaN</MinimumWidth> 
         <TextAlignment>Right</TextAlignment> 
         <Width>NaN</Width> 
         <MaxValue>79228162514264337593543950335</MaxValue> 
         <MinValue>-79228162514264337593543950335</MinValue> 
         <NullText /> 
         <NumberDecimalDigits>2</NumberDecimalDigits> 
         <NumberDecimalSeparator>,</NumberDecimalSeparator> 
         <NumberGroupSeparator>.</NumberGroupSeparator> 
         <NumberGroupSizes xmlns:a='http://schemas.microsoft.com/2003/10/Serialization/Arrays'> 
            <a:int>0</a:int> 
         </NumberGroupSizes> 
         <NumberNegativePattern>1</NumberNegativePattern> 
      </GridColumn> 
     .                         .                         .                       .                       .                 .  
            </Columns> 
           </SfDataGrid>"; 
            Response.ContentType = "text/xml"; 
            XmlDocument doc1 = new XmlDocument(); 
            doc1.LoadXml(json); 
            var doc = DocumentToXDocument(doc1);           
            return doc; 
        } 
        private static XDocument DocumentToXDocument(XmlDocument doc) 
        { 
            return XDocument.Parse(doc.OuterXml); 
        } 
 
For your convenience we have attached the modified sample which can be downloaded from below link 
 
 
Note: as an example we have serialized wpf Grid xml data. similarly you can modify for other columns. 
 
Regards, 
Vignesh Natarajan 


Loader.
Live Chat Icon For mobile
Up arrow icon