Are you are trying to get different datasources to be displayed in a common
datagrid at different times?
If so, and assuming you are using datatables as your datasources:
When binding the datasource, clear the datagrid's tablestyles collection,
and add the tablestyle needed for the current datasource.
This code covers the basics of setting up tablestyles.
Code:
'Form level objects:
Private DT1 As New DataTable()
Private DT2 As New DataTable()
Private ts1 As New DataGridTableSt yle()
Private ts2 As New DataGridTableSt yle()
'Put in Form Load event:
'define the 2 datatables that will be used as datasources for grid
DT1.Columns.Add (New DataColumn("Fir stName"))
DT1.Columns.Add (New DataColumn("Las tName"))
Dim dr As DataRow = DT1.NewRow
dr.Item(0) = "John"
dr.Item(1) = "Smith"
DT1.Rows.Add(dr )
dr = DT1.NewRow
dr.Item(0) = "Mary"
dr.Item(1) = "Jones"
DT1.Rows.Add(dr )
DT2.Columns.Add (New DataColumn("Con tactNumber"))
DT2.Columns.Add (New DataColumn("Dev iceType"))
dr = DT2.NewRow
dr.Item(0) = "(123) 456-7890"
dr.Item(1) = "cell"
DT2.Rows.Add(dr )
dr = DT2.NewRow
dr.Item(0) = "(987) 654-3210"
dr.Item(1) = "work fax"
DT2.Rows.Add(dr )
'create 2 tablestyles, one to match each datasource
Dim dgc1 As New DataGridTextBox Column()
dgc1.MappingNam e = "FirstName"
dgc1.HeaderText = "First Name"
ts1.GridColumnS tyles.Add(dgc1)
Dim dgc2 = New DataGridTextBox Column()
dgc2.HeaderText = "Last Name"
dgc2.MappingNam e = "LastName"
ts1.GridColumnS tyles.Add(dgc2)
ts1.Alternating BackColor = Color.LightYell ow
Dim dgc3 As New DataGridTextBox Column()
dgc3.HeaderText = "Contact Number"
dgc3.MappingNam e = "ContactNum ber"
ts2.GridColumnS tyles.Add(dgc3)
Dim dgc4 = New DataGridTextBox Column()
dgc4.HeaderText = "Type"
dgc4.MappingNam e = "DeviceType "
ts2.GridColumnS tyles.Add(dgc4)
ts2.Alternating BackColor = Color.MintCream
'create events that set Datagrid's current datasource and tablestyle
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
DG.TableStyles. Clear()
DG.TableStyles. Add(ts1)
DG.DataSource = DT1
End Sub
Private Sub Button2_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button2.Click
DG.TableStyles. Clear()
DG.TableStyles. Add(ts2)
DG.DataSource = DT2
End Sub
www.charlesfarriersoftware.com
"KC" wrote:
Why would the code below fail? I'm making a tablestyle, which works fine. I
test to see if the style already exist for this datagrid, if it does, remove
it before adding the new version. What I don't understand is the 'Contains'
method. I get different answers depending on whether I use:
dgNC.TableStyle s.Contains(dgts NC) or
dgNC.TableStyle s.Contains(dgts NC.MappingName)
I guess I just don't get this tablestyles thing.
-------------------------
Dim dgtsNC As New DataGridTableSt yle
Dim dgNC as DataGrid = DataGrid1
...
create a table style
...
If dgNC.TableStyle s.Contains(dgts NC) Then
dgNC.TableStyle s.Remove(dgtsNC )
Next
dgNC.TableStyle s.Add(dgtsNC)
Ken