Getting frustrated looking for answers for hours on this one…
I have an .xml file that I deserialize into a DataSet, along with an .xsd so types of the dataset are known. Like this:
mDataSetVariables = new DataSet();
using (var rdr = new StringReader(Properties.Resources.VariableListSchema))
{
mDataSetVariables.ReadXmlSchema(rdr);
}
mDataSetVariables.ReadXml(filename, XmlReadMode.ReadSchema);
dataGridView_Params.Columns.Clear();
dataGridView_Params.DataSource = mDataSetVariables;
dataGridView_Params.DataMember = "Variable";
The .xml file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<VariableList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<List>
<Variable>
<Name>kPeakPeriodAccThresh</Name>
<Type>FixedPoint</Type>
<Min>0.0001</Min>
<Max>0.02</Max>
<InitialValue>0.003</InitialValue>
<BestValue>0.0052570276796268427</BestValue>
<CurrentValue>0.006459431678617374</CurrentValue>
<Enabled>true</Enabled>
</Variable>
< ... more variables here >
And the .xsd looks like this (generated by xsd.exe on my classes of type Variable and VariableList):
<?xml version="1.0" encoding="utf-8"?>
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="VariableList" nillable="true" type="VariableList" />
<xs:complexType name="VariableList">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="List" type="ArrayOfVariable" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="ArrayOfVariable">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Variable" nillable="true" type="Variable" />
</xs:sequence>
</xs:complexType>
<xs:complexType name="Variable">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="1" name="Name" type="xs:string" />
<xs:element minOccurs="1" maxOccurs="1" name="Type" type="VariableType" />
<xs:element minOccurs="1" maxOccurs="1" name="Min" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Max" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="InitialValue" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="BestValue" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="CurrentValue" type="xs:double" />
<xs:element minOccurs="1" maxOccurs="1" name="Enabled" type="xs:boolean" />
</xs:sequence>
</xs:complexType>
<xs:simpleType name="VariableType">
<xs:restriction base="xs:string">
<xs:enumeration value="Integer" />
<xs:enumeration value="FixedPoint" />
<xs:enumeration value="Boolean" />
</xs:restriction>
</xs:simpleType>
</xs:schema>
When I bind the DataSet to the Datagridview, the Boolean value “Enabled” automatically gets represented with checkboxcolumn. How can I get the “Type” column to automatically be a comboboxcolumn with a dropdown of the enum types?
I’ve found several other answers online but none that seem to work with my scenario. If I have to change how things are arranged (i.e. use a strongly typed data set), then I guess I will but I’d like to get it working with the code as is.
Many thanks…
the problem is, that as far as your dataset is concerned, that column is no enum but a string …
the dataset will happily ignore that enum restriction …
you can verify that, if you ask your mDataSetVariables for getXmlSchema()
note the change from
to
to handle this problem you will have to parse the xsd yourself, and identify the enum columns, build up string arrays with the appropriate values, and deliver those as datasource for the comboboxes …
i would go for strongly typed datasets …