hi, i have a problem with my code. i have 1 tab control and 3 tabs. now the problem is that when i switch from one tab to another i want the datagridviews to clear their selection. this code works but when one selects the tab for the second time. any help pls?

private void tabControl1_Selected(object sender, TabControlEventArgs e)
        {
            
            if (tabControl1.SelectedTab == tbStarter)
            {
                dgvLightMeal.ClearSelection();
            }
            if (tabControl1.SelectedTab == tbMainCourse)
            {
                dgvPasta.ClearSelection();
                dgvPizza.ClearSelection();
                dgvGrill.ClearSelection();
                dgvKids.ClearSelection();
            }
            
        }

Hi there,

I tried this out and replicated your issue. After much head scratching and googling I found an MS article about a known issue with DataGridView that it does not update if it is not visible. To get round the problem I made each tab visible before binding the datagrid to its source e.g.:

tabControl1.SelectedTab = tabControl1.TabPages[2];
            dataGridView2.DataSource = dt;
            dataGridView2.ClearSelection();

            tabControl1.SelectedTab = tabControl1.TabPages[1];
            dataGridView1.DataSource = dt2;
            dataGridView1.ClearSelection();

etc etc

The code you posted works fine just make the above changes in the Load method of your form. Also note that initially i updated the tabs in order and then made the first one visible again and the problem came back so as long as you update the tab you want to be initially visible to the user last you are ok.

Hope this helps.

i solved my problem mate thanks very much :)

Sometimes if you have other events that happen after load, clear selection doesn't appear to work. ie cell repaint event add a CurrentCell = Nothing in that event...

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.