在小窗口添加数据后在大窗口的视图同步刷新数据怎么实现啊。。。
这个是小窗口的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SupperMarket
{
public partial class FrmEditStore : Form
{
DataSet ds;
SqlDataAdapter sda;
public FrmEditStore()
{
InitializeComponent();
}
private void FrmEditStore_Load(object sender, EventArgs e)
{
GetStoreClass();
this.cboStoreClass.DataSource = ds.Tables["CommoditySort"];
this.cboStoreClass.ValueMember = "SortID";
this.cboStoreClass.DisplayMember = "SortName";
}
public void GetStoreClass()
{
string sql = "select * from CommoditySort";
DBHelper db = new DBHelper();
try
{
ds = new DataSet();
sda = new SqlDataAdapter(sql, db.Connection);
sda.Fill(ds, "CommoditySort");
}
catch (Exception)
{
MessageBox.Show("系统错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
//是否为特价的控制
private void cbIs_CheckedChanged(object sender, EventArgs e)
{
if (this.cbIs.Checked == false)
{
this.nudPriceOf.Enabled = false;//是否禁用用控件
this.nudPriceOf.ReadOnly = true;//是否为只读状态
}
else
{
this.nudPriceOf.Enabled = true;
this.nudPriceOf.ReadOnly = false;
}
}
private void btnSave_Click(object sender, EventArgs e)
{
if (this.txtStoreName.Text.Trim().Length != 0 || this.nudPrice.Value != 0)
{
int num = SaveStore();
if (num > 0)
{
MessageBox.Show("商品添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (num == 0)
{
MessageBox.Show("商品添加失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("系统错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
else
{
MessageBox.Show("请填写商品信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
public int SaveStore()
{
int num;
int flag = 0;
if (this.cbIs.Checked == false)
{
num = 0;
}
else
{
num = 1;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("insert commodity(CommodityName,SortID,CommodityPrice,IsDiscount,ReducedPrice)");
sb.AppendFormat(" values('{0}',{1},{2},'{3}',{4})", this.txtStoreName.Text, this.cboStoreClass.SelectedValue, this.nudPrice.Value, num, this.nudPriceOf.Value);
DBHelper db = new DBHelper();
try
{
db.OpenConnection();
SqlCommand com = new SqlCommand(sb.ToString(), db.Connection);
flag = com.ExecuteNonQuery();
}
catch (Exception ex)
{
flag = -1;
}
finally
{
db.CloseConnection();
}
return flag;
}
private void btnClean_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
大窗口的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SupperMarket
{
public partial class FrmStore : Form
{
DataSet ds;
SqlDataAdapter sda;
public FrmStore()
{
InitializeComponent();
}
public void GetStore()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("select c.CommodityID,c.CommodityName,cs.SortName,c.CommodityPrice,c.IsDiscount,c.ReducedPrice");
sb.AppendLine(" from commodity c");
sb.AppendLine(" join CommoditySort cs on cs.SortID = c.SortID");
DBHelper db = new DBHelper();
try
{
ds = new DataSet();
sda = new SqlDataAdapter(sb.ToString(), db.Connection);
sda.Fill(ds, "commodity");
}
catch (Exception ex)
{
MessageBox.Show("系统错误!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void FrmStore_Load(object sender, EventArgs e)
{
GetStore();
this.dgvStoreDialog.AutoGenerateColumns = false;
DataView dv = new DataView(ds.Tables["commodity"]);
this.dgvStoreDialog.DataSource = dv;
}
private void tsbExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void tvStoreClass_AfterSelect(object sender, TreeViewEventArgs e)
{
int num = this.tvStoreClass.SelectedNode.Level;
string choice = this.tvStoreClass.SelectedNode.Text;
this.dgvStoreDialog.AutoGenerateColumns = false;
DataView dv = new DataView(ds.Tables["commodity"]);
int flag = choice.Equals("特价商品") ? 1 : 0;
if (num == 1)
{
dv.RowFilter = string.Format("IsDiscount = {0}", flag);
}
this.dgvStoreDialog.DataSource = dv;
}
private void tsbAdd_Click(object sender, EventArgs e)
{
FrmEditStore fes = new FrmEditStore();
fes.ShowDialog();
}
}
}