DataSet Wrapper - DataGrid.MappingName Issues

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.
 
G

Guest

Wouldn't you know it. After looking at this forever, just after I post, I
find a solution/work around for it.

It turns out that ds.Orders.TableName still returns "table" since that is
the name of the base table in the DataSet. If I change the OrdersWrapper
class as follows, all works as intended.

Public Class OrdersWrapper
Inherits dsOrders

<System.ComponentModel.Browsable(False), _

System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)> _
Public ReadOnly Property Orders() As tableDataTable
Get
Me.table.TableName = "Orders"
Return Me.table
End Get
End Property
End Class
 
B

Bart Mermuys

Hi,

Aspnot said:
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.

Maybe i'm missing something, but step 3 doesn't seem to work for me. It
looks like you're using a typed dataset. If i drag a table to the form then
a DataAdapter is created, then i right click the form and choose generate
dataset. Then i end up with a typed dataset containing an Orders table.

Anyway, all that matters is you have a DataSet with an Orders table, then i
see no reason to rename the table Orders and write a wrapper that does the
opposite.

Just let the table name be Orders and use the right Fill overload :

' Fill DataTable Orders in DataSet ds
da.Fill( ds, "Orders" )

-or-

' Fill DataTable Orders
da.Fill( ds.Orders )


Don't use da.Fill(ds), because then .NET will choose a table name ("table")
that will be used to fill.


HTH,
Greetings
 

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