big_bai 2016-09-09 06:24 采纳率: 100%
浏览 3414
已采纳

求助,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条回答 默认 最新

  • 猪头三号 2016-09-09 08:53
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果