是否可以创建一个datagridview并将其实例传递给C#中的winform中的每个TabPage? [英] Is it possible to create a datagridview and pass the instance of the same to every TabPage in winform in C#?

查看:128
本文介绍了是否可以创建一个datagridview并将其实例传递给C#中的winform中的每个TabPage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我想拥有相同的datagridview,比如我的TabControl的每个TabPage中的dgv。并且将在运行时创建tabpages。所以每当我开始一个新的标签页时,都会加载一个datagridview dgv。



是否可以在所有TabPages中使用相同控件的实例?

如果是,请建议方式......

谢谢:)

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 :)

推荐答案

如果真的应该是相同的 实例,我建议重新设计你的GUI(例如,在所有需要的上下文(页面)中显示的区域中显示DataGrid,与其他生成的TabPage内容无关)。



其他只是将您的实例分配给当前的TabPage,但您必须跟踪Tab-Controls页面更改,因为您必须传递单个实例arround(任何Control实例只能存在于一个容器控件中, - >不是我要实现的解决方案......
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...


是的,你可以在任何标签页上使用一个网格实例。



你可能会在运行时将新的tabpage添加到tabControl.TabPages集合中,然后切换tabcontrol.SelectedIndex以显示新添加的tabPage。



只需添加一个事件处理程序tabControl.SelectedIndexChanged。此示例将单个dataGridView实例移动到所选tabPage的Controls集合中。它将自动从上一个选项卡的Controls集合中删除。将重新应用dataGridView的Location或Dock值,使其看起来与每个选项卡上的位置完全相同。



如果需要保留使用的dataSources对于每个选项卡上的网格,同时让用户选择不同的选项卡,使用您根据需要添加或删除的列表,然后您也可以在此处交换dataGridView.DataSource。请记住,如果您使用更大的数据源,则可能会重新考虑持久性和/或处理更新延迟。在集合之间移动网格实例的时间是名义上的。



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.

// 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];
}


这篇关于是否可以创建一个datagridview并将其实例传递给C#中的winform中的每个TabPage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆