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

时间:2018-09-09 19:10:44

标签: c# mysql datagridview combobox

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

这是保存代码:

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();
    }

这是加载代码。这会将数据加载到该行中除组合框之外的其他单元格中。当我尝试加载operation_name时,它会创建一个新列,但我想要的是将操作名放在组合框中。我该如何实现?

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();

    }

1 个答案:

答案 0 :(得分:0)

我认为在设置数据源之后,我将像下面这样(无论是组合还是检查):

//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);

}