Winforms c# app on datagridview binding

Question

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.Columns.Add(col1);
this.table.Columns.Add(col2);
this.table.ReadXml(AppDomain.CurrentDomain.BaseDirectory + "CFResource.xml");
table.Rows[0].Field<TimeSpan>("BhrFromTime").ToString(@"hh\:mm");
this.dataGridView1.DataSource = this.table;

My XML file looks as follows:

<root>
  <Time>
  <data name="BhrFromTime" xml:space="preserve">
    <value>08:00</value>
    <comment>Bhr start time</comment>
  </data>
  <data name="BhrToTime" xml:space="preserve">
    <value>19:00</value>
    <comment>bhr from time</comment>
  </data>
  </Time>
  <UserWarning>
  <data name="U0001" xml:space="preserve">
    <value>UW1</value>
    <comment>UserWarning 1</comment>
  </data>
  <data name="U0003" xml:space="preserve">
    <value>UW2</value>
    <comment>UserWarning 3</comment>
  </data>
  <data name="U0002" xml:space="preserve">
    <value>UW3</value>
    <comment>UserWarning 2</comment>
  </data>
  <data name="U0004" xml:space="preserve">
    <value>UW4</value>
    <comment>UserWarning 4</comment>
  </data>
  <data name="U0007" xml:space="preserve">
    <value>UW5</value>
    <comment>user warning 5</comment>
  </data>
  </UserWarning>
</root>

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

Answers to Winforms c# app on datagridview binding ( 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();
    ds.ReadXml("xmlfile1.xml");
    dataGridView1.DataSource = ds.Tables["data"];
    

    Which produces this:

    enter image description here

Leave a reply to - Winforms c# app on datagridview binding

◀ Go back