473,396 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Tablestyle problems

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
Nov 21 '05 #1
2 1559
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


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

Nov 21 '05 #2
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

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

Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Herve MAILLARD | last post by:
I have added aTableStyle to my Datagrid. The table linked to the datagrid display a boolean field. Before apply the TableStyle,the datagrid was displaying a Checkbox. Since I have apply the...
4
by: Ken Powers | last post by:
I have a bound datagrid that has data but the formatting from the TableStyle is not affecting the appearence of the grid. Code below. Private Sub BindDataGrid() Try...
1
by: Agnes | last post by:
For single datagrid, i can bind the table very well, I can add myGridTablestyle to the datagrid. However, after i set the relations before dgParents and dgDetails. It ignore my tablestyle()....
0
by: KC | last post by:
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...
1
by: Bob | last post by:
is it possible to remove just a specific tablestyle and keep anyothers that may exist? I've got the folowing code: Dim TS As New DataGridTableStyle TS.MappingName = "ABQTG0UN" ...... ...
3
by: Reza G. | last post by:
I am creating a window forms, and placed a datagrid on it. Set the DataSource and DataMember, and I get all the column in the table (relation) shown. I used TableStyles of the DataGrid and add a...
2
by: Mac via DotNetMonster.com | last post by:
Hi all, I have a datagrid on a form which I have set the datasource (through the form designer) to be a dataset that I have added to the form, also through the form designer. I then have added a...
0
by: Kmistic | last post by:
I have created the datagrid in the following link http://support.microsoft.com/default.aspx?scid=kb;en-us;836672 When i try to apply a tablestyle i get an exeption stating that the data grid...
0
by: AliRezaGoogle | last post by:
Hi I have a problem with tablestyle of datagrid: First I create a new tablestyle in form designer. Then I add some gridecolumnstyle to the new tablestyle. I fill all MappingNames with correct...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.