Winforms c# app on datagridview binding


I have a WinForms application and would like to bind an XML file to a DataGridView. On the first 2 rows of the DataGridView I need to append a timespan (HH:MM) time format. How do I achieve this, so that in addition all the rows are editable? What would be the best way to display the data in the DataGridView.

DataColumn col1 = new DataColumn("value", typeof(string));
DataColumn col2 = new DataColumn("comment", typeof(string));
this.table.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "CFResource.xml");
this.dataGridView1.DataSource = this.table;

My XML file looks as follows:

  <data name="BhrFromTime" xml:space="preserve">
    <comment>Bhr start time</comment>
  <data name="BhrToTime" xml:space="preserve">
    <comment>bhr from time</comment>
  <data name="U0001" xml:space="preserve">
    <comment>UserWarning 1</comment>
  <data name="U0003" xml:space="preserve">
    <comment>UserWarning 3</comment>
  <data name="U0002" xml:space="preserve">
    <comment>UserWarning 2</comment>
  <data name="U0004" xml:space="preserve">
    <comment>UserWarning 4</comment>
  <data name="U0007" xml:space="preserve">
    <comment>user warning 5</comment>

Show source
| c#   | xml   | winforms   | datagridview   2016-12-25 23:12 1 Answers

Answers ( 1 )

  1. 2016-12-26 01:12

    Perhaps the easiest way is to load the xml file into a DataSet and in your case bind table data to the DataGridView:

    DataSet ds = new DataSet();
    dataGridView1.DataSource = ds.Tables["data"];

    Which produces this:

    enter image description here

◀ Go back