【问题标题】:displaying query result as default item in a datagridview combo box在 datagridview 组合框中将查询结果显示为默认项
【发布时间】:2018-09-09 19:10:43
【问题描述】:

我有一个查询将数据从 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();

    }

【问题讨论】:

    标签: c# mysql datagridview combobox


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-10
      • 1970-01-01
      • 2011-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多