Dataset, XML and datagrids

S

Steve

I would not have thought that getting a DataGrid connected to a DataSet
(read in from an XML file) would be this complicated. I cannot get this to
work. I have read everything I could find and modeled all the examples
given but this grid just refuses to behave nicely.

The code is listed below as well as the XML schema and XML data.

The connection between the data set and grid seems to work just fine. What
won't work is the display of the data. No matter what I do, the grid is
always displayed collapsed. I also do not want to display the "Mappings:"
caption that appears above the column headers. The column headers do not
seem to respond to the .width command.

I also read something about a SetDataBindings command that I do not know if
I need or not. Some code has it, some doesn't.

Any help would be, well, helpful.

Thanks.

Steve



***********************

Code



Imports System
Imports System.Data
Imports System.Xml
Imports System.Drawing
Imports System.Windows.Forms

Public Class TestDataGrid

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents dgTest As System.Windows.Forms.DataGrid

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.dgTest = New System.Windows.Forms.DataGrid()


CType(Me.dgTest,System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'
'dgTest
'
Me.dgTest.CaptionVisible = False
Me.dgTest.DataMember = ""
Me.dgTest.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgTest.Location = New System.Drawing.Point(32, 32)
Me.dgTest.Name = "dgTest"
Me.dgTest.Size = New System.Drawing.Size(416, 216)
Me.dgTest.TabIndex = 0
'
'TestDataGrid
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(496, 317)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dgTest})
Me.Name = "TestDataGrid"
Me.Text = "Test Data Grid"
CType(Me.dgTest, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Dim dsTM As DataSet
Dim mstrAppDataPath As String

Private Sub TestDataGrid_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strStartupPath As String
Dim iLen As Integer

' get the application data path

strStartupPath = Application.StartupPath()
iLen = strStartupPath.LastIndexOf("\")
mstrAppDataPath = strStartupPath.Substring(0, iLen)

GetTC2000Mappings()

BindXMLtoGrid()

End Sub

Private Sub GetTC2000Mappings()

Dim sXMLDataFN As String

dsTM = New DataSet("SymbolMapping")

' Load the XML File

sXMLDataFN = mstrAppDataPath & "\data\SymbolMapping.xsd"
dsTM.ReadXmlSchema(sXMLDataFN)

sXMLDataFN = mstrAppDataPath & "\data\SymbolMapping.xml"
dsTM.ReadXml(sXMLDataFN)

End Sub

Private Sub BindXMLtoGrid()

Dim ts As DataGridTableStyle
Dim cs As DataGridTextBoxColumn

dgTest.DataSource = dsTM

'create a custom tablestyle and add three columnstyles
ts = New DataGridTableStyle()
ts.MappingName = "tsSymbolMapping"

' create a column for TC2000

cs = New DataGridTextBoxColumn()
cs.MappingName = "TC2000"
cs.HeaderText = "TC2000 Symbol"
cs.Width = 40
cs.NullText = ""
ts.GridColumnStyles.Add(cs)

' create a column for HQuote

cs = Nothing
cs = New DataGridTextBoxColumn()
cs.MappingName = "HQuote"
cs.HeaderText = "HQuote Symbol"
cs.Width = 40
cs.NullText = ""
ts.GridColumnStyles.Add(cs)

' create a column for Comment
cs = Nothing
cs = New DataGridTextBoxColumn()
cs.MappingName = "Comment"
cs.HeaderText = "Comment"
cs.Width = 160
cs.NullText = ""
ts.GridColumnStyles.Add(cs)


dgTest.TableStyles.Clear()
dgTest.TableStyles.Add(ts)
dgTest.CaptionVisible = False

ts = Nothing
cs = Nothing
End Sub

End Class


***********************

XML Schema in file AppDataPath & "\data\SymbolMapping.xsd"


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://exactlymypoint.com/namespace"
xmlns="http://exactlymypoint.com/namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xs:element name="Mappings">
<xs:annotation>
<xs:documentation>Maps HQuote symbols to TC2000
symbols</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Map">
<xs:complexType>
<xs:sequence>
<xs:element name="HQuote"
type="xs:string"/>
<xs:element name="TC2000"
type="xs:string"/>
<xs:element name="Comment"
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


***********************

XML Data in file AppDataPath & "\data\SymbolMapping.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<Mappings xmlns="http://exactlymypoint.com/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://exactlymypoint.com/namespace
C:\DOCUME~1\sgould\MYDOCU~1\VISUAL~1\HourlyConverter\HourlyConverter\data\Sy
mbolMapping.xsd">
<Map>
<HQuote>$INDU</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (sm)</Comment>
</Map>
<Map>
<HQuote>DJI</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (sm)</Comment>
</Map>
<Map>
<HQuote>TRAN</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average</Comment>
</Map>
<Map>
<HQuote>DJT</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average</Comment>
</Map>
<Map>
<HQuote>UTIL</HQuote>
<TC2000>dj-15</TC2000>
<Comment>Dow Jones Utilities Average</Comment>
</Map>
<Map>
<HQuote>DJU</HQuote>
<TC2000>dj-15</TC2000>
<Comment>Dow Jones Utilities Average</Comment>
</Map>
<Map>
<HQuote> DJX.X</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (CBOE) </Comment>
</Map>
<Map>
<HQuote>DTX.X</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average (CBOE)</Comment>
</Map>
</Mappings>
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Steve,

Some observations and suggestions:

1. All the table style / column style setup stuff should go BEFORE the data
binding occurs. This seems to be not the case in your code and this is the
most likely reason of the problem.

2. It is strongly recommended to use SetDataBinding method instead of
setting the DataSource and DataMember properties directly. The reason seems
to be that this method does some additional but necessary work behind the
scenes which is not done when raw properties are being set.

Hope this helps.
 
S

Steve

Thanks for the help.



I moved the binding statement as such.





Private Sub BindXMLtoGrid()



Dim ts As DataGridTableStyle

Dim cs As DataGridTextBoxColumn



'create a custom tablestyle and add three columnstyles

ts = New DataGridTableStyle()

ts.MappingName = "tsSymbolMapping"



' create a column for TC2000



cs = New DataGridTextBoxColumn()

cs.MappingName = "TC2000"

cs.HeaderText = "TC2000 Symbol"

cs.Width = 40

cs.NullText = ""

ts.GridColumnStyles.Add(cs)



:

:



dgTest.TableStyles.Clear()

dgTest.TableStyles.Add(ts)

dgTest.CaptionVisible = False



dgTest.SetDataBinding(dsTM, "tsSymbolMapping")



ts = Nothing

cs = Nothing



End Sub





The statement

dgTest.SetDataBinding(dsTM, "tsSymbolMapping")



is giving me errors now. It says "Can't create a child list for field
tsSymbolMapping. This was giving me fits earlier. What is the datamember
that is supposed to go here?



Thanks again.



Steve











Dmitriy Lapshin said:
Hi Steve,

Some observations and suggestions:

1. All the table style / column style setup stuff should go BEFORE the data
binding occurs. This seems to be not the case in your code and this is the
most likely reason of the problem.

2. It is strongly recommended to use SetDataBinding method instead of
setting the DataSource and DataMember properties directly. The reason seems
to be that this method does some additional but necessary work behind the
scenes which is not done when raw properties are being set.

Hope this helps.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Steve said:
I would not have thought that getting a DataGrid connected to a DataSet
(read in from an XML file) would be this complicated. I cannot get this to
work. I have read everything I could find and modeled all the examples
given but this grid just refuses to behave nicely.

The code is listed below as well as the XML schema and XML data.

The connection between the data set and grid seems to work just fine. What
won't work is the display of the data. No matter what I do, the grid is
always displayed collapsed. I also do not want to display the "Mappings:"
caption that appears above the column headers. The column headers do not
seem to respond to the .width command.

I also read something about a SetDataBindings command that I do not know if
I need or not. Some code has it, some doesn't.

Any help would be, well, helpful.

Thanks.

Steve



***********************

Code



Imports System
Imports System.Data
Imports System.Xml
Imports System.Drawing
Imports System.Windows.Forms

Public Class TestDataGrid

Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.

InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If

MyBase.Dispose(disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents dgTest As System.Windows.Forms.DataGrid

<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

Me.dgTest = New System.Windows.Forms.DataGrid()


CType(Me.dgTest,System.ComponentModel.ISupportInitialize).BeginInit()

Me.SuspendLayout()

'
'dgTest
'
Me.dgTest.CaptionVisible = False
Me.dgTest.DataMember = ""
Me.dgTest.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.dgTest.Location = New System.Drawing.Point(32, 32)
Me.dgTest.Name = "dgTest"
Me.dgTest.Size = New System.Drawing.Size(416, 216)
Me.dgTest.TabIndex = 0
'
'TestDataGrid
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(496, 317)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.dgTest})
Me.Name = "TestDataGrid"
Me.Text = "Test Data Grid"
CType(Me.dgTest, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region

Dim dsTM As DataSet
Dim mstrAppDataPath As String

Private Sub TestDataGrid_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim strStartupPath As String
Dim iLen As Integer

' get the application data path

strStartupPath = Application.StartupPath()
iLen = strStartupPath.LastIndexOf("\")
mstrAppDataPath = strStartupPath.Substring(0, iLen)

GetTC2000Mappings()

BindXMLtoGrid()

End Sub

Private Sub GetTC2000Mappings()

Dim sXMLDataFN As String

dsTM = New DataSet("SymbolMapping")

' Load the XML File

sXMLDataFN = mstrAppDataPath & "\data\SymbolMapping.xsd"
dsTM.ReadXmlSchema(sXMLDataFN)

sXMLDataFN = mstrAppDataPath & "\data\SymbolMapping.xml"
dsTM.ReadXml(sXMLDataFN)

End Sub

Private Sub BindXMLtoGrid()

Dim ts As DataGridTableStyle
Dim cs As DataGridTextBoxColumn

dgTest.DataSource = dsTM

'create a custom tablestyle and add three columnstyles
ts = New DataGridTableStyle()
ts.MappingName = "tsSymbolMapping"

' create a column for TC2000

cs = New DataGridTextBoxColumn()
cs.MappingName = "TC2000"
cs.HeaderText = "TC2000 Symbol"
cs.Width = 40
cs.NullText = ""
ts.GridColumnStyles.Add(cs)

' create a column for HQuote

cs = Nothing
cs = New DataGridTextBoxColumn()
cs.MappingName = "HQuote"
cs.HeaderText = "HQuote Symbol"
cs.Width = 40
cs.NullText = ""
ts.GridColumnStyles.Add(cs)

' create a column for Comment
cs = Nothing
cs = New DataGridTextBoxColumn()
cs.MappingName = "Comment"
cs.HeaderText = "Comment"
cs.Width = 160
cs.NullText = ""
ts.GridColumnStyles.Add(cs)


dgTest.TableStyles.Clear()
dgTest.TableStyles.Add(ts)
dgTest.CaptionVisible = False

ts = Nothing
cs = Nothing
End Sub

End Class


***********************

XML Schema in file AppDataPath & "\data\SymbolMapping.xsd"


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="http://exactlymypoint.com/namespace"
xmlns="http://exactlymypoint.com/namespace"
xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"
attributeFormDefault="unqualified">

<xs:element name="Mappings">
<xs:annotation>
<xs:documentation>Maps HQuote symbols to TC2000
symbols</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:sequence maxOccurs="unbounded">
<xs:element name="Map">
<xs:complexType>
<xs:sequence>
<xs:element name="HQuote"
type="xs:string"/>
<xs:element name="TC2000"
type="xs:string"/>
<xs:element name="Comment"
type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>


***********************

XML Data in file AppDataPath & "\data\SymbolMapping.xsd"

<?xml version="1.0" encoding="UTF-8"?>
<Mappings xmlns="http://exactlymypoint.com/namespace"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://exactlymypoint.com/namespace
C:\DOCUME~1\sgould\MYDOCU~1\VISUAL~1\HourlyConverter\HourlyConverter\data\Sy
mbolMapping.xsd">
<Map>
<HQuote>$INDU</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (sm)</Comment>
</Map>
<Map>
<HQuote>DJI</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (sm)</Comment>
</Map>
<Map>
<HQuote>TRAN</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average</Comment>
</Map>
<Map>
<HQuote>DJT</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average</Comment>
</Map>
<Map>
<HQuote>UTIL</HQuote>
<TC2000>dj-15</TC2000>
<Comment>Dow Jones Utilities Average</Comment>
</Map>
<Map>
<HQuote>DJU</HQuote>
<TC2000>dj-15</TC2000>
<Comment>Dow Jones Utilities Average</Comment>
</Map>
<Map>
<HQuote> DJX.X</HQuote>
<TC2000>dj-30</TC2000>
<Comment>Dow Jones Industrial Average (CBOE) </Comment>
</Map>
<Map>
<HQuote>DTX.X</HQuote>
<TC2000>dj-20</TC2000>
<Comment>Dow Jones Transportation Average (CBOE)</Comment>
</Map>
</Mappings>
 

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