【问题标题】:Combobox dropdownlist is not filling from datagridview row组合框下拉列表未从 datagridview 行填充
【发布时间】:2019-06-30 06:46:27
【问题描述】:

我希望当我单击 datagridview 行时,我的两个文本框和 combobox 填充相应的值,但两个文本框准确填充但类别下拉列表未填充。

我的 C# 代码是

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (e.RowIndex != -1 && e.ColumnIndex !=-1)
    {
        //edit = 1;
        DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
        proID = Convert.ToInt32(row.Cells["proIDGV"].Value.ToString());
        proTxt.Text = row.Cells["nameGV"].Value.ToString();
        barcodetxt.Text = row.Cells["barcodeGV"].Value.ToString();
        catDD.SelectedValue = row.Cells["catIDGV"].Value.ToString();// not working properly due to which edit button is not working
       // catDD.SelectedItem = row.Cells["catGV"].Value.ToString();//Also Write this line of code but not produce the desire result
        MainClass.Disabled(leftPanel);
    }
}

【问题讨论】:

  • 先生,我在 windowForms 工作
  • 你需要告诉我们你是如何设置ComboBox的DataSource的。
  • 组合框中的项目是否填写正确?您的代码仅显示选择不填充。
  • 检查comboBox的值,其值应与datagridview选择的行类别值相匹配
  • 如果 combobx.Items 中的值填充了 DGV 列中的可能值,那么 catDD.SelectedItem= row.Cells["catIDGV"].Value.ToString(); 应该可以工作。如果不是,则值或列名不正确。

标签: c# winforms


【解决方案1】:

对于作为组合框的类别下拉列表,要具有 SelectedItem 或 SelectedValue,您应该已经在“items”属性中拥有所有可能的类别,否则,编程无法突然选择类别。要选择不存在的项目,您需要先添加一个项目,或者如果您以后不需要这些项目,您可以使用:

catDD.Text = row.Cells["catIDGV"].Value.ToString();

虽然我不建议这样做。

另一种方法是您可以通过这样做来添加项目(这是我推荐的):

编辑:首先添加项目,然后选择它。

string item = row.Cells["catIDGV"].Value.ToString(); // Your selected item in DataGridView
catDD.Items.Add(item); // Add the item
catDD.SelectedItem = item; // Select the item

【讨论】:

  • 您是否尝试通过转到 items 属性在组合框项目中添加类别?
  • 如果有效,请选择我的答案作为官方答案。欢迎您。
  • 先生,我不知道如何选择官方答案,我是stackoverflow的新手
  • 您已经选择了它。嘿,你学到了一些东西 :) 谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 2013-12-19
相关资源
最近更新 更多