Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to have a same datagridview ,say dgv in every TabPage of my TabControl. and the tabpages will be created at run-time. So whenever i start a new tab page, a datagridview dgv is loaded.

Is it possible to have the instances of same control be used in all TabPages?
If yes, please suggest ways...
Thanks :)
Posted

If it should be really the "SAME" instance, I would suggest to "redesign" your GUI (e.g. Show the DataGrid in an "area" visible in all needed contexts (pages) independend from the rest of the generated TabPage Contents).

Other from that just assign your instance to the current TabPage, but you have to track Tab-Controls page changes cause you have to pass your single instance "arround" (any Control instance can only live in one container-control), -> Not a solution I would implement...
 
Share this answer
 
Comments
sovit agarwal 5-Jun-14 7:21am    
My situation is such that i need to create multiple tabpages at runtime. Each tabPage must have 3 controls (text box, button and datagridview).The datagridview is populated again at runtime from the location entered in the textbox.
So what do you suggest for this now?
johannesnestler 5-Jun-14 7:44am    
now I'm confused - so is it the same "instance"/"reference" or just of the same "type"?
sovit agarwal 6-Jun-14 3:11am    
different instances of the same type
Yes, you can use one grid instance on any tab page.

You will probably add the new tabpage in runtime to the tabControl.TabPages collection, then switch the tabcontrol.SelectedIndex to display the newly added tabPage.

Just add an event handler for the tabControl.SelectedIndexChanged. This sample moves the single dataGridView instance into the Controls collection of the selected tabPage. It will automatically be removed from the Controls collection of the previous tab. The Location or Dock values for the dataGridView will be reapplied so that it appears to be in exactly the same place on each tab.

If you need to persist the dataSources used for the grid on each tab while letting the user choose different tabs, use a list that you add to or remove as needed, then you can swap the dataGridView.DataSource here too. Keep in mind that if you use larger datasources you might reconsider persistence and/or handling update delays. The time to 'move' the grid instance between collections is nominal.

C#
// Assumed to contain BindingList items indexed to match tabs
List<BindingList<SomeBindableClass>> _bindingLists = new 
   List<BindingList<SomeBindableClass>>();

private void onTabControlSelectedIndexChanged(object sender, EventArgs e)
{
    TabControl tControl = (TabControl)sender;
    tControl.SelectedTab.Controls.Add(this.dataGridView1);

    this.dataGridView1.DataSource = _bindingLists[tControl.SelectedIndex];
}
 
Share this answer
 
Comments
sovit agarwal 6-Jun-14 3:09am    
Thanks Pete!!!
One question here...My datagridview will have different values in all the tabs which will be loaded from different file locations.
Can this be accounted for?
Pete_H 6-Jun-14 9:32am    
The sample syncs the grid's datasource with the selected tab. It's a good approach if you pre-loaded your data sources or if fetching data is fast. It also assumes one definition of a record type (SomeBindableClass) in multiple sources

If you loaded the data after an action is performed on a tab, like a button-click, its just as well to store the data source with the tab using the tabPage.Tag property.

If 'different values' also means different record types and columns that are configured in runtime, you could just store the column collection with the datasource in a KeyValuePair in tabPage.Tag. Honestly though, at that point I would probably abandon using only one dataGridView for creating a new instance on each tab, which can also be done at runtime.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900