我的设计是父窗体只做展示,子窗体做更新。
一开始尝试直接调子窗体的刷新,无效;
后来用了委托遂解决!!
主要代码就是在子类声明委托,在子类调用事务刷新父类
父窗体设计:
点修改跳转子窗体:
详细代码我放下面了,上传代码是有好处的,会让自己发现bug。
父窗体代码:
using DataBase;
using NLog;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Xml;
using WcsTaskManager.Common;
using WcsTaskManager.DbBll;
namespace WcsTaskManager
{
public partial class AreaForm : Form
{
private WcsDatabase _wcstask;
List<DeviceStation> list = new List<DeviceStation>() { };
public AreaForm()
{
InitializeComponent();
DgvStationList.DataBindingComplete += new DataGridViewBindingCompleteEventHandler(dataGridViewStationList_DataBindingComplete);
}
#region 映射关系的增删改查
/// <summary>
/// 加载数据
/// </summary>
public void LoadData()
{
try
{
_wcstask = new WcsDatabase();
list = _wcstask.GetStationStatus().ToList();
DgvStationList.DataSource = list;
DgvStationList.Refresh();
}
catch (Exception ex)
{
Logger.Info(ex.Message);
}
}
/// <summary>
///编辑按钮
/// </summary>
private void dataGridViewStationList_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
var aaa = (sender as DataGridView);
if (aaa == null) return;
if (e.RowIndex >= 0)
{
if (list.Count > 0)
{
for (int i = 0; i < list.Count; i++)
{
if (i == e.RowIndex)
{
if (aaa.Columns[e.ColumnIndex].HeaderText.Equals("编辑"))
{
string stationid = list[i].stationId;
LocationLock state = list[i].state;
StationWareType stationtype = list[i].stationWareType;
UpdateStateForm addForm = new UpdateStateForm(stationid, state, stationtype, this);
addForm.raiseCallBackRefreshEvent += new raiseCallBackRefreshDelegate(childfrm_raiseCallBackRefreshEvent); // 注册回调事件,也即将子窗体抛出的事件接收,然后处理
addForm.Show();
}
}
}
}
}
}
private void childfrm_raiseCallBackRefreshEvent()
{
LoadData(); // 刷新主窗体
}
}
}
子窗体类
using DataBase;
using Newtonsoft.Json;
using NLog;
using System;
using System.Data;
using System.Windows.Forms;
using WcsTaskManager.DbBll;
namespace WcsTaskManager
{
public delegate void raiseCallBackRefreshDelegate(); // 全局委托申明
public partial class UpdateStateForm : Form
{
public event raiseCallBackRefreshDelegate raiseCallBackRefreshEvent; // 公开的事件
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
private string _stationId = "";
private LocationLock _state;
private StationWareType _stationWaretype;
private AreaForm _form;
public UpdateStateForm(string agvId, LocationLock state, StationWareType stationWaretype, AreaForm form)
{
_form = form;
InitializeComponent();
if (agvId != "")
LoadData(agvId, state, stationWaretype);
}
public void LoadData(string stationId, LocationLock state, StationWareType stationWaretype)
{
try
{
_stationId = stationId;
_state = state;
_stationWaretype = stationWaretype;
}
catch (Exception exception)
{
Logger.Info(exception.Message);
}
}
/// <summary>
/// 有货
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
bool flag = false;
if (_stationId != "")
{
flag = UpdateData(_stationId);
}
if (flag)
{
MessageBox.Show(this, "更新成功");
_form = new AreaForm();
_form.LoadData();
raiseCallBackRefreshEvent();
}
else
MessageBox.Show(this, "更新失败");
this.Close();
}
/// <summary>
/// 更新方法
/// </summary>
/// <param name="stationId"></param>
/// <returns></returns>
private bool UpdateData(string stationId)
{
bool flag = false;
string msg = String.Empty;
string isCheck = string.Empty;
try
{
WcsDatabase wcs = new WcsDatabase();
string para = "{stationId:'" + stationId + "',stationWareType:'SkuOne',state:'" + _state + "}";
flag = wcs.updateState(para);
return flag;
}
catch (Exception ex)
{
Logger.Info(ex.Message);
}
return flag;
}
/// <summary>
/// 无货
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnNone_Click(object sender, EventArgs e)
{
bool flag = false;
try
{
if (_stationId != "")
{
WcsDatabase wcs = new WcsDatabase();
string para = "{stationId:'" + _stationId + "',stationWareType:'None',state:'" + _state + "'}";
flag = wcs.updateState(para);
if (flag)
{
MessageBox.Show(this, "更新成功");
_form = new AreaForm();
_form.LoadData();
raiseCallBackRefreshEvent();
}
else
MessageBox.Show(this, "更新失败");
this.Close();
}
}
catch (Exception ex)
{
Logger.Info(ex.Message);
}
}
/// <summary>
/// 取货占用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
bool flag = false;
if (_stationId != "")
{
WcsDatabase wcs = new WcsDatabase();
string para = "{stationId:'" + _stationId + "',state:'PickUpLock',stationWareType:'" + _stationWaretype + "'}";
// DeviceStation json = JsonConvert.DeserializeObject<DeviceStation>(para);
flag = wcs.updateState(para);
if (flag)
{
MessageBox.Show(this, "更新成功");
_form = new AreaForm();
_form.LoadData();
raiseCallBackRefreshEvent();
}
else
MessageBox.Show(this, "更新失败");
this.Close();
}
}
/// <summary>
/// 放货占用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
bool flag = false;
if (_stationId != "")
{
WcsDatabase wcs = new WcsDatabase();
string para = "{stationId:'" + _stationId + "',state:'ReleaseLock',stationWareType:'" + _stationWaretype + "'}";
// DeviceStation json = JsonConvert.DeserializeObject<DeviceStation>(para);
flag = wcs.updateState(para);
if (flag)
{
MessageBox.Show(this, "更新成功");
_form = new AreaForm();
_form.LoadData();
raiseCallBackRefreshEvent();
}
else
MessageBox.Show(this, "更新失败");
this.Close();
}
}
/// <summary>
/// 清除占用
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_clearPickLock_Click(object sender, EventArgs e)
{
bool flag = false;
if (_stationId != "")
{
WcsDatabase wcs = new WcsDatabase();
string para = "{stationId:'" + _stationId + "',state:'NoLock',stationWareType:'" + _stationWaretype + "'}";
// DeviceStation json = JsonConvert.DeserializeObject<DeviceStation>(para);
flag = wcs.updateState(para);
if (flag)
{
MessageBox.Show(this, "更新成功");
_form = new AreaForm();
_form.LoadData();
raiseCallBackRefreshEvent();
}
else
MessageBox.Show(this, "更新失败");
this.Close();
}
}
}
}
我是把父子窗口分开了,本功能如果做在一个界面也没这么麻烦
记录一下吧,脑子不好用