zoukankan      html  css  js  c++  java
  • 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;
                }
            }
    
        }
    }
    

      

  • 相关阅读:
    iOS开发之--隐藏状态栏
    iOS开发之--iPhone X 适配:MJRefresh上拉加载适配
    iOS开发之--为UITextField监听数值变化的三种方法
    ios开发之--为父view上的子view添加阴影
    iOS开发之--在UIWindow上展示/移除一个View
    iOS开发之--Masonry多个平均布局
    CocoaPods更新过程中出现的坑及解决方法
    那些已成定局的人和事
    两个陌生人的对话
    好好写代码吧,没事别瞎B去创业!
  • 原文地址:https://www.cnblogs.com/lhlong/p/4938716.html
Copyright © 2011-2022 走看看