简体   繁体   English

C#Win。 表格-设置2个datagridviews相等时数据不反映

[英]C# Win. Form - Data not reflecting when 2 datagridviews are set equal

I have 2 forms (Form1, Form2) each having 1 datagridview with modifier Public and 1 button. 我有2个表单(Form1,Form2),每个表单都有1个带修饰符Public和1个按钮的datagridview The code written on the buttons are:- 按钮上写的代码是:-

Form2 f = new Form2();
f.Show();
f.datagridview1 = datagridview1;

Problem 问题

Now when i press the button of Form1 , Form2 appears but the values in datagridview1 of Form1 are not displayed in the datagridview1 of Form2 but when i programmatically check for the values in datagridview1 of Form2 , the values are there. 现在,当我按下Form1的按钮 ,窗体2显示,但在datagridview1 Form1的值在窗体2的datagridview1不显示,但是当我以编程方式datagridview1窗体2的检查值,这些值在那里。

Solutions tried 解决方案尝试

  1. datagridview1.Refresh(); datagridview1.Refresh(); - i tried to refresh the control of Form2 but no changes were seen. -我尝试刷新Form2的控件,但未看到任何更改。
  2. f.Refresh(); f.Refresh(); - i even tried to update the whole Form2 but still no changes were seen -我什至尝试更新整个Form2,但仍未看到任何更改

Note :- I know i instead of writing f.datagridview1 = datagridview1; 注意 :-我知道我不是写f.datagridview1 = datagridview1; i can write a loop to populate the datagridview but i need to know what is the problem with the above code. 我可以编写一个循环来填充datagridview,但是我需要知道上面的代码有什么问题。

Thanks in advance 提前致谢

You shouldn't override the reference to the dataGridView (which means that you will access the dataGridView from Form1, even if you access f.dataGridView1 from Form2), but rather set the DataSource from both dataGridViews to the same object. 您不应覆盖对dataGridView的引用(这意味着即使您从Form2访问f.dataGridView1,也要从Form1访问dataGridView),而应将两个dataGridViews的DataSource设置为同一对象。

So the error in your code can be made visible if you add the following line in your code: 因此,如果在代码中添加以下行,则可以使代码中的错误可见:

Form2 f = new Form2();
f.Show();
f.datagridview1 = datagridview1;
f.datagridview1.DataSource = null;

You will see that, all of a sudden, your datagridview in Form1 will be empty, because datagridview1 AND f.datagridview1 will point to the same datagridview in Form1. 您会突然发现,Form1中的datagridview将为空,因为datagridview1f.datagridview1将指向Form1中的同一datagridview。 Also you can no longer access the datagridview from From2 (at least not that easy). 另外,您将无法再从From2访问datagridview(至少不是那么容易)。

So maybe try this instead: 因此,也许尝试以下方法:

f.datagridview1.DataSource = datagridview1.DataSource

This code will just set both DataSources (which contain the actual data) to the same object in both datagridviews. 这段代码只会将两个DataSource(包含实际数据)设置为两个datagridviews中的同一对象。

I hope this explanation is somewhat understandable : ) 我希望这个解释可以理解:)

You could use ResetBindings on the BindingSource object. 您可以在BindingSource对象上使用ResetBindings。 Like that: 像那样:

//could be simpler assignment, but to give you a view on what is going on
BindingSource source = new BindingSource();
source.DataSource = f.datagridview1.DataSource;
source.ResetBindings(false);

Let me know if that works for you 让我知道这是否适合您

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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