Export RadGridView to XML

1 Answer 223 Views
GridView
Pin
Top achievements
Rank 1
Iron
Pin asked on 29 May 2022, 09:04 AM

Hi,

Is it possible to export radgridview (with grouped columns) to .XML format?

 

Thanks

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 31 May 2022, 08:34 AM

Hello, Pin,

RadGridView from the Telerik UI for WinForms suite offers different export options listed here: 

https://docs.telerik.com/devtools/winforms/controls/gridview/exporting-data/overview 

However, export to XML is not available out of the box. The possible approach that I can suggest is to build programmatically a DataTable populated with the data from RadGridView. Then, use the DataTable.WriteXml method: https://docs.microsoft.com/en-us/dotnet/api/system.data.datatable.writexml?view=net-6.0 

I have researched in general programming forums and found the following threads which seem to be quite useful on this topic:

https://stackoverflow.com/questions/3136303/convert-datatable-to-xml-file-and-viceversa 

https://www.c-sharpcorner.com/UploadFile/deepak.sharma00/how-to-write-data-of-an-sql-server-table-to-an-xml-file-usin/ 

I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Principal
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Pin
Top achievements
Rank 1
Iron
commented on 31 May 2022, 11:34 AM

Hi Dess,

Thanks for the suggestions and helpful links, But, is this method (Radgridview -> Datatable -> XML) possible for a Radgridview with grouped column(s) ?

Dess | Tech Support Engineer, Principal
Telerik team
commented on 31 May 2022, 12:12 PM

Hi, Pin,

When the grid is grouped, note that you can iterate the RadGridView.ChildRows collection and this will traverse the first level of groups. Thus, you can traverse the ChildRows collection recursively and thus make the flat structure of the DataTable. I have prepared a sample code snippet for your reference which result is illustrated in the gif file:

        private void RadForm1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'nwindDataSet.Customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.nwindDataSet.Customers);
            this.radGridView1.BestFitColumns();
        }
     
        DataTable dt = new DataTable();

        private void radButton1_Click(object sender, EventArgs e)
        {
            dt = new DataTable();
            foreach (GridViewColumn col in this.radGridView1.Columns)
            {
                dt.Columns.Add(col.Name);
            }
            dt.Columns.Add("Group");

            TraverseRows(this.radGridView1.ChildRows);
            this.radGridView2.DataSource = dt;
            this.radGridView2.BestFitColumns();
        }

        private void TraverseRows(IEnumerable<GridViewRowInfo> rows)
        {
            foreach (GridViewRowInfo row in rows)
            {
                GridViewGroupRowInfo groupRow = row as GridViewGroupRowInfo;
                if (groupRow != null)
                {
                    DataRow addedRow = dt.Rows.Add();
                    addedRow["Group"] = groupRow.HeaderText;
                    TraverseRows(groupRow.ChildRows);
                }
                else
                {
                    DataRow addedRow = dt.Rows.Add();
                    foreach (GridViewCellInfo cell in row.Cells)
                    {
                        addedRow[cell.ColumnInfo.Name] = cell.Value;
                    }
                }

            }
        }

Note that this is just a sample approach and it may not cover all possible cases. Feel free to modify and extend it in a way which suits your requirements best.

For RadGridView grouped by some of its columns, it is also possible to consider changing the exported file type, e.g. export it to Excel file. It is appropriate to use GridViewSpreadExport and thus the groups will be exported automatically: https://docs.telerik.com/devtools/winforms/controls/gridview/exporting-data/spread-export 

Feel free to use this approach which suits your requirements best.

Pin
Top achievements
Rank 1
Iron
commented on 01 Jun 2022, 05:41 AM

MANY THANKS...
Tags
GridView
Asked by
Pin
Top achievements
Rank 1
Iron
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or