kuujinbo_dot_info

Posted 2006-08.

While describing one way to use the DataGrid.OnItemDataBound() method, I needed a small data source to fill the datagrid. Seen and used examples of using an XML file to bind to the DataGrid, but never tried using a string.

Since XML is so central to .NET and the DataSet is central to the DataGrid, looked up the DataSet members in the SDK. Found the DataSet.ReadXml(TextReader) method, which loads a XML string into the DataSet. TextReader is the abstract base class for StringReader, so we can pass a StringReader object to ReadXml:

// create dataset from xml string
private static DataSet get_xml_ds() {
  DataSet d = new DataSet();
  d.ReadXml(new StringReader(xml_task_data));
  // string in helper class  ^^^^^^^^^^^^^
  return d;
}

Then bind the DataSet to the DataGrid:

public static void bind(DataGrid dg) {
  dg.DataSource = get_xml_ds();
  dg.DataBind();
}

The XML string:

<tasks>
  <task>
    <date_assigned>2006-01-07</date_assigned>
    <date_deadline>2006-01-08</date_deadline>
    <date_completed>2006-01-09</date_completed>
    <description>Install M$ January Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-02-07</date_assigned>
    <date_deadline>2006-02-08</date_deadline>
    <date_completed>2006-02-08</date_completed>
    <description>Install M$ February Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-03-07</date_assigned>
    <date_deadline>2006-03-08</date_deadline>
    <date_completed>2006-03-07</date_completed>
    <description>Install M$ March Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-04-07</date_assigned>
    <date_deadline>2006-04-08</date_deadline>
    <date_completed>2006-04-12</date_completed>
    <description>Install M$ April Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-05-07</date_assigned>
    <date_deadline>2006-05-08</date_deadline>
    <date_completed>2006-05-10</date_completed>
    <description>Install M$ May Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-06-07</date_assigned>
    <date_deadline>2006-06-08</date_deadline>
    <date_completed>2006-06-07</date_completed>
    <description>Install M$ June Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-07-07</date_assigned>
    <date_deadline>2006-07-08</date_deadline>
    <date_completed>2006-07-18</date_completed>
    <description>Install M$ July Hotfixes</description>
  </task>
  <task>
    <date_assigned>2006-08-07</date_assigned>
    <date_deadline>2006-08-08</date_deadline>
    <date_completed>2006-08-14</date_completed>
    <description>Install M$ August Hotfixes</description>
  </task>
</tasks>

Note that the two methods and string above are static members defined in a helper class.

The results:

AssignedDeadlineCompletedTask
2006-01-07 2006-01-08 2006-01-09 Install M$ January Hotfixes
2006-02-07 2006-02-08 2006-02-08 Install M$ February Hotfixes
2006-03-07 2006-03-08 2006-03-07 Install M$ March Hotfixes
2006-04-07 2006-04-08 2006-04-12 Install M$ April Hotfixes
2006-05-07 2006-05-08 2006-05-10 Install M$ May Hotfixes
2006-06-07 2006-06-08 2006-06-07 Install M$ June Hotfixes
2006-07-07 2006-07-08 2006-07-18 Install M$ July Hotfixes
2006-08-07 2006-08-08 2006-08-14 Install M$ August Hotfixes