在DataGrid和DataGridView中顯示主從表的數據


微軟提供了DataGridView以替代DataGrid,但為了向下兼容,在新版的.net Framework中還是提供了DataGrid。

如果要在一個DataGrid中實現主從表的功能。關鍵是創建DataSet中表之間的關系。

此示例在DataGrid和DataGridView控件分別實現中顯示2個關聯表的數據,NorthWind數據庫中的Customers表和Orders表是一對多的關系,Customers表的主鍵CustomerID作為Orders表的外鍵。

如:

            //創建表之間的關系。
            DataRelation dataRelation;
            DataColumn dataColumn1, dataColumn2;
            dataColumn1 = myDs.Tables["Customers"].Columns["CustomerID"];
            dataColumn2 = myDs.Tables["Orders"].Columns["CustomerID"];
            dataRelation = new DataRelation("CustomersToOrders",dataColumn1,dataColumn2);

            //在DataSet中添加關系
            myDs.Relations.Add(dataRelation);

然后,設置DataGrid的相關屬性,顯示數據:

            //設置DataGridView的視圖和DataMembers成員並顯示數據
            DataViewManager DataSetView = myDs.DefaultViewManager;
            dataGrid1.DataSource = DataSetView;
            dataGrid1.DataMember = "Customers";

運行結果如下圖:

全部代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//此示例在DataGridView控件中顯示2個關聯表的數據。
//NorthWind數據庫中的Customers表和Orders表是一對多的關系。
//Customers表的主鍵CustomerID作為Orders表的外鍵。
namespace WindowsFormsApplication6
{
    using System.Data.SqlClient;
    public partial class Form1 : Form
    {
        private SqlConnection myConn;//數據庫連接對象
        private DataSet myDs;//數據集
        private SqlCommand myCmd1,myCmd2;//數據庫命令對象。
        private SqlDataAdapter myDataAdt1,myDataAdt2;//數據適配器。

        public Form1()
        {
            InitializeComponent();
            //創建數據庫連接連接對象,實例化,並打開連接。
            //如果不會寫連接字符串,可以在IDE中數據-->添加數據源
            //然后選擇要連接的數據庫,把產生出來的連接字符串復制過來就好。
            string conStr = "Data Source=.;Initial Catalog=NorthWind;Integrated Security=True";
            myConn = new SqlConnection(conStr);
            myConn.Open();

            //創建DataSet
            myDs = new DataSet();
            myDs.CaseSensitive = true;//此數據集的字符串比較區分大小寫(大小寫敏感)
            
            //為Customer表創建數據庫命令並用數據適配器填充數據到數據集。
            myCmd1 = new SqlCommand();
            myCmd1.Connection = myConn;
            myCmd1.CommandText = "select * from Customers";
            myDataAdt1 = new SqlDataAdapter();
            myDataAdt1.SelectCommand = myCmd1;
            myDataAdt1.TableMappings.Add("Table", "Customers");
            myDataAdt1.Fill(myDs);

            //為Orders表創建數據庫命令並用數據適配器填充數據到數據集。
            myCmd2 = new SqlCommand();
            myCmd2.Connection = myConn;
            myCmd2.CommandText = "select * from Orders";
            myDataAdt2 = new SqlDataAdapter();
            myDataAdt2.SelectCommand = myCmd2;
            myDataAdt2.TableMappings.Add("Table", "Orders");
            myDataAdt2.Fill(myDs);

            //創建表之間的關系。
            DataRelation dataRelation;
            DataColumn dataColumn1, dataColumn2;
            dataColumn1 = myDs.Tables["Customers"].Columns["CustomerID"];
            dataColumn2 = myDs.Tables["Orders"].Columns["CustomerID"];
            dataRelation = new DataRelation("CustomersToOrders",dataColumn1,dataColumn2);

            //在DataSet中添加關系
            myDs.Relations.Add(dataRelation);

            //設置DataGridView的視圖和DataMembers成員並顯示數據
            DataViewManager DataSetView = myDs.DefaultViewManager;
            dataGrid1.DataSource = DataSetView;
            dataGrid1.DataMember = "Customers";
        }
    }
}

 上述是在DataGrid中實現主從表功能。在DataGirdView中,一個DataGridView無法顯示多個表。要想實現主從表的功能,微軟MSDN的變通的方法是:

用2個DataGridView,一個顯示主表數據,一個顯示從表數據,在主表中選擇一行記錄,從表中也會隨之變化,顯示相應的記錄。 實現的關鍵是:

    把數據集綁定到主綁定源masterBindingSource

    把從數據源綁定到主數據源。
    用數據表之間的關系CustomersToOrders基於主表的當前行中從從表中篩選數據。

如下代碼所示:

            //把數據集綁定到主綁定源masterBindingSource
            masterBindingSource.DataSource = myDs;
            masterBindingSource.DataMember = "Customers";

            //把從數據源綁定到主數據源。
            //用數據表之間的關系CustomersToOrders基於主表的當前行中從從表中篩選數據。
            detailsBindingSource.DataSource = masterBindingSource;
            detailsBindingSource.DataMember = "CustomersToOrders";

最后把數據綁定對象綁定到DataGridView:

            //設置DataGridView數據源
            masterDataGridView.DataSource = masterBindingSource;
            detailsDataGridView.DataSource = detailsBindingSource;

運行效果如圖所示:

全部代碼如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//此示例在DataGridView控件中顯示2個關聯表的數據。
//NorthWind數據庫中的Customers表和Orders表是一對多的關系。
//Customers表的主鍵CustomerID作為Orders表的外鍵。
namespace WindowsFormsApplication6
{
    using System.Data.SqlClient;
    public partial class Form1 : Form
    {
        private SqlConnection myConn;//數據庫連接對象
        private DataSet myDs;//數據集
        private SqlCommand myCmd1,myCmd2;//數據庫命令對象。
        private SqlDataAdapter myDataAdt1,myDataAdt2;//數據適配器。
        //數據源綁定對象
        private BindingSource masterBindingSource = new BindingSource();
        private BindingSource detailsBindingSource = new BindingSource();
        public Form1()
        {
            InitializeComponent();
            //創建數據庫連接連接對象,實例化,並打開連接。
            //如果不會寫連接字符串,可以在IDE中數據-->添加數據源
            //然后選擇要連接的數據庫,把產生出來的連接字符串復制過來就好。
            string conStr = "Data Source=.;Initial Catalog=NorthWind;Integrated Security=True";
            myConn = new SqlConnection(conStr);
            myConn.Open();

            //創建DataSet
            myDs = new DataSet();
            myDs.CaseSensitive = true;//此數據集的字符串比較區分大小寫(大小寫敏感)
            
            //為Customer表創建數據庫命令並用數據適配器填充數據到數據集。
            myCmd1 = new SqlCommand();
            myCmd1.Connection = myConn;
            myCmd1.CommandText = "select * from Customers";
            myDataAdt1 = new SqlDataAdapter();
            myDataAdt1.SelectCommand = myCmd1;
            myDataAdt1.TableMappings.Add("Table", "Customers");
            myDataAdt1.Fill(myDs);

            //為Orders表創建數據庫命令並用數據適配器填充數據到數據集。
            myCmd2 = new SqlCommand();
            myCmd2.Connection = myConn;
            myCmd2.CommandText = "select * from Orders";
            myDataAdt2 = new SqlDataAdapter();
            myDataAdt2.SelectCommand = myCmd2;
            myDataAdt2.TableMappings.Add("Table", "Orders");
            myDataAdt2.Fill(myDs);

            //創建表之間的關系。
            DataRelation dataRelation;
            DataColumn dataColumn1, dataColumn2;
            dataColumn1 = myDs.Tables["Customers"].Columns["CustomerID"];
            dataColumn2 = myDs.Tables["Orders"].Columns["CustomerID"];
            dataRelation = new DataRelation("CustomersToOrders",dataColumn1,dataColumn2);

            //在DataSet中添加關系
            myDs.Relations.Add(dataRelation);

            //把數據集綁定到主綁定源masterBindingSource
            masterBindingSource.DataSource = myDs;
            masterBindingSource.DataMember = "Customers";

            //把從數據源綁定到主數據源。
            //用數據表之間的關系CustomersToOrders基於主表的當前行中從從表中篩選數據。
            detailsBindingSource.DataSource = masterBindingSource;
            detailsBindingSource.DataMember = "CustomersToOrders";

            //設置DataGridView數據源
            masterDataGridView.DataSource = masterBindingSource;
            detailsDataGridView.DataSource = detailsBindingSource;

        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM