Reorder columns in datatable?
I'm using xsd files to validate xml files. In the xsd files, only a few of the elements are required and the rest are optional. However, it is sequential so the order of the elements as they appear in the datatable matter lots. After considering a few scenarios the user may encounter, I came up with a case that I have a little trouble solving.
The case is this: For example, There are 6 elements A, B, C, D, E, F in the xsd file. Only A, C, and D are needed. The user starts off with A, C, D. I allow 开发者_Go百科the user to add a column, with the name B. However, it gets attached on the end: A, C, D, B. Therefore, since it's sequential, it will not validate.
So my question is, how do I insert B between A and C in order for the xsd validation to pass? Or is there any other way to reorder the things in the datagridview and reflect that in the datatable?
I thought I would be able to solve the problem by allowing the user to reorder the columns in the datagridview where the table is displayed.
EDIT: Under the assumption that I cannot alter the xsd files.
That is,
dataGridView1.AllowUserToOrderColumns = true;
However, the validation always told me that an "element" after the last necessary element was expected. This means that the shift in the datacolumn in the datagridview was not reflected in the datatable itself.
In order to pass the validation, I believe I would need to insert the column in the respective place it is relative to everything else. Or at least insert it at the end and reflect the proper order when the user presses the validate button.
I've come across some other things online telling me to try things like:
Table.Columns["Column Name"].SetOrdinal(NewIndex);
But I have not yet implemented it. Any ideas in case that line above doesn't work? =) Additionally, I want to be able to sync the datatable with what the user does in the datagridview. That would be ideal. If the user moves the columns in the datagridview, the datatable should reflect that change.
Any insight is greatly appreciated, I'll be happy to offer additional information if needed. Thanks!
Sincerely, tf.rz
(.NET 3.5 SP1, Visual Studio C# 2008)
You haven't given your XSD, but I'm guessing you're using
<xs:sequence>.
Instead use
<xs:all>
e.g.
<xs:element name="person">
<xs:complexType>
<xs:all minOccurs="0">
<xs:element name="firstname" type="xs:string"/>
<xs:element name="lastname" type="xs:string"/>
</xs:all>
</xs:complexType>
</xs:element>
The example above indicates that the "firstname" and the "lastname" elements can appear in any order and each element CAN appear zero or one time!
See http://www.w3schools.com/schema/el_all.asp for further info.
Wouldn't it make more sense to not require the user to enter the data in schema order, and instead have the system automatically determine the appropriate order based on the schema? It seems that your schema(s) will be of similar construction given your assumptions, so determining the order from the schema should not be difficult.
For ex., you could leave the DataTable
as is, matching the user input, with columns in any arbitrary order. When you need to output the data to xml, examine the schema file and identify the sequence of elements that should be output. Iterate over the list of elements, and output matching columns from your DataTable
as they're found.
To process the schema file and discover the sequence, you could either use System.Xml.Schema.XmlSchema
, or, as the schema is itself xml, you could query the xml directly using XmlDocument
, XPath
, XDocument
etc.
精彩评论