By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,702 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,702 IT Pros & Developers. It's quick & easy.

Custom DataGridView and Xml Schema Enumerations

P: n/a
Core Question: Is there a better way to dynamically pull the allowed
values of an attribute out of a dataset's schema.

I have an XML file that contains a string element with two attributes,
weight and color. for example,

<keyword color="blue" weight="normal">And</keyword>

the color and weight attributes are restricted to only certain values
(color to the 16 named colors in HTML; weight to normal, bold or italic
-- the defaults being blue and normal, respectively). for example,

<xs:simpleType name="color">
<xs:restriction base="xs:string">
<xs:enumeration value="aqua"/>
<xs:enumeration value="black"/>
<xs:enumeration value="black"/>
<xs:enumeration value="blue"/>
<xs:enumeration value="fuchsia"/>
<xs:enumeration value="gray"/>
<xs:enumeration value="green"/>
<xs:enumeration value="lime"/>
<xs:enumeration value="maroon"/>
<xs:enumeration value="navy"/>
<xs:enumeration value="olive"/>
<xs:enumeration value="purple"/>
<xs:enumeration value="red"/>
<xs:enumeration value="silver"/>
<xs:enumeration value="teal"/>
<xs:enumeration value="white"/>
<xs:enumeration value="yellow"/>

I also have a VS-generated typed DataSet which I use to populate a
DataGridView. since I want to limit the values of these two attributes,
I am using a DataGridViewComboBoxColor to represent each.

Dim colColor As New DataGridViewComboBoxColumn()
colColor.HeaderText = "Color"
colColor.DataPropertyName = kwords.keyword.colorColumn.ColumnName

two parts are giving me fits
1) getting the allowed values of each column from the DataSet -- I see
no easy way to extract the schema from the DataSet in memory and have
resorted to reading the schema directly from the file an additional time

2) the procedure for pulling the allowed values from the enumeration
seems overly complex -- IS THERE A BETTER WAY THAN THIS?

here's working code:
'TODO: this seems awkward -- find a better way
Dim schemaset As New XmlSchemaSet()
schemaset.Add(Nothing, My.Application.AssemblyInfo.DirectoryPath +
Dim schema As XmlSchema
For Each schema In schemaset.Schemas()
Dim schobj As XmlSchemaObject
'iterate through types
For Each schobj In schema.SchemaTypes.Values

If TypeOf (schobj) Is XmlSchemaSimpleType Then 'simple type

'cast to simple type
Dim simple As XmlSchemaSimpleType = _
CType(schobj, XmlSchemaSimpleType)

If TypeOf (simple.Content) Is _
XmlSchemaSimpleTypeRestriction Then 'has restriction

If (simple.Name = "color") Then 'color attribute

'cast Content to STR
Dim rest As XmlSchemaSimpleTypeRestriction = _
CType(simple.Content, _

'each enum value shows as a facet
For Each facet As XmlSchemaFacet In rest.Facets
'add value to combo box items
Next 'facet

ElseIf (simple.Name = "weight") Then 'weight attribute

Dim rest As XmlSchemaSimpleTypeRestriction = _
CType(simple.Content, _

For Each facet As XmlSchemaFacet In rest.Facets
Next 'facet

End If 'Color

End If 'simle.Content

End If 'XmlSchemaSimpleType

Next 'schobj

Next 'schema

nemo me impune lacessit

*** Sent via Developersdex ***
Nov 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.