How to generate DataSet object from SfDataGrid

hello,

Is there any way (any method to be specific) to store a table inside sfDataGrid with proper header and all the rows into a DataSet object.

currently, my approach is to first generate the headers and loop through each row and storing the values in a DataRow then in a DataTable and finally adding it into a DataSet. Below is the code I wrote till now..


       private string[] HeaderList =
        {
            "Cable Type",
            "Insulation Type",
            "VOP",
            "Status"
        };

DataSet DSET = new DataSet();
DTAB = new DataTable();
foreach (string HLIST_ITEM in HeaderList)
    {
          DTAB.Columns.Add(new DataColumn(HLIST_ITEM.Replace(" " ,"")));
    }

    foreach (var record in DG_CblLib.View.Records)
    {
          DataRowView DRV = (DataRowView) record.Data;
          DataRow DROW;
          DTAB.NewRow();
                
          for (int i = 0; i < HeaderList.Length; i++)
          {
          //Looping through each cell value of current row under each column but don't know how to           do that
          }
    }



3 Replies 1 reply marked as answer

DM Dhanasekar Mohanraj Syncfusion Team December 29, 2020 02:17 PM UTC

Hi Shankul Gupta, 

Thank you for using Syncfusion controls 

We are little unclear with your requirement. We suspect you need to create a DataTable with the SfDataGrid records. If not, can you please explain your scenario. 

It will be helpful for us to investigate further and provide you with a solution at the earliest. 

Regards,
Dhanasekar Mohanraj.  



SG shankul gupta December 30, 2020 05:50 AM UTC

Yes, Exactly that is what I want. I want to convert the table with the schema into a DataTable.


DM Dhanasekar Mohanraj Syncfusion Team December 31, 2020 10:04 AM UTC

Hi Shankul Gupta, 

Thank you for your response.

 
You can create DataTable with SfDataGrid records like below, 
private void Convert() 
    try 
   
        var dataSet = new DataSet(); 
        var dataTable = new DataTable(); 
        dataSet.Tables.Add(dataTable); 
        dataTable.Columns.Add("OrderID"); 
        dataTable.Columns.Add("CustomerID"); 
        dataTable.Columns.Add("CustomerName"); 
        dataTable.Columns.Add("Country"); 
        dataTable.Columns.Add("ShipCity"); 
        foreach (var record in sfDataGrid1.View.Records) 
       
            var data = (record.Data as OrderInfo); 
            var newRow = dataTable.NewRow(); 
            newRow["OrderID"] = data.OrderID; 
            newRow["CustomerID"] = data.CustomerID; 
            newRow["CustomerName"] = data.CustomerName; 
            newRow["Country"] = data.Country; 
            newRow["ShipCity"] = data.ShipCity; 
            dataTable.Rows.Add(newRow); 
       
   
    catch (Exception e) 
   
        MessageBox.Show("error"); 
   

We have prepared the sample for the same, 

 
We hope it helps please let us know if you need further assistance.

 
Regards, 
Dhanasekar Mohanraj. 


Marked as answer
Loader.
Up arrow icon