简体   繁体   中英

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

I'm trying to make an App to make Order. I have 4 tables in Mysql:

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

In my C# app i created a DatagridView, and show the items in Order using 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;
        }

To this datagridview can be added a new row to insert products items and removed, but i don't know how to send this to MYSQL because i have multiple tables with foreign keys in every row. I want to click "ENTER" inside product cell, open a window with all products and select what product i want to insert in row and in container too.

I would use DataGridViewComboBoxColumn where ever possible. Simply like:

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

If you really need to run a dialog for a selection of particular item, you can use a combination of hidden value column (a DataGridViewTextBoxColumn to store selected ID), diplay column ( DataGridViewTextBoxColumn ) and DataGridViewButtonColumn to trigger new dialog using Button (you can assign a different dialog to each column checking for e.ColumnIndex, even each row, if needed, via e.RowIndex). If you need to pass something to button, you can employ Tag property of the DataGridViewButtonCell . With this approach you would effectively emulate a DataGridViewComboBox logic, but in case you really need a dialog to provide an assistance with selection to the end user, it might be reasonable to apply it. Obviously, you will have to handle a Click event and create a new instance of desired form for every click, create a Public ByRef DataGridView or better DataTable (for DGV datasource), ShowDialog() and make sure that after you will finish editing, you'll have to update DataGridView (row, or the DataTable Row) before you close() it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM