从DataTable加载后如何在DataGridView中添加自定义列

时间:2019-04-30 00:14:45

标签: c# datagridview

我的Windows窗体应用程序上有一个DataGridView以及一个Textbox字段。在“文本框”字段中输入数据后,将重新加载DataGridView。

我查询我的SQL Server,并从结果生成一个DataTable。 然后将这些结果加载到DataGridView中。
我想向DataGridView添加一些其他列。 我已经在下面的代码中添加了它们,但是,当再次调用该函数时,它将向现有的DataGridView定义添加另一组列。如果我多次刷新数据,则每次都会添加另一组。

如果我在从DataTable加载DataGridView之前添加它们,则首先添加列,这不是我想要的。

在加载之前我应该​​以某种方式擦除DataGridView吗? 创建之前是否需要检查该列是否已经存在?

这里最好的选择是什么?

private void FillTable(string location, string supplier, string item) {
        string sql = "";
        string connString = "Data Source=B606SQLERP;Initial Catalog=P21DCImport17;Integrated Security=true;";

        // If ONLY Location is entered
        if (location != "" && supplier == "" && item == "") {
            MessageBox.Show("Only Location");
            sql = "SELECT " +
                    "      location_id AS 'Location', " +
                    "      supplier_id AS 'Supplier', " +
                    "      item_id AS 'Item', " +
                    "      recommended_qty_to_order AS 'Rec Qty'" +
                    "  FROM PorgReqs  " +
                    "  WHERE location_id = @LocationID " +
                    "  ORDER BY supplier_id, item_id";

        }

        // If ONLY Supplier is entered
        if (location == "" && supplier != "" && item == "") {
            MessageBox.Show("Only Supplier");
            sql = "SELECT " +
                    "      location_id AS 'Location', " +
                    "      supplier_id AS 'Supplier', " +
                    "      item_id AS 'Item', " +
                    "      recommended_qty_to_order AS 'Rec Qty'" +
                    "  FROM PorgReqs  " +
                    "  WHERE supplier_id = @SupplierID " +
                    "  ORDER BY location_id, item_id";

        }

        // If ONLY Item is entered
        if (location == "" && supplier == "" && item != "") {
            MessageBox.Show("Only Item");
            sql = "SELECT " +
                    "      location_id AS 'Location', " +
                    "      supplier_id AS 'Supplier', " +
                    "      item_id AS 'Item', " +
                    "      recommended_qty_to_order AS 'Rec Qty'" +
                    "  FROM PorgReqs  " +
                    "  WHERE item_id = @ItemID " +
                    "  ORDER BY location, supplier_id";

        }


        // Creates Connection, Sets Variables in Connection and tries to load table
        using (SqlConnection conn = new SqlConnection(connString)) {
            SqlCommand cmd = new SqlCommand(sql, conn);

            // Sets Variables
            cmd.Parameters.AddWithValue("@LocationID", txtLocation.Text);
            cmd.Parameters.AddWithValue("@SupplierID", txtSupplier.Text);
            cmd.Parameters.AddWithValue("@ItemID", txtItem.Text);

            // Opens Connection
            conn.Open();

            try 
            {
                // Creates Reader Object
                SqlDataReader sqlDataReader = cmd.ExecuteReader();


                // Fill the list box with the values retrieved
                if (sqlDataReader.HasRows) {
                    // Create DataTable
                    DataTable dt = new DataTable();

                    // Add Columns to DataTable
                    dt.Columns.Add("Location");
                    dt.Columns.Add("Supplier");
                    dt.Columns.Add("Item");
                    dt.Columns.Add("Rec Qty");
                    dt.Load(sqlDataReader);

                    gridData.DataSource = dt;

                    DataGridViewTextBoxColumn txtAddlQty = new DataGridViewTextBoxColumn();
                    txtAddlQty.Name = "AddlQty";
                    txtAddlQty.HeaderText = "Add'l Qty";
                    txtAddlQty.Width = 75;
                    gridData.Columns.Add(txtAddlQty);

                    DataGridViewTextBoxColumn txtFinalQty = new DataGridViewTextBoxColumn();
                    txtFinalQty.Name = "FinalQty";
                    txtFinalQty.HeaderText = "Final Qty";
                    txtFinalQty.Width = 75;
                    gridData.Columns.Add(txtFinalQty);

                    DataGridViewComboBoxColumn cboAction = new DataGridViewComboBoxColumn();
                    cboAction.Name = "Action";
                    cboAction.HeaderText = "Action";
                    cboAction.Items.AddRange("Buy", "Transfer", "Not Buy");
                    cboAction.Width = 75;
                    gridData.Columns.Add(cboAction);

                    DataGridViewCheckBoxColumn chkApprove = new DataGridViewCheckBoxColumn();
                    chkApprove.Name = "Approve";
                    chkApprove.HeaderText = "Approve";
                    chkApprove.Width = 50;
                    gridData.Columns.Add(chkApprove);

                    gridData.Columns["Location"].Width = 60;
                    gridData.Columns["Supplier"].Width = 60;
                    gridData.Columns["Item"].Width = 125;
                    gridData.Columns["Rec Qty"].Width = 75;

                    // Adds default value to the Action column
                    foreach (DataGridViewRow row in gridData.Rows) {
                        row.Cells["Action"].Value = "Buy";
                    }
                }

            } catch (Exception ex) {
                Console.WriteLine(ex.Message);
                MessageBox.Show(ex.Message);
            }
        }
    } // End Function

0 个答案:

没有答案