0

I am pragmatically creating TabPage with DataGridView. The TabPage's are appearing correctly, but they are missing the DataGridView. Anyone able to discern why they DGV's are not appearing with the tabs? The LoadDataGridToTab is in the correct form. It is being called from another Form though. Should?

    public void LoadDataGridToTab(Main main, string category)
    {
        try
        {
            //Set Cell Style
            var dataGridViewCellStyle = new DataGridViewCellStyle
            {
                Alignment = DataGridViewContentAlignment.MiddleLeft,
                BackColor = SystemColors.Control,
                Font = new Font("Microsoft Sans Serif", 8.25F,
                    FontStyle.Regular, GraphicsUnit.Point, 0),
                ForeColor = SystemColors.WindowText,
                SelectionBackColor = SystemColors.Highlight,
                SelectionForeColor = SystemColors.HighlightText,
                WrapMode = DataGridViewTriState.True
            };

            //Make the Grid
            var grid = new DataGridView
            {
                Name = "dgv_" + category,
                Text = category,
                Visible = true,
                Dock = DockStyle.Fill,
                AllowUserToAddRows = false,
                AllowUserToDeleteRows = false,
                AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
                ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize,
                ColumnHeadersDefaultCellStyle = dataGridViewCellStyle,
                DataSource = Auction.CacheAuctionsDataSet.Tables[category]
            };

            //Binding
            //var source = new BindingSource();
            //source.DataSource = CacheAuctionsDataSet.Tables[category];

            //Made the Tab
            var tmpTabPage = new TabPage(category)
            {
                Name = "tabctrl_" + category,
                Text = category,
                Visible = true
            };

            //Add the Grid to the Tab
            tmpTabPage.Controls.Add(grid);
            tmpTabPage.Refresh();

            //Add the Tab to the Control
            tabctrl_Auctions.Controls.Add(tmpTabPage);
            tabctrl_Auctions.Refresh();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }

    }
  • 1
    Are you calling this function from a new thread? – JohnKiller Oct 11 '14 at 09:44
  • This is getting called from a non-ui thread. I was thinking I might need to methodinvoke? –  Oct 11 '14 at 16:03
  • 1
    yep. call invoke on the parent form – JohnKiller Oct 11 '14 at 16:04
  • I talk a better game than I can perform evidently. Would you happen to have a stackoverflow reference on how to do that? I have been doing my best to make it happen, but failing miserably atm. –  Oct 11 '14 at 17:05

1 Answers1

0

You need to use the Invoke method.

An example is found here https://stackoverflow.com/a/253150/2633161

Community
  • 1
  • 1
JohnKiller
  • 2,008
  • 18
  • 28