简体   繁体   中英

tab control with datagridview is not working in winforms

I have a form 1 with 2 datagrid view controls and tab control with two tabs.

When I click on the datagridview cell I want to load something into two tabs.

First Tab

I want to display the selected datagridview row values in text boxes in first tab.... this was working fine....

Second tab

I want to populate the other datagridview in this tab depending on the selected row value (cell [0] value) in main the datagridview in form

But this is not working.

This is what I have done so far...

private void dgvCorporatedetails_CellClick(object sender, DataGridViewCellEventArgs e)
{
        textboxreadonly(false);
        btnAdd.Enabled = false;

        if (e.RowIndex >= 0)
        {
            int.TryParse(dgvCorporatedetails.Rows[e.RowIndex].Cells[0].Value.ToString(), out corporateid);
            if (corporateid > 0 && tccorporates.SelectedTab == tpDetails)
            {
                getselectedrecord(corporateid);

            }
            if (corporateid > 0 && tccorporates.SelectedTab == tpmembers)
            {

                Getmembersdetails(corporateid);

            }

        }

    }

It does not enter into this condition if (corporateid > 0 && tccorporates.SelectedTab == tpmembers)

even if I click on the datagridview cell and then I select the tab2(tpmembers) datagridview does not load in this tab page (tpmembers)

would any one pls help on this...

Place a breakpoint on that line then. Is corporateid greater than 0? Is the SelectedTab set to tpmembers? If your first if statement runs, indicating that the currently selected tab is tpDetails, then the second if statement will not run. Your if statements, as written, are mutually exclusive.

The way you've got it now, if the user is on tab1 and selects a row in the grid, then only tab1 will show data regarding the selected row. If they want to view the data in tab2, they have to click on your grid again to load data into that tab. How about just loading all the data into both tabs at once, regardless of which one is currently selected?

int corporateid;
if (e.RowIndex >= 0 && int.TryParse(dgvCorporatedetails.Rows[e.RowIndex].Cells[0].Value.ToString(), out corporateid))
{
    if (corporateid > 0)
    {
        getselectedrecord(corporateid);  // load data into tpdetails tab
        Getmembersdetails(corporateid);  // load data into tpmembers tab
    }
}

Just omit the tccorporates.SelectedTab == tpDetails and tccorporates.SelectedTab == tpmembers statements from your if blocks. When the user clicks on tab2, everything will already be loaded for them.

Try to implement the SelectedIndexChanged event for the tabcontrol. So when you switch between the tab pages, update the respective controls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM