Questions on DataGridView and XML File as Data Source

S

Stewart Berman

I have an application that populates a DataGridView control with an XML file:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.DataSource = gridDataSet;
gridDataSet.ReadXml("E:\\ImageControl.xml");
dataGridView1.DataMember = "Images";
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.ColumnHeadersVisible = true;
dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Visible = false;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
}

The interface has an Add and a Delete button for adding and deleting entries.

How do I setup the XML file so the grid only shows the headers?
The schema is:
<?xml version="1.0" standalone="yes" ?>
<xs:schema id="DataSet1" targetNamespace="http://www.tempuri.org/DataSet1.xsd"
xmlns:mstns="http://www.tempuri.org/DataSet1.xsd"
xmlns="http://www.tempuri.org/DataSet1.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified"
elementFormDefault="qualified">
<xs:element name="DataSet1" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Images" minOccurs="0" >
<xs:complexType>
<xs:sequence>
<xs:element name="Sequence"
msdata:AutoIncrement="true" type="xs:int" />
<xs:element name="Name" type="xs:string"
minOccurs="0" />
<xs:element name="Location" type="xs:string"
minOccurs="0" />
<xs:element name="Style" type="xs:string"
minOccurs="0" />
<xs:element name="Duration" type="xs:int"
minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

The initial XML file has:
<?xml version="1.0" encoding="utf-8"?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
<!--SABBackgroundSlideShow Image Conrol-->
<Images>
<Sequence></Sequence>
<Name></Name>
<Location></Location>
<Style></Style>
<Duration></Duration>
</Images>
</DataSet1>

When the application opens the grid shows the headers and a row of empty cells. If I delete the
<Images> section. The system complains that it cannot find the table. If I delete just the children
un Images the system complains that there aren't any child entries defined.

So how do I stop the empty row from displaying?
 
Z

Zhi-Xin Ye [MSFT]

Hello Stewart,

Thank you for using Microsoft Managed Newsgroup Service. My name is Zhi-Xin
Ye, it's my pleasure to work with you on this issue.

To show only the headers in the DataGridView, you can call the
DataSet.ReadXmlSchema() method instead. For example:

gridDataSet.ReadXmlSchema("E:\\ImageControl.xml");

Please try my suggestion and let me know the result.

Have a splendid day!

Sincerely,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Stewart Berman

To show only the headers in the DataGridView, you can call the
DataSet.ReadXmlSchema() method instead. For example:

gridDataSet.ReadXmlSchema("E:\\ImageControl.xml");

Actual the solution was as follows:
Starting with an empty XML filet:
<?xml version="1.0" standalone="yes"?>
<DataSet1 xmlns="http://www.tempuri.org/DataSet1.xsd">
</DataSet1>

In the form load event:
// Configure the DataGridView control
dataGridView1.DataSource = gridDataSet;
gridDataSet.ReadXmlSchema("E:\\DataSet1.xsd"); // Loads the schema
gridDataSet.ReadXml("E:\\ImageControl.xml"); // Loads 0 or more records
dataGridView1.DataMember = "Images"; // Identifies the table
// Grid formatting
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
dataGridView1.ColumnHeadersVisible = true;
// Hide the column containing the sequence number
dataGridView1.Columns.GetFirstColumn(DataGridViewElementStates.Visible).Visible = false;

It is necessary to read the schema from the DataSet1.xsd file not the Images.xml file. This should
always be done anyway as it assigns the field attributes. For example the Sequence field is
supposed to have the AutoIncrement attribute. If you don't read the schema from the DataSet1.xsd
file it doesn't have that attribute.
 
Z

Zhi-Xin Ye [MSFT]

Hi Stewart,

Yes, it necessary to call the ReadXmlSchema() method to read the schema.
And do you need further help on this issue? Please let me know if there is
anything I can do to help you.

Best Regards,
Zhi-Xin Ye
Microsoft Managed Newsgroup Support Team

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top