簡體   English   中英

在 C# DatagridView 中插入行,在表中使用外鍵連接到 MYSQL

[英]Insert Row in C# DatagridView connected to MYSQL with foreign key in tables

我正在嘗試制作一個應用程序來下訂單。 我在 Mysql 中有 4 個表:

CREATE TABLE `containers` (
  `id_containers` int(11) NOT NULL AUTO_INCREMENT,
  `container_name` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_containers`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4

CREATE TABLE `order` (
  `id_order` int(11) NOT NULL AUTO_INCREMENT,
  `out_number` varchar(45) NOT NULL,
  `out_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `order_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `client` varchar(45) NOT NULL,
  `client_ref` varchar(45) DEFAULT NULL,
  `billed` tinyint(1) DEFAULT NULL,
  `notes` longtext,
  PRIMARY KEY (`id_order`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4

CREATE TABLE `order_details` (
  `id_order_item` int(11) NOT NULL AUTO_INCREMENT,
  `id_order` int(11) NOT NULL,
  `id_product` int(11) NOT NULL,
  `id_container` int(11) NOT NULL,
  `pallets` int(11) DEFAULT NULL,
  `volumes` int(11) DEFAULT NULL,
  `caliber` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`id_order_item`),
  KEY `id_container` (`id_exportation`),
  CONSTRAINT `id_container` FOREIGN KEY (`id_order`) REFERENCES `containers` (`id_containers`),
  CONSTRAINT `id_order` FOREIGN KEY (`id_order`) REFERENCES `order` (`id_order`),
  CONSTRAINT `id_product` FOREIGN KEY (`id_order`) REFERENCES `products` (`id_product`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

CREATE TABLE `products` (
  `id_product` int(11) NOT NULL AUTO_INCREMENT,
  `product_name` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id_product`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

在我的 C# 應用程序中,我創建了一個 DatagridView,並使用 INNER JOIN function 按順序顯示項目。

private BindingSource GetOrderList()
        {
            string connString = ConfigurationManager.ConnectionStrings["ManagementApp.Properties.Settings.ConnectionString"].ConnectionString;
            MySqlConnection con = new MySqlConnection(connString);
            con.Open();

            mysqladapter1.SelectCommand = new MySqlCommand("SELECT products.product_name as 'Produto', containers.container_name as 'Vasilhame', order_details.pallets 'Pallets', order_details.volumes as 'Volumes', order_details.caliber as 'Calibre' " +
                "FROM order" +
                "INNER JOIN order_details ON order.id_exportation = order_details.id_order " +
                "INNER JOIN products ON order_details.id_product = products.id_product " +
                "INNER JOIN containers ON order_details.id_container = containers.id_containers " +
                "WHERE order.id_order = 1;", con);
            cmdbuilder = new MySqlCommandBuilder(mysqladapter1);

            DataTable table = new DataTable();
            mysqladapter1.Fill(table);

            OrderBindingSource.DataSource = table;

            return OrderBindingSource;
        }

可以向此 datagridview 添加一個新行以插入產品項目並刪除,但我不知道如何將其發送到 MYSQL 因為我在每一行都有多個帶有外鍵的表。 我想在產品單元格中單擊“ENTER”,打開一個包含所有產品的 window 和 select 我想在行和容器中插入什么產品。

我會盡可能使用DataGridViewComboBoxColumn 就像:

DataGridViewComboBoxColumn dcb = this.DataGridView1.Columns("Category");
dcb.ValueMember = "ID";
dcb.DisplayMember = "CategoryName";
dcb.DataSource = DataTableCategories;

如果您確實需要為特定項目的選擇運行對話框,您可以使用隱藏值列(一個DataGridViewTextBoxColumn來存儲選定的 ID)、顯示DataGridViewTextBoxColumn )和DataGridViewButtonColumn的組合來使用Button觸發新對話框(您可以分配對每列檢查 e.ColumnIndex 的不同對話框,甚至每行,如果需要,通過 e.RowIndex)。 如果您需要將某些內容傳遞給按鈕,您可以使用DataGridViewButtonCellTag屬性。 使用這種方法,您將有效地模擬DataGridViewComboBox邏輯,但如果您確實需要一個對話框來為最終用戶提供選擇幫助,那么應用它可能是合理的。 顯然,您必須處理 Click 事件並為每次單擊創建所需表單的新實例,創建 Public ByRef DataGridView 或更好的 DataTable(用於 DGV 數據源), ShowDialog()並確保在完成編輯后,您'必須在close()之前更新DataGridView (行或DataTable Row)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM