Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGrid - DataGridTableStyle???

Status
Not open for further replies.

cschristian78

Technical User
Nov 3, 2004
23
US
I have about 10 queries that I run based on what the user selects in a listbox. Those 10 queries can be put into 1 of 3 catagories: LP1, LP2 and LP3. Each catagory has a different number of columns. For example, I run a query that is in cat LP1 which may have 4 columns. I then select another entry in the listbox which calls a query from the LP2 catagory. This query may have 3 of the same columns from the LP1 query but may also contain a couple more. When I then select another query that goes back to the four column query, I have nulls in the additional two columns. I would like to remove the additional columns. I assume that I need to use the DataGridTableStyle. When setting that up, I get "The data grid table styles collection already contains a table style with the same mapping name execption. I am unsure how to make the mapping name unique. Here is the following code. (LP2 and LP3 have the same code outside of the query and all tables, tablestyles are the same except the number eg dgtsLP1 vs dgtsLP2 vs dgtsLP3). Thanks

Public Sub FillGrid_LP1()
Dim dgtsLP1 As New DataGridTableStyle()

'Clear data in Dataset
Try
dgtsLP1.Dispose()
dsGrid.Tables("dtLP1").Clear()
Catch
Exit Try
End Try

'Create Profile for specified type
daLP.SelectCommand = New SqlClient.SqlCommand(Query, ProfDataCon)

Try
dgtsLP1.MappingName = lstbxProfiles.SelectedItem(0).ToString

'Fill Dataset
daLP.Fill(dsGrid, "dtLP1")
'fill DataGrid by setting bindings
dgrdResults.DataSource = dsGrid.Tables("dtLP1")
dgrdResults.TableStyles.Add(dgtsLP1)
dgrdResults.CaptionText = lstbxProfiles.SelectedItem(0).ToString
Catch ex As Exception
daLP.Fill(dsGrid, "dtLP1")
'fill DataGrid by setting bindings
dgrdResults.DataSource = dsGrid.Tables("dtLP1")
dgrdResults.CaptionText = lstbxProfiles.SelectedItem(0).ToString
'MessageBox.Show(ex.ToString)
End Try

End Sub
 
Hi cschristian78..

check out the following code.. It setup the Datagrid easier than what you were trying to use..

Private Sub Setup_Datagrid2()
With DataGrid2
.BackColor = Color.GhostWhite
.BackgroundColor = Color.Lavender
.BorderStyle = BorderStyle.None
.CaptionBackColor = Color.RoyalBlue
.CaptionFont = New Font("Tahoma", 12.0!, FontStyle.Bold)
.CaptionForeColor = Color.Bisque
.CaptionText = "Installed Software"
.Font = New Font("Tahoma", 10.0!)
.ParentRowsBackColor = Color.Lavender
.ParentRowsForeColor = Color.MidnightBlue
End With

Dim grdTableStyle1 As New DataGridTableStyle()
With grdTableStyle1
.AlternatingBackColor = Color.GhostWhite
.BackColor = Color.GhostWhite
.ForeColor = Color.MidnightBlue
.GridLineColor = Color.RoyalBlue
.HeaderBackColor = Color.MidnightBlue
.HeaderFont = New Font("Tahoma", 12.0!, FontStyle.Bold)
.HeaderForeColor = Color.Lavender
.SelectionBackColor = Color.Teal
.SelectionForeColor = Color.PaleGreen
' Do not forget to set the MappingName property.
' Without this, the DataGridTableStyle properties
' and any associated DataGridColumnStyle objects
' will have no effect.
.MappingName = "BaseLine"
.PreferredColumnWidth = 125
.PreferredRowHeight = 15
End With

Dim grdColStyle1 As New DataGridTextBoxColumn()
With grdColStyle1
.HeaderText = " Software Titles"
.MappingName = "Title"
.Width = 355
.Alignment = HorizontalAlignment.Center
End With

Dim grdColStyle2 As New DataGridTextBoxColumn()
With grdColStyle2
.HeaderText = " Flag"
.MappingName = "FlagSet"
.Width = 20
.Alignment = HorizontalAlignment.Center
End With

' Add the style objects to the table style's collection of
' column styles. Without this the styles do not take effect.
grdTableStyle1.GridColumnStyles.AddRange _
(New DataGridColumnStyle() _
{grdColStyle1, grdColStyle2})

DataGrid2.TableStyles.Add(grdTableStyle1)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top