簡體   English   中英

DataGridView綁定上的Winforms C#應用

[英]Winforms c# app on datagridview binding

我有一個WinForms應用程序,想將XML文件綁定到DataGridView DataGridView的前兩行中,我需要附加一個時間跨度(HH:MM)時間格式。 我如何實現這一點,從而使所有行都可編輯? 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;

我的XML文件如下所示:

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

也許最簡單的方法是將xml文件加載到DataSet中,然后將表data綁定到DataGridView:

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

產生此:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM