积极答复者
请教为何DataGridView未刷新

问题
-
如以下代码所示,之前两个方法在同一个窗体类中,DataGridView是OK的。
后来我将其修改为两个窗体时,DataGridView未刷新(查询结果是正确的),请问是哪里的问题?要如何修改呢?
//修改前 Class Form1 { UpdateDgv() { //... Query(string sql); } Query(string sql) { //... dgv.DataSource = dataTable; } } //修改后 Class Form1 { Query(string sql) { //... dgv.DataSource = dataTable; } } Class Form2 { Form1 f1 = new Form1(); //... f1.Query(string sql);//查询结果OK,但dgv绑定dataTable从界面看未刷新 }
答案
-
Hi leon,
如果你想在两个窗体之间进行交互,那么你这种单纯的弹出一个新窗体然后调用旧窗体中的方法是不可行的,因为这样做的话其实两个窗体之间并没有产生联系,所以在你建一个新窗体的时候,需要把旧窗体传递过去:
Form1中的代码:
private void button1_Click(object sender, EventArgs e) { var f2 = new Form2(this); f2.Show(); }
Form2中的代码:
public partial class Form2 : Form { Form1 frm1; public Form2(Form1 f1) { InitializeComponent(); frm1 = f1; } private void button1_Click(object sender, EventArgs e) { frm1.UpdateDataDGV();//我需要执行此方法后Form1界面的DataGridView相应更新 //this.Close(); } }
Regards,
Stanly
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已建议为答案 Hart WangModerator 2017年11月9日 6:19
- 已标记为答案 leon1526 2017年11月9日 10:28
全部回复
-
Hi,
感谢你在MSDN论坛发帖。
你有执行Refresh() 这个函数吗?
如果你能提供一个可以重现问题的demo 到 onedrive 上面,这样方便大家测试问题,然后找到解决方案。
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已编辑 Hart WangModerator 2017年11月6日 7:42
- 已标记为答案 leon1526 2017年11月6日 7:52
- 取消答案标记 leon1526 2017年11月6日 7:52
-
Hi,
我没能找到,你提供的one drive 的链接?
Best Regards,
Hart
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
Hi Hart,
多谢你的回复,我把Demo拖到OneDrive并设置了共享。我以前从未用过OneDrive,因此可能是哪里设置有误。
那个WinForm程序是我下班做的,所以现在我重新做了一个码可以重现这个问题。
//Form1中的代码 private DataTable dataTable = new DataTable(); private void Form1_Load(object sender, EventArgs e) { dataTable.Columns.Add("A"); dataTable.Columns.Add("B"); DataRow dataRow; dataRow = dataTable.NewRow(); dataRow["A"] = "1"; dataRow["B"] = "2"; dataTable.Rows.Add(dataRow); this.dataGridView1.DataSource = dataTable; } public void UpdateDataDGV() { dataTable.Columns.Add("C"); dataTable.Columns.Add("D"); DataRow dataRow; dataRow = dataTable.NewRow(); dataRow["C"] = "3"; dataRow["D"] = "4"; dataTable.Rows.Add(dataRow); this.dataGridView1.DataSource = dataTable; this.dataGridView1.Refresh(); } private void button2_Click(object sender, EventArgs e) { var f2 = new Form2(); f2.Show(); } //Form2中的代码 private void button1_Click(object sender, EventArgs e) { var f1 =new Form1(); f1.UpdateDataDGV();//我需要执行此方法后Form1界面的DataGridView相应更新 this.Close(); }
-
Hi leon,
如果你想在两个窗体之间进行交互,那么你这种单纯的弹出一个新窗体然后调用旧窗体中的方法是不可行的,因为这样做的话其实两个窗体之间并没有产生联系,所以在你建一个新窗体的时候,需要把旧窗体传递过去:
Form1中的代码:
private void button1_Click(object sender, EventArgs e) { var f2 = new Form2(this); f2.Show(); }
Form2中的代码:
public partial class Form2 : Form { Form1 frm1; public Form2(Form1 f1) { InitializeComponent(); frm1 = f1; } private void button1_Click(object sender, EventArgs e) { frm1.UpdateDataDGV();//我需要执行此方法后Form1界面的DataGridView相应更新 //this.Close(); } }
Regards,
Stanly
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.- 已建议为答案 Hart WangModerator 2017年11月9日 6:19
- 已标记为答案 leon1526 2017年11月9日 10:28