I am working on a Windows Forms application. I have two tab2 in my window form. In each tab, I build a datagridview which I named it dgvRound. I loaded two different set of datas to the two datagridview in these two tabs. When I switch from tab1 to tab2, I still see the datagridview data on the tab1. How do I change it (currency?) to see the dgvRound in tab2 which I update the datagridview with new data?
// tabView is my main tab page control
// Add a new tab for a new round
string title = "Round " + Convert.ToString(intRound);
TabPage myTabPage = new TabPage(title);
tabView.TabPages.Add(myTabPage);
// set up datagridview box
DataGridView dgvRound = new DataGridView();
myTabPage.Controls.Add(dgvRound);
// set up the datagridview column and row headings
SetupTableView(dgvRound);
//load datagridview in a loop with different data from different dataset
string[] row1 = new string[] { "array of datas" };
dgvRound.Rows.Add(row1);
I called the above code to create the tab and dgv multiple times depending on some variables. The data in each datagrid in each tab are ok. But when I try to pull the data from datagridview (dgvRound) from tab2, i see the data in tab1 not tab2. For example:
dgvRound.Rows[0].Cells[“Game 1”].Value. How do I tell it look at tab2.dgvRound.Rows[0].Cells[“Game 1”].Value?
Thanks for your help
Daniel
Thanks HotSoft, I finally found the code to search for the current datagridview in a dynamically created tab page. I added this and it is working now.
TabPage CurrentTabPage = tabView.SelectedTab;
// Get all the controls here
Control.ControlCollection col = CurrentTabPage.Controls;
// should have only one dgv
foreach (Control myControl in col)
{
if (myControl.ToString() == “System.Windows.Forms.DataGridView”)
{
DataGridView tempdgv = (DataGridView)myControl;
tempdgv.SelectAll();
dgvRound = tempdgv;
}
}
Thanks again for your help…
Given that I cannot see you code dynamically creating the DataGridView on each Tab
Looks like you are showing the same DataGrid on both tabs with out reloading or re-binding the different datasource for datagrid’s on different tabs
Update on OP’s comment:
You can get the DataGridView from the tabpage like this