在datagridview组合框中将查询结果显示为默认项

35g0bw71  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(379)

我有一个从mysql数据库加载数据到datagridview的查询,但是在同一个datagridview中我有一个组合框。正在从另一个查询填充项列表。当用户第一次保存数据时,他从组合框中选择一个项目并手动填充其他列。然后使用插入查询保存该行。我想做的是显示以前保存在datagridview中的内容,包括combobox选项。如何在组合框中显示以前保存为默认项的列表项?
以下是保存代码:

public void SaveOperations()
    {
        // create new row in project_operations with datagridview operations
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
        MySqlConnection con = new MySqlConnection(conSettings.ToString());
        MySqlCommand cmd = new MySqlCommand(@"insert into shopmanager.project_operations (products_product_id, operation_name, operation_description) values (@products_product_id, @operation_name, @operation_description)", con);
        con.Open();

        foreach (DataGridViewRow row in operation_dataGridView.Rows)
        {
            try
            {
                if (row.IsNewRow) continue;
                cmd.Parameters.AddWithValue("@products_product_id", product_id.Text);
                cmd.Parameters.AddWithValue("@operation_name", row.Cells["combo"].Value);
                cmd.Parameters.AddWithValue("@operation_description", row.Cells["Description"].Value);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        MessageBox.Show("Operation Sauvegardé");
        con.Close();
    }

这是装载代码。这会将数据加载到除组合框之外的行中的其他单元格。当我尝试加载操作名时,它会创建一个新列,但我希望操作名在组合框中。我怎样才能做到这一点?

private void LoadData()
    {
        // fill textbox columns
        MessageBox.Show("load operations data textboxes");
        ConnectionStringSettings conSettings = ConfigurationManager.ConnectionStrings["shopmanagerConnectionString1"];
        MySqlConnection con = new MySqlConnection(conSettings.ToString());
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        MySqlDataAdapter adapter = new MySqlDataAdapter();
        MySqlCommand cmd;
        cmd = new MySqlCommand(@"select operation_description as 'Description', operation_start_date as 'Début', operation_finish_date as 'Fin', users_employee_number as 'Employé' from shopmanager.project_operations where products_product_id = @product_id", con);

        try
        {
            con.Open();
            cmd.Parameters.AddWithValue("@product_id", product_id.Text);
            cmd.ExecuteNonQuery();
            adapter.SelectCommand = cmd;
            adapter.Fill(ds);
            dt = ds.Tables[0];

            operation_dataGridView.DataSource = dt;
            cmd.Parameters.Clear();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        con.Close();

    }
hrirmatl

hrirmatl1#

我想在设置完数据源之后,我会像下面这样做(对于combo或check):

//ADD COLUMNS
var comboBox = new DataGridViewComboBoxColumn();
comboBox.HeaderText = "Header title";
comboBox.Name = "combo box";

var dataRow = new ArrayList();

foreach(var dr in dt.Rows)
   dataRow.Add(dr["fieldName"].ToString()); // this will be the combo box data from database

// add data to combobox
comboBox.Items.AddRange(dataRow.ToArray());

// finally add the combo box to dgv
dataGridView1.Columns.Add(comboBox);

}

相关问题