gridview-子母表

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Collections.Specialized;

namespace gridview_子母表
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                
            MySqlConnection con = new MySqlConnection(@"SERVER=localhost;UID=root;DATABASE=anxinwh;PWD=123456;charset = utf8 ;Allow Zero Datetime=True");
            con.Open();
            #region 主表数据
             string stradt = string.Format("select *  from t_checkResult");
            MySqlDataAdapter adt = new MySqlDataAdapter(stradt, con);
            
            DataTable dt_zhubiao = new DataTable();

            adt.Fill(dt_zhubiao);
            gridControl1.DataSource = dt_zhubiao.DefaultView;
            #endregion

            #region 子表数据
            string stradt_detail = string.Format("select *  from t_checkdetailResult");
            MySqlDataAdapter adt_detail = new MySqlDataAdapter(stradt_detail, con);
            DataTable dt_detail = new DataTable();
            adt_detail.Fill(dt_detail);
            ShowDetail(dt_detail);
            #endregion
            
            this.gridView1.BestFitColumns();
                gridView1.OptionsView.ColumnAutoWidth = true;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        private void ShowDetail(DataTable Detail)
        {
            StringDictionary w_dicConds = new StringDictionary();
            w_dicConds["check_id"] = "check_id";

            try
            {
                //设置编辑,保存按钮可用
                if (this.gridView1.DataSource == null)
                {
                    return;
                }

                DataSet ds = new DataSet();

                //获取主表数据
                DataTable dt;
                ds.Tables.Add(((DataView)this.gridView1.DataSource).Table.Copy());

                //获取详细数据
                
                dt = Detail.Copy();
                if (dt.DataSet != null)
                {
                    dt.DataSet.Tables.Remove(dt);
                }
                dt.TableName = "t_checkdetailResult";
                ds.Tables.Add(dt);

                //管理主表与关联表
                //将两个gridview建立联系
                DataRelation relation1 = new DataRelation("relation",
                                                        ds.Tables[0].Columns["check_id"],
                                                        ds.Tables[1].Columns["check_id"], false);

                ds.Relations.Add(relation1);
                this.gridControl1.DataSource = ds.Tables[0];
                this.gridView1.BestFitColumns();
                this.gridView2.BestFitColumns();

            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    }
}

  

posted @ 2015-11-05 10:47  人生为卒  阅读(1063)  评论(0编辑  收藏  举报