G
Guest
I am using a wrapper around my DataSets so that I can call the tables by
their appropriate names and not table, Table1, Table2, etc. When I populate
a DataGrid upon which I have applied a DataGridTableStyle, the DataGrid does
not apply the DataGridTableStyle when I use a MappingName that points to my
wrapper. It does, however, show properly when I pass a MappingName of
'table'.
The code below is a quick application that I put together to demonstrate my
problem. I am hoping that someone with some more experience can point me in
the right direction.
1 - To start, create a new VB.NET project in VS
2 - Add a dataset to the project named dsOrders
3 - From Server Explorer, select your Northwind database and drag the Orders
into dsOrders
4 - Within dsOrders, rename "Orders" to "table" since this is the way tables
usually get named within a stored procedure.
5 - Edit the code below to point to your SQL Server and also change the
security appropriately.
Replace your Form1 code with the following:
Public Class Form1
Inherits System.Windows.Forms.Form
Private ds As New OrdersWrapper
#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 DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(5, 5)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(667, 387)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 400)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 22)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Load with 'Orders' MappingName"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(480, 400)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(176, 22)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Load with 'table' MappingName"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(680, 430)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cn As New System.Data.SqlClient.SqlConnection("Data
Source=YOURDBSERVER;Initial Catalog=NorthWind;User ID=XXXX;Password=XXXX")
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim da As New System.Data.SqlClient.SqlDataAdapter
With cmd
.CommandText = "Select * from Orders"
.CommandType = CommandType.Text
.Connection = cn
End With
da.SelectCommand = cmd
da.Fill(ds)
End Sub
Private Sub ConfigDG(ByVal inMappingName As String)
Dim dgts As New DataGridTableStyle
With dgts
.AlternatingBackColor = System.Drawing.Color.LemonChiffon
.HeaderForeColor = System.Drawing.SystemColors.ControlText
.MappingName = inMappingName
.RowHeadersVisible = False
End With
With DataGrid1
.CaptionVisible = False
.TableStyles.Clear()
.TableStyles.Add(dgts)
.DataSource = ds.Orders
.ReadOnly = True
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call ConfigDG("Orders")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Call ConfigDG("table")
End Sub
End Class
Public Class OrdersWrapper
Inherits dsOrders
<System.ComponentModel.Browsable(False), _
System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Orders() As tableDataTable
Get
Return Me.table
End Get
End Property
End Class
Any and all assistance in this matter will be greatly appreciated. I have
been working on this issue off and on for about 3 weeks now and I really need
to get my DataGrids working.
their appropriate names and not table, Table1, Table2, etc. When I populate
a DataGrid upon which I have applied a DataGridTableStyle, the DataGrid does
not apply the DataGridTableStyle when I use a MappingName that points to my
wrapper. It does, however, show properly when I pass a MappingName of
'table'.
The code below is a quick application that I put together to demonstrate my
problem. I am hoping that someone with some more experience can point me in
the right direction.
1 - To start, create a new VB.NET project in VS
2 - Add a dataset to the project named dsOrders
3 - From Server Explorer, select your Northwind database and drag the Orders
into dsOrders
4 - Within dsOrders, rename "Orders" to "table" since this is the way tables
usually get named within a stored procedure.
5 - Edit the code below to point to your SQL Server and also change the
security appropriately.
Replace your Form1 code with the following:
Public Class Form1
Inherits System.Windows.Forms.Form
Private ds As New OrdersWrapper
#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 DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(5, 5)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(667, 387)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 400)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(176, 22)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Load with 'Orders' MappingName"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(480, 400)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(176, 22)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Load with 'table' MappingName"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(680, 430)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.DataGrid1)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1,
System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim cn As New System.Data.SqlClient.SqlConnection("Data
Source=YOURDBSERVER;Initial Catalog=NorthWind;User ID=XXXX;Password=XXXX")
Dim cmd As New System.Data.SqlClient.SqlCommand
Dim da As New System.Data.SqlClient.SqlDataAdapter
With cmd
.CommandText = "Select * from Orders"
.CommandType = CommandType.Text
.Connection = cn
End With
da.SelectCommand = cmd
da.Fill(ds)
End Sub
Private Sub ConfigDG(ByVal inMappingName As String)
Dim dgts As New DataGridTableStyle
With dgts
.AlternatingBackColor = System.Drawing.Color.LemonChiffon
.HeaderForeColor = System.Drawing.SystemColors.ControlText
.MappingName = inMappingName
.RowHeadersVisible = False
End With
With DataGrid1
.CaptionVisible = False
.TableStyles.Clear()
.TableStyles.Add(dgts)
.DataSource = ds.Orders
.ReadOnly = True
End With
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Call ConfigDG("Orders")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Call ConfigDG("table")
End Sub
End Class
Public Class OrdersWrapper
Inherits dsOrders
<System.ComponentModel.Browsable(False), _
System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Orders() As tableDataTable
Get
Return Me.table
End Get
End Property
End Class
Any and all assistance in this matter will be greatly appreciated. I have
been working on this issue off and on for about 3 weeks now and I really need
to get my DataGrids working.