【问题标题】:Winforms c# app on datagridview binding关于datagridview绑定的Winforms c# app
【发布时间】:2016-12-25 22:43:00
【问题描述】:

我有一个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>

【问题讨论】:

    标签: c# xml winforms datagridview


    【解决方案1】:

    也许最简单的方法是将 xml 文件加载到 DataSet 中,并在您的情况下将表 data 绑定到 DataGridView:

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

    产生这个:

    【讨论】:

      猜你喜欢
      • 2015-01-23
      • 2012-11-02
      • 1970-01-01
      • 2019-07-21
      • 2011-09-15
      • 2010-09-12
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      相关资源
      最近更新 更多