繁体   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