c# - 在 datagridview 组合框中将查询结果显示为默认项
问题描述
我有一个查询将数据从 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();
}
解决方案
我想在设置数据源之后,我会像下面这样(对于组合或检查):
//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);
}
推荐阅读
- python - Dataframe - 在 x 轴上组织数据和绘制列
- python - 使用 osmnx 获取 osm 步行网络,如果人行道沿这条路,则删除道路
- python-3.x - 如何加载和读取多个 csv,然后合并到一个文件中?
- javascript - 在 JavaScript 中将数据动态写入嵌套对象
- amazon-web-services - 如何使用 AWS CLI 查找与 ec2 关联的辅助私有 IPv4 地址是什么?
- python - 使用 Python 查找 JSON 中特定部分的值
- python-3.x - 为什么 Python 的第三个包默认安装在与 Python 在 Windows 上的安装不同的目录中?
- asp.net-core - 如何将 pdf 文件传递给需要 IFormFile 的控制器
- apache-storm - 在 Apache Storm 中的 python bolt 中勾选元组
- html - 元素验证
错误- 元素