Tablestyle problems

  • Thread starter Thread starter KC
  • Start date Start date
K

KC

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.TableStyles.Contains(dgtsNC) or

dgNC.TableStyles.Contains(dgtsNC.MappingName)

I guess I just don't get this tablestyles thing.


-------------------------
Dim dgtsNC As New DataGridTableStyle
Dim dgNC as DataGrid = DataGrid1
...
create a table style
...

If dgNC.TableStyles.Contains(dgtsNC) Then
dgNC.TableStyles.Remove(dgtsNC)
Next
dgNC.TableStyles.Add(dgtsNC)



Ken
 
Try something like the below code (note you should run this check before you
actually add the dgtsNC to your dgNC tablestyles collection:

Dim ts As DataGridTableStyle
Dim foundmapname as boolean
For Each ts In dgNC.TableStyles
if dgNC.MappingName = dgtsNC.MappingName then
foundmapname = True
exit for
end if
next
 
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 DataGridTableStyle()
Private ts2 As New DataGridTableStyle()

'Put in Form Load event:

'define the 2 datatables that will be used as datasources for grid
DT1.Columns.Add(New DataColumn("FirstName"))
DT1.Columns.Add(New DataColumn("LastName"))
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("ContactNumber"))
DT2.Columns.Add(New DataColumn("DeviceType"))
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 DataGridTextBoxColumn()
dgc1.MappingName = "FirstName"
dgc1.HeaderText = "First Name"
ts1.GridColumnStyles.Add(dgc1)
Dim dgc2 = New DataGridTextBoxColumn()
dgc2.HeaderText = "Last Name"
dgc2.MappingName = "LastName"
ts1.GridColumnStyles.Add(dgc2)
ts1.AlternatingBackColor = Color.LightYellow

Dim dgc3 As New DataGridTextBoxColumn()
dgc3.HeaderText = "Contact Number"
dgc3.MappingName = "ContactNumber"
ts2.GridColumnStyles.Add(dgc3)
Dim dgc4 = New DataGridTextBoxColumn()
dgc4.HeaderText = "Type"
dgc4.MappingName = "DeviceType"
ts2.GridColumnStyles.Add(dgc4)
ts2.AlternatingBackColor = Color.MintCream

'create events that set Datagrid's current datasource and tablestyle

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
DG.TableStyles.Clear()
DG.TableStyles.Add(ts1)
DG.DataSource = DT1
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
DG.TableStyles.Clear()
DG.TableStyles.Add(ts2)
DG.DataSource = DT2
End Sub


www.charlesfarriersoftware.com
 

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

Back
Top