Originally posted by Mr Ed
View Post
The Problem is when you create a New DataGrid the Old one is Kaput so a new one is required each time
So you need to sort out not only the Tree Click but the Tab Click as well to do this (Depends what you are doing)
So try it from here anyway and see how you get On
First to make life easy Take all your DataGrid stuff out of where it is and place it in a Separate Sub otherwise you will have miles of code to trawl through – You can just Edit it from the New sub then when you want new Columns etc
This is A SNIP what I’m using at the mo so see how you get on
Dave

Code:
Private WithEvents objDataGrid As DataGridView 'For Click Events etc OK 28th Oct
'Data Grid Designand Properties here - Add Cols Rows as req etc OK 28th Oct
Sub BuildDataGrid() 'http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview(v=vs.110).aspx
With objDataGrid
.ColumnCount = 9 'Actually 10 but the cbx Col is added s extra
.Columns(0).Name = "Runners"
.Columns(1).Name = "Sel ID"
.Columns(2).Name = "Back"
.Columns(3).Name = "Lay"
.Columns(4).Name = "SP"
.Columns(5).Name = "Liab"
.Columns(6).Name = "Stk"
.Columns(7).Name = "MUM" 'Matched or Unmatched
.Columns(8).Name = "Status" 'Moved this after the New Checkboxes (It is still Col 8 although it is 9)
' ======Header Style======
With .ColumnHeadersDefaultCellStyle
.BackColor = Color.Blue 'xx Does not Work here??
.ForeColor = Color.White 'xx Why not?
.Font = New Font(objDataGrid.Font, FontStyle.Bold) 'xx Because these work?
.Alignment = DataGridViewContentAlignment.MiddleCenter 'and this
End With
'====Default Settings=======
.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
.CellBorderStyle = DataGridViewCellBorderStyle.Single
.GridColor = Color.Black
.RowHeadersVisible = False 'No thanks
.Dock = DockStyle.Fill
.AllowUserToAddRows = False
.AllowUserToOrderColumns = False
.AllowUserToResizeColumns = True 'Only if Width is Not set to Auto
.AllowUserToResizeRows = False
'.ReadOnly = True Do seperatly as some are like stakes and liability
'.SelectionMode = DataGridViewSelectionMode.CellSelect 'Selects Single cell for click events etc
.SelectionMode = DataGridViewSelectionMode.FullRowSelect 'Selects The Full Row but above can still be accessed
.MultiSelect = False
.BackgroundColor = Color.CadetBlue
'Selections
With .Columns(0) 'Horse Names
.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
.SortMode = DataGridViewColumnSortMode.NotSortable
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.ReadOnly = True
End With 'Selections
'Selection ID
With .Columns(1) 'Sel Id Hide this Later
.SortMode = DataGridViewColumnSortMode.NotSortable
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.ReadOnly = True
End With 'sel ID
With .Columns(2) 'Back Column Format
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.DefaultCellStyle.BackColor = Color.AliceBlue
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = True
End With 'Back Column Format
With .Columns(3) 'Lay Column Format
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.DefaultCellStyle.BackColor = Color.MistyRose
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = True
End With 'Lay Column Format
With .Columns(4) 'SP Column
'.Width = .AutoSizeMode
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.DefaultCellStyle.BackColor = Color.LightBlue
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = True
End With 'SP Column Format
'======Editable Liability and stks Columns========
With .Columns(5) ''Liability
.Width = 60
.DefaultCellStyle.BackColor = Color.LightSeaGreen
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = False
End With 'Liability Column Format
'Stakes Column
With .Columns(6) 'Stakes
.Width = 60
.DefaultCellStyle.BackColor = Color.LightBlue
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = False
End With 'Stakes
'Matched or Unmatched Column
With .Columns(7) 'MU
.Width = 30
.DefaultCellStyle.BackColor = Color.LightBlue
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = True
End With 'Matched Unmatched Not editable
'Runner Status Column
With .Columns(8) 'Status Column Format
.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.DefaultCellStyle.BackColor = Color.LightGray
.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
.ReadOnly = True
End With 'Status Column Format
''CheckBoxes Extra column 'http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcheckboxcolumn(v=vs.110).aspx
Dim column As New DataGridViewCheckBoxColumn()
With column
.HeaderText = "Bet"
.Name = "colBet"
.ReadOnly = False
.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells
.FlatStyle = FlatStyle.Standard
.CellTemplate = New DataGridViewCheckBoxCell()
.CellTemplate.Style.BackColor = Color.Beige
End With
objDataGrid.Columns.Insert(8, column) 'Insert 1 from the End this is inserted between 8 and 9
End With '========Data Grid==========
End Sub 'Create the Basic Data Grid and the properties 'OK 28th Oct
Code:
'Display the Runners and Selection ID's
For i = 0 To selList.Count - 1
With objDataGrid
.Rows.Add(selList.Item(i), idList.Item(i))
End With
Next
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Add an Extra row for other Data
objDataGrid.Rows.Add(selList.Count & " Declared")
End With 'tabMarkets '
[B][COLOR="Red"] Else 'We have a Tab ID but the Grid does need a new one again for reloads as Book will not Update otherwise[/COLOR][/B]
'The Problem here is after a Non runner and the BF wankers think its a good idea to Move them around!
tabMarkets.SelectedTab = tabMarkets.TabPages(MarketID) 'Because this is tree click
''==============NEW GRID IF TREE IS CLICKED AFTERWARDS===================================
Try 'Delete the Tab and get new one (DO IF IN TREE LIST--Else New Info closed!)
'Need to put back in right place and Insert not Add as Above for first creation
'Store the MarketID
Dim newMktId As String = tabMarkets.SelectedTab.Name
'This was the Position of the Tab prior to removal using the Current tab Index
Dim OldPageIndex As Integer = tabMarkets.TabPages.IndexOf(tabMarkets.SelectedTab)
nudTabIndex.Value = OldPageIndex 'This displays the Index in a Numeric Up Down Control
'Remove the Existing Tab
tabMarkets.TabPages.Remove(tabMarkets.SelectedTab)
'Load a New Tab and New Data Grid Oct 23rd
With tabMarkets
' Create New Tab and Select ready to apply new datagrid
Tab.Name = newMktId 'This was the Old market ID stored previously'
'Note Insert and NOT Add
.TabPages.Insert(OldPageIndex, Tab) 'Add this tab to the TabPages Collection
.SelectedTab = Tab 'Select the Tab page
'New Data Grid so the Book will Reload its values
objDataGrid = New DataGridView 'Use declared Data grid object and setup
Tab.Controls.Add(objDataGrid) 'Put a DataGridView/textbox on the Tab
.SelectedTab = Tab 'Reselect Tab Page
'Activate the New Datagrid
BuildDataGrid() '========Create the Data Grid==========



Comment