hey ya all....
i have a tab control in a win form app. and the tab control has 3 tabs. in every tab there is a DGV docked in a container's panel, here is the thing. up until now i used the containers click event to load data into the DGV's but for some reason i can not do it any more. i want to know is there a way to load or refresh (or do anything for that matter) when i switch to a certain tab in the tab control?
i just want the event's name and i need it bad.
thanks guys

Dani AI

Generated

As noted, the TabControl.SelectedIndexChanged event is the simplest place to refresh a DataGridView when a tab becomes active. SelectedIndexChanged fires whenever the TabControl.SelectedIndex changes (whether by a user click or in code) and the handler can check SelectedIndex or SelectedTab to decide which grid to load—this is the intended replacement for relying on a container's Click event, which only fires on mouse clicks inside that control and not on tab switches (the original approach used by ).

Other useful events and when to use them:

  • TabControl.Selected gives a TabControlEventArgs (new page and index) and is useful when the handler needs the TabPage object or action details.
  • TabControl.Selecting is cancelable (TabControlCancelEventArgs) and runs before the change.
  • TabPage.Enter fires when the page receives focus and can be convenient for initializing controls that need focus-based setup.

Practical pattern (lazy load + avoid UI freezes): handle SelectedIndexChanged, check which tab is active, and load data asynchronously. Example sketch:

private async void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    var page = tabControl1.SelectedTab;
    if (page == tabPage2)
    {
        dataGridView2.DataSource = null;
        var data = await Task.Run(() => LoadDataForTab2()); // heavy work off UI thread
        dataGridView2.DataSource = data;
    }
}

Troubleshooting tips: confirm the event handler is wired to the TabControl (not the panel), guard against repeated reloads with a simple loaded[] flag per tab, avoid long synchronous DB calls on the UI thread, and prefer Selected/Selecting when the handler needs the TabPage or must cancel the switch.

Recommended Answers

All 2 Replies

SelectedIndexChanged fires when you change tabs.

thanks, that is just what i needed :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.