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!

Populating a DataGrid a Second Time 3

Status
Not open for further replies.

dolfo35

Programmer
Jan 26, 2001
28
US
Hello,

I need to populate a datagrid a second time. The first time it is populated works perfect. However, when I click on the button that populates it a second time I get this error:

THE DATA GRID STYLES COLLECTION ALREADY CONTAINS A TABLE STYLE WITH THE SAME MAPPING NAME!

Code:
' Code that fills the dataset

Grid1.DataSource = dataset.Tables(0)

' Apply new TableStyles to DataGrid
Dim dgStyle1 As New DataGridTableStyle
dgStyle1.MappingName = dataset.Tables(0).ToString

' First column (Key field in table)
Dim dgTextCol As New DataGridTextBoxColumn
dgTextCol.MappingName = "Key"
dgTextCol.HeaderText = "Key"
dgTextCol.Width = 0

' Add dgTextCol text box column to dgStyle table style
dgStyle1.GridColumnStyles.Add(dgTextCol)

' Second column (Field in table)
dgTextCol = New DataGridTextBoxColumn
dgTextCol.MappingName = "Col1"
dgTextCol.HeaderText = "Col1"
dgTextCol.Width = 150
dgStyle1.GridColumnStyles.Add(dgTextCol)

' Third column (field in table)
dgTextCol = New DataGridTextBoxColumn
dgTextCol.MappingName = "Col2"
dgTextCol.HeaderText = "Col2"
dgTextCol.Width = 100
dgStyle1.GridColumnStyles.Add(dgTextCol)

' Forth column (field in table)
dgTextCol = New DataGridTextBoxColumn
dgTextCol.MappingName = "Col3"
dgTextCol.HeaderText = "Col3"
dgTextCol.Width = 100
dgStyle1.GridColumnStyles.Add(dgTextCol)

' Fifth column (field in table)
dgTextCol = New DataGridTextBoxColumn
dgTextCol.MappingName = "Col4"
dgTextCol.HeaderText = "Col4"
dgTextCol.Width = 200
dgStyle1.GridColumnStyles.Add(dgTextCol)

' Second column (field in table)
dgTextCol = New DataGridTextBoxColumn
dgTextCol.MappingName = "Col5"
dgTextCol.HeaderText = "Col5"
dgTextCol.Width = 100
dgStyle1.GridColumnStyles.Add(dgTextCol)


'********  This next line of code is where the error occurs when the button is clicked and the grid is already populated.

Grid1.TableStyles.Add(dgStyle1)

The reason I need to populate the grid a second time is that the data is a result of a stored procedure that has two dates as parameters. If one of those date changes, the data will change also.

Thanks in advance for any help you provide. I've read some really good advice/pointers/help/tek-tips in this forum. Thank you for that too.

Rudy
 
how about:
Code:
if Grid1.tablestyles.count = 0 then Grid1.TableStayles.Add(dgstyle1)

Not sure if that'll work, I haven't worked much with the data grid control from MS. But you should be able to determine if there is a table style, or if the table's current style is the same as the one you just created.

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
do you really want to redo the columnstyle whenever the data changes? I think not. Just don't do that and just refill the table a second time. Something like this will work

if grid1.tablestyles.count < 1 then 'or whatever it is in the beginning before you add your style to it
'apply grid styles here
else
'do nothing
end if

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Thank you Rick and Christiaan. Even though the reply was not exactly correct, it did lead me to the solution.

' since tablestyles does not have a count property (or I didn't see it anyway)
Grid1.tablestyles.clear

' Then add the style
Grid1.TableStyles.Add(dgStyle1)

That got me working again! Thanks again.

Rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top