473,511 Members | 16,846 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1566
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
1593
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
2664
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
988
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
245
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
1279
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
1804
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
1088
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
957
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
912
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
7138
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
7355
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
7423
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
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5066
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
781
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.