2 big bai big_bai 于 2016.09.09 14:24 提问

求助,C#的窗体程序如何进行数据更新。

图片说明
图片说明
在小窗口添加数据后在大窗口的视图同步刷新数据怎么实现啊。。。

这个是小窗口的代码

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();
    }
}

}

2个回答

tina622
tina622   2016.09.09 16:53
已采纳

//在小窗体定义一个FrmStore
private FrmStore __frmStoreFrm = null;
//在构造方法中赋值
public FrmEditStore(FrmStore frm)
{
_frmStoreFrm = frm; //就可以在小窗体中用_frmStoreFrm调用大窗体中的public方法了
}
//大窗体中这样调用
FrmEditStore fes = new FrmEditStore(this);
fes.ShowDialog();

z3237109
z3237109   2016.09.09 16:54

代码先不看了,直接说思路吧:
第一种:大小窗口都是加载同一个数据库中的内容的话,当小窗口重新修改了数据之后,大窗口在接收到小窗口关闭的事件之后,重新加载数据
第二种:小窗口修改了数据之后,同时将修改的事件传递给大窗口,然后大窗口执行与小窗口修改数据一样的代码,将大窗口中的数据也按照同样的方式进行修改
第二种能比第一种节省资源,不用大窗口整个刷新数据了,不知道你的大小窗口show时是不是用的多线程,所以不敢轻易动你的代码,数据刷新的时候,多线程可能涉及到跨线程调用组件,一点点窗口生命周期的知识,具体还要看你自己啦~有问题可以继续讨论,吼吼~~~。

big_bai
big_bai 我直接改了个activated事件 里面写的重新加载的方法,小窗口进行操作以后 大窗口会受到一次激活,直接进行刷新数据了。。。
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片