日期:2014-05-19  浏览次数:20767 次

winform程序,在一窗体更新数据在另一窗体中用DataGrid控件显示更新数据的问题,急急急
第一个窗体中用DataGrid控件显示数据库的内容,在此窗体中通过菜单打开第二个窗体,并在其中修改数据库里的内容,并且更新数据库(此时直接进入数据库中可看到数据库确实已经更新),而第一个窗体中仍然显示数据库中原来的数据,(上边所说的数据库指同一数据库),请问如何绑定才能使DataGrid控件里的数据同步更新?如果检查数所行state状态该如何检查?谢谢谢谢.我急着等用啊.

------解决方案--------------------
你在A窗口(有DATAGRID)的窗口写个可以更新DATAGRID的函数,然后B窗口修改数据库之后调用这个方法不就好了
------解决方案--------------------
如果想使用同步刷新的话 还可以用一个计时器定时刷新
------解决方案--------------------
要用到设计模式
------解决方案--------------------
在 第一个窗体中ENTER事件中重新给DataGrid赋值
不知道是不是这样可以符合你的要求
如果同步更新,就只有用定时器了!

------解决方案--------------------
你必须保证你打开的窗体不是模式窗体
你在修改数据库的时候让他触发一个事件,在第一个窗体去用就可以了的撒
------解决方案--------------------
用委托吧:
1:定义一个委托的类:
  using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace EIP
{
public delegate void DelegateEvent(string ttt);

}
==============
2:窗体2中代码
public event DelegateEvent refreshData;//生明一个委托的事件

// 当你在数据保存后窗体2数据刷新...
void btn_click()
{
string tt = "ttt ";
this.refreshMethod(tt);
}

3:================================在你想要刷新的窗体的动作
form2 frm = new form2();
frm.refreshMethod+=new DeletegateTestMethod(refreshMethod);
frm.ShowDialog();

........
void refreshMethod(string ttt)
{
.....
可以根据窗体2中传来的参数做一些动作...
}



------解决方案--------------------
//在另一个窗口中修改当前选定的DataGrid的行

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
//主窗体代码
namespace Zhzuo
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private DataSet ds;
private System.Windows.Forms.Button button1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
ds = new DataSet( "MyDataSet ");
InitData(ds);
this.dataGrid1.DataSource = this.ds;
this.dataGrid1.DataMember = this.ds.Tables[0].TableName;
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = " ";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;