GZFramwork数据库层《三》普通主从表增删改查

运行结果:

201503211426922892550d1d8c7e622image

使用代码生成器(GZCodeGenerate)生成tb_Cusomer和tb_CusomerDetail的Model

生成方式见第一节:

GZFramwork数据库层《一》普通表增删改查

  生成明细表ORM略有不同:

image

项目附加结果:

image

新增一个自定义控件:ucTableMD

image

界面:

image

后台代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GZFramworkDB.Model;
using GZFramworkDB.BLL;

namespace GZFramworkDB.Main.MyControls
{
    public partial class ucTableMD : UserControl, IData
    {
        public bllBusiness bll;

        protected DataTable dtMain;
        protected DataTable dtDetail;

        FormStatus Status;

        public ucTableMD()
        {

            InitializeComponent();
            Status = FormStatus.View;
            bll = new bllBusiness(typeof(tb_Customer), typeof(tb_CustomerDetail));
            gv_Summary.FocusedRowChanged += gv_Summary_FocusedRowChanged;
            this.gv_Detail.InitNewRow += new DevExpress.XtraGrid.Views.Grid.InitNewRowEventHandler(this.gv_Detail_InitNewRow);
        }

        void gv_Summary_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gv_Summary.FocusedRowHandle < 0)
            {
                gc_Detail.DataSource = null;
                return;
            }
            string Key = gv_Summary.GetFocusedDataRow()[bll.RelationKey].ToString();
            if (Status == FormStatus.Add)
            {
                dtDetail.DefaultView.RowFilter = String.Format("{0}='{1}'", tb_CustomerDetail.CustomerCode, Key);
                gc_Detail.DataSource = dtDetail;
            }
            else
            {
                DataSet ds = bll.GetDetailData(Key);
                gc_Detail.DataSource = ds.Tables[0].Copy();
            }

        }

        //明细表新增行的时候
        private void gv_Detail_InitNewRow(object sender, DevExpress.XtraGrid.Views.Grid.InitNewRowEventArgs e)
        {
            string CustomerCode = gv_Summary.GetFocusedRowCellValue(tb_Customer.CustomerCode).ToString();
            gv_Detail.SetRowCellValue(e.RowHandle, tb_Customer.CustomerCode, CustomerCode);
        }


        public void DoSearch()
        {
            gc_Summary.DataSource = null;
            gc_Detail.DataSource = null;

            if (dtMain != null)
                dtMain.Rows.Clear();
            if (dtDetail != null)
                dtDetail.Rows.Clear();

            dtMain = bll.GetSummaryData();
            dtDetail = bll.GetDetailData("").Tables[0];
            
            gc_Summary.DataSource = dtMain;
            gc_Detail.DataSource = dtDetail;

            Status = FormStatus.View;
        }



        public void DoAdd()
        {
            gc_Summary.DataSource = null;
            gc_Detail.DataSource = null;

            if (Status == FormStatus.View)
            {
                if (dtMain != null)
                    dtMain.Rows.Clear();
                if (dtDetail != null)
                    dtDetail.Rows.Clear();
                Status = FormStatus.Add;
            }
            dtMain.Rows.Add();

            gc_Summary.DataSource = dtMain;
            gc_Detail.DataSource = dtDetail;

        }

        public void DoDeleteKey()
        {
            string Key = gv_Summary.GetFocusedDataRow()[bll.SummaryKey].ToString();
           
            bll.Delete(Key);
            foreach (DataRow dr in dtDetail.Select(String.Format("{0}='{1}'", tb_CustomerDetail.CustomerCode, Key)))
            {
                dr.Delete();
            }

            gv_Summary.DeleteSelectedRows();

            dtDetail.AcceptChanges();
            dtMain.AcceptChanges();

        }

        public void DoDeleteTable()
        {
            
            string Key = gv_Summary.GetFocusedDataRow()[bll.SummaryKey].ToString();

            

            foreach (DataRow dr in dtDetail.Select(String.Format("{0}='{1}'", tb_CustomerDetail.CustomerCode, Key)))
            {
                dr.Delete();
            }

            gv_Summary.DeleteSelectedRows();

        }


        public void DoUpdate()
        {
            DataSet ds = new DataSet();
            ds.Tables.Add(dtMain.Copy());
            ds.Tables.Add(dtDetail.Copy());

            if (bll.Update(ds))
            {
                MessageBox.Show("更新成功!");
                return;
            }
            ds.Tables.Clear();
            dtDetail.AcceptChanges();
            dtMain.AcceptChanges();

            Status = FormStatus.View;
        }

    }

    public enum FormStatus
    {
        Add,
        View
    }
}

和前面一样修改Main.cs

运行结果:

201503211426922892550d1d8c7e622[4]

项目源码下载地址:https://github.com/GarsonZhang/GZFramework.Demo

系列文章

1. GZFramwork数据库层《前言》Demo简介

2. GZFramwork数据库层《前言》DLL项目引用

3. GZFramwork数据库层《一》普通表增删改查

4. GZFramwork数据库层《二》单据表增删改查(自动生成单据号码)

5. GZFramwork数据库层《三》普通主从表增删改查

6. GZFramwork数据库层《四》单据主从表增删改查(主键自动生成)

7. GZFramwork数据库层《五》高级主从表增删改查(主表明细表主键都自动生成)

8. GZFramwork数据库层《六》存储过程调用

9. GZFramwork数据库层《七》总结

慎于行,敏于思!GGGGGG
原文地址:https://www.cnblogs.com/GarsonZhang/p/4355675.html