///刷新父页面中的datagrid你得datagrid肯定应该有个数据源,你实际要做的工作就是刷新数据源//能在一个页面中调用另一个页面的组件嘛?当然可以了,假设a窗口是父,b窗口是子1、在b窗口定义中定义public A w_parent; 2、在a窗口打开b窗口的代码中 B w_b =new B(); w_b.w_parent = this;
有两种方法: 1。将form中绑定datagrid的方法设成public类型,每次修改之后重新调用该方法 2。写一个消息类,其实也就是自己写的继承自System.EventArgs的类 当改变数据后发送消息,datagrid所在界面接收到消息,重载数据,就可以了 以下是消息类,方法写在类的最上面的注释上 using System;namespace Vavic { /// <summary> /// 当信息更改是通知其他的窗口重新加载数据 /// 使用方法为:1)通知信息更改 /// Notify.Instance().SendMessage(100, "Form1 Activate test notify event"); /// 其中第一个参数为信息号,第二个参数为信息描述 /// 2)收取信息 /// 使用方法,在每个FORMLOAD事件中加入如下语句 /// Notify.Instance().OnNotifyEvent += new Notify.NotifyEvent(OnNotifyEvent); /// 同时编写如下的方法用于重新加载数据 ///protected void OnNotifyEvent(object sender, Notify.NotifyEventArgs e) ///{ /// if (e.Code == Notify.CompanyInfo) /// System.Diagnostics.Debug.WriteLine(string.Format(" Form1 Code={0}, Message = {1}", e.Code,e.Message)); ///} /// </summary> public class Notify { protected static Notify m_notify = null; public delegate void NotifyEvent(object sender, NotifyEventArgs e); public event NotifyEvent OnNotifyEvent; protected Notify() { // // TODO: 在此处添加构造函数逻辑 // } public static Notify Instance() { if (m_notify == null) m_notify = new Notify(); return m_notify; }
public void SendMessage(int code, string message) { NotifyEventArgs e = new NotifyEventArgs(code, message); if ( OnNotifyEvent != null) OnNotifyEvent(this, e); } public class NotifyEventArgs : System.EventArgs { public NotifyEventArgs(int code, string message) { m_nCode = code; m_strMessage = message; } public NotifyEventArgs() { } protected int m_nCode; public int Code { get { return m_nCode;} set { m_nCode = value;} } protected string m_strMessage = string.Empty; public string Message { get { return m_strMessage;} set { m_strMessage = value; if (m_strMessage == null) m_strMessage = string.Empty; } } } #region Common Infomation public const int CompanyInfo = 1; public const int ProjectInfo = 2; public const int TaskInfo = 3; public const int Opportunity = 4; public const int Contactor = 5; public const int EmployeeInfo = 6; public const int DepartmentInfo = 7; public const int AdvCityInfo = 8; public const int ProductInfo=9; #endregion } }
2、在a窗口打开b窗口的代码中
B w_b =new B();
w_b.w_parent = this;
1。将form中绑定datagrid的方法设成public类型,每次修改之后重新调用该方法
2。写一个消息类,其实也就是自己写的继承自System.EventArgs的类
当改变数据后发送消息,datagrid所在界面接收到消息,重载数据,就可以了
以下是消息类,方法写在类的最上面的注释上
using System;namespace Vavic
{
/// <summary>
/// 当信息更改是通知其他的窗口重新加载数据
/// 使用方法为:1)通知信息更改
/// Notify.Instance().SendMessage(100, "Form1 Activate test notify event");
/// 其中第一个参数为信息号,第二个参数为信息描述
/// 2)收取信息
/// 使用方法,在每个FORMLOAD事件中加入如下语句
/// Notify.Instance().OnNotifyEvent += new Notify.NotifyEvent(OnNotifyEvent);
/// 同时编写如下的方法用于重新加载数据
///protected void OnNotifyEvent(object sender, Notify.NotifyEventArgs e)
///{
/// if (e.Code == Notify.CompanyInfo)
/// System.Diagnostics.Debug.WriteLine(string.Format(" Form1 Code={0}, Message = {1}", e.Code,e.Message));
///}
/// </summary>
public class Notify
{
protected static Notify m_notify = null;
public delegate void NotifyEvent(object sender, NotifyEventArgs e);
public event NotifyEvent OnNotifyEvent; protected Notify()
{
//
// TODO: 在此处添加构造函数逻辑
//
} public static Notify Instance()
{
if (m_notify == null)
m_notify = new Notify();
return m_notify;
}
public void SendMessage(int code, string message)
{
NotifyEventArgs e = new NotifyEventArgs(code, message);
if ( OnNotifyEvent != null)
OnNotifyEvent(this, e);
}
public class NotifyEventArgs : System.EventArgs
{
public NotifyEventArgs(int code, string message)
{
m_nCode = code;
m_strMessage = message;
} public NotifyEventArgs()
{
} protected int m_nCode;
public int Code
{
get { return m_nCode;}
set { m_nCode = value;}
} protected string m_strMessage = string.Empty;
public string Message
{
get { return m_strMessage;}
set
{
m_strMessage = value;
if (m_strMessage == null)
m_strMessage = string.Empty;
}
}
} #region Common Infomation
public const int CompanyInfo = 1;
public const int ProjectInfo = 2;
public const int TaskInfo = 3;
public const int Opportunity = 4;
public const int Contactor = 5;
public const int EmployeeInfo = 6;
public const int DepartmentInfo = 7;
public const int AdvCityInfo = 8;
public const int ProductInfo=9;
#endregion
}
}