Having a problem with table styles

A

Aaron Smith

I can't seem to get my table styles to apply to my DataGrid. In order
for them to apply correctly, what exactly has to be set in the
DataSource and DataMember, and then the DataMapping properties? I keep
trying all different combinations of DataSets, tables, and even
DataViews, but it just isn't working...

I have a DataSet that has the following:
Table1
|_
Table 2
|_
Table 3

Table 3 is the one I am trying to display in the grid..

I've tried for the DataSource the DataSet.Table1 and DataMember
Table2.Table3

Also just DataSet and then in DataMember - Table1.Table2.Table3

Nothing seems to work except doing DataSet and then Table3 for the
DataMember... This is ok, except it displays all the records all the
time, and I only want records for what is selected in Table2....

Aaron
 
K

Ken Tucker [MVP]

Hi,

Use a datareleation and setdatabinding.


Dim ds As DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim daEmployees As OleDbDataAdapter

Dim daOrders As OleDbDataAdapter

Dim daOrderDetails As OleDbDataAdapter

Dim conn As OleDbConnection

Dim strConn As String

Dim strSQL As String

Dim strItem As String

Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf ctrl Is DataGrid Then

Dim dg As DataGrid = ctrl

dg.AllowNavigation = False

End If

Next

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"

strConn &= "Data Source = Northwind.mdb;"



conn = New OleDbConnection(strConn)

ds = New DataSet

daEmployees = New OleDbDataAdapter("Select * from Employees", conn)

daOrders = New OleDbDataAdapter("Select * from Orders", conn)

daOrderDetails = New OleDbDataAdapter("Select * from [Order Details]", conn)

daEmployees.Fill(ds, "Employee")

daOrders.Fill(ds, "Orders")

daOrderDetails.Fill(ds, "OrderDetails")

ds.Relations.Add("EmployeeOrder",
ds.Tables("Employee").Columns("EmployeeID"), _

ds.Tables("Orders").Columns("EmployeeID"))

frm.DataGrid1.SetDataBinding(ds, "Employee.EmployeeOrder")

frm.Show()

ds.Relations.Add("Order2Details", ds.Tables("Orders").Columns("OrderID"), _

ds.Tables("OrderDetails").Columns("OrderID"))

dgEmployees.ReadOnly = True

dgOrders.ReadOnly = True

dgOrderDetails.ReadOnly = True

dgEmployees.SetDataBinding(ds, "Employee")

dgOrders.SetDataBinding(ds, "Employee.EmployeeOrder")

dgOrderDetails.SetDataBinding(ds, "Employee.EmployeeOrder.Order2Details")

End Sub

Private Sub SetupGrid()

Dim ts As New DataGridTableStyle

ts.MappingName = "Orders"

Dim dgc As New DataGridTextBoxColumn

With dgc

..MappingName = "CustomerID"

..HeaderText = "ID"

..Width = 250

End With

ts.GridColumnStyles.Add(dgc)

dgOrders.TableStyles.Add(ts)

End Sub

Private Sub SetupGrid2()

Dim ts As New DataGridTableStyle

ts.MappingName = "OrderDetails"

Dim dgc As New DataGridTextBoxColumn

With dgc

..MappingName = "ProductID"

..HeaderText = "ID"

..Width = 250

End With

ts.ReadOnly = True

ts.GridColumnStyles.Add(dgc)

dgOrderDetails.TableStyles.Add(ts)

End Sub



Ken

-----------------

I can't seem to get my table styles to apply to my DataGrid. In order
for them to apply correctly, what exactly has to be set in the
DataSource and DataMember, and then the DataMapping properties? I keep
trying all different combinations of DataSets, tables, and even
DataViews, but it just isn't working...

I have a DataSet that has the following:
Table1
|_
Table 2
|_
Table 3

Table 3 is the one I am trying to display in the grid..

I've tried for the DataSource the DataSet.Table1 and DataMember
Table2.Table3

Also just DataSet and then in DataMember - Table1.Table2.Table3

Nothing seems to work except doing DataSet and then Table3 for the
DataMember... This is ok, except it displays all the records all the
time, and I only want records for what is selected in Table2....

Aaron
 
A

Aaron Smith

Thats basically how I set it up in the DataSource and DataMember
properties. I found that in order for it to work, I have to first set it
to the tables that are not replated... Ex: DS = DataSet DM = DS.Table3
Then once I do that, the table styles will apply to the grid on the
screen. Then, I can go in and set the DM = DS.Table1.Table2.Table3 and
it is still applied and works correctly..

This works fine on 2 of 3 grids though. They ALL work fine when the
program is run... However, there is one grid that when I set it to the
proper databinding, the IDE will throw some exceptions when switching
between code and design views. I'm going to create a seperate thread for
this problem I think, because I don't think it's really a table style
problem anymore, I think there is something else wrong..

Thanks for the help though, I didn't know you could set the relations in
code like that. I saved the example in case I need it later...

Thanks Again,
Aaron
Hi,

Use a datareleation and setdatabinding.
 

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