【问题标题】:C# Display the same DataGridView in two different TabsC#在两个不同的选项卡中显示相同的DataGridView
【发布时间】:2017-03-21 10:58:38
【问题描述】:

是否可以在两个不同的选项卡中显示完全相同的 DataGridView?我可以创建两个不同的 DataGridView,并在它们更改为具有相同数据时使用一堆方法来更新它们,但我宁愿只有一个 DataGridView 并在两个地方都显示它。

【问题讨论】:

  • tabPage1.Controls.Add(dataGridView)tabPage2.Controls.Add(dataGridView)?
  • 请注意,这是移动控件。一个可能的解决方案,但应该注意!您可以在选择 Tab 或更改 TabPages 时来回移动它;这里的问题很不清楚!

标签: c# winforms


【解决方案1】:

您可以将它们的.DataSource 属性设置为相同的BindingSource 对象,而不是处理复制DataGridView 控件。 When the tab selected index changed, you'll be able to refresh the controls.由于它们具有相同的绑定源,因此它们将始终包含相同的数据。示例:

var bs = new BindingSource();
// populate bs object
dataGridView1.DataSource = bs;
dataGridView2.DataSource = bs;

【讨论】:

  • 我碰巧有一个对象列表,我可以用这个列表作为 BindingSource 吗?
  • 如果您愿意,可以使用您的对象列表作为数据源。或者,您可以转换为数据表,但无论哪种方式都可以。
  • 如何填充 BS 对象?我试过这样做:tableDataBS = new BindingSource() { DataSource = productList };,但似乎对象是空的/null。 (我确定productList 不为空。)
【解决方案2】:

尝试为两个 DataGridView 使用一个 DataSource 或 Adapter。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-18
    • 2021-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多