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

标签 c# mysql datagridview combobox

我有一个查询将数据从 MySQL 数据库加载到数据 GridView 中,但在同一个数据 GridView 中我有一个组合框。正在从另一个查询填充项目列表。当用户第一次保存数据时,他从组合框中选择一个项目并手动填写其他列。然后使用插入查询保存该行。我想要做的是显示以前保存在 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();

    }

最佳答案

我想在设置数据源之后,我会像下面那样(对于组合或检查):

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

}

关于c# - 在 datagridview 组合框中将查询结果显示为默认项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52247822/

相关文章:

php - rsyslog 性能优化

c# - DataGridAutoGeneratingColumnEventArgs.PropertyDescriptor 是什么类型的?

php - SQL在一行中查询一个值并在另一行中更改它的值

mysql - 用于用户数据库的 XML 还是 MySQL?

c# - 有时我想隐藏 DataGridViewButtonColumn 中的按钮

c# - 从datagridview插入数据到数据库

c# - 如何区分Windows的服务器版本和客户端版本?

c# - C# 中的只读列表

c# - 在 ASP.NET MVC 中为所有 Controller 路由设置顺序

C# DataGridView Checkbox 勾选事件