【问题标题】:DataGridViewComboBoxColumn get value from other columnDataGridViewComboBoxColumn 从其他列获取值
【发布时间】:2018-07-16 11:48:59
【问题描述】:

我从 sql 数据库中提取数据并用它填充 datagridView1:

dataGridView1.DataSource = bsour;

用户将能够编辑 1 列中的值,但该列的可能值只有 Null、0、1 或 2,因此为了防止输入不正确的值,我决定做一个 ComboBoxColumn:

DataGridViewComboBoxColumn col1 = new DataGridViewComboBoxColumn();
col1.Name = "il_normal_combo";
col1.HeaderText = "Normal";
col1.Items.Add("");
col1.Items.Add("0");
col1.Items.Add("1");
col1.Items.Add("2");
dataGridView1.Columns.Insert(5, col1);

我将用 .Visible = false; 隐藏的另一个现在不必要的列 我希望 ComboBoxColumn 显示来自将被隐藏的其他列的数据库值,我似乎无法让它工作。

我尝试过的:

col1.ValueMember = "il_normal";
col1.DisplayMember = "il_normal";

那些不起作用但没有错误消息。 这不起作用并产生错误“值不正确”

for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
{
     dataGridView1.Rows[i].Cells[5].Value = dataGridView1.Rows[i].Cells[7].Value;
}

【问题讨论】:

    标签: c# datagridview datagridviewcomboboxcolumn


    【解决方案1】:

    我在这里发现了类似的问题 Change DataGridViewColumn to DataGridViewComboBoxColumn in bound DataGridView 但是该帖子中的解决方案对我不起作用。在 ComboBoxColumn 而不是数字中,我得到 System.Data.DataRows.(something) 所以我推断它不知道要使用哪一列(即使 DataSource 中只有一个列)所以我添加了

    col1.DisplayMember = "number";
    

    然后它起作用了:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-12-05
      • 2016-11-30
      • 2020-07-06
      • 1970-01-01
      • 2021-09-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多