将项目添加到DataGridView中的组合框 [英] Adding items to a combobox in DataGridView

查看:53
本文介绍了将项目添加到DataGridView中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在winforms应用程序中有一个DataGridView.除了来自db表的4列之外,我还需要显示一个在datagridview中具有组合框的附加列[可能正在使用DataGridViewComboColumn?].2.然后,我想为每一行的每个组合框添加不同的项目集.

I've a DataGridView in a winforms app. Apart from the 4 columns coming from the db table,I need to show an additional column having a combobox in the datagridview[may be using DataGridViewComboColumn?]. 2.And then I want to add different set of items to each combobox for every row.

我该怎么办?

谢谢.

推荐答案

您可以尝试通过网格的 DataBindingComplete 添加它们

You may try to add them via DataBindingComplete of the grid

这些东西上的东西

void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
       if (row.Cells[0] is DataGridViewComboBoxCell && row.Index == 1)
          (row.Cells[0] as DataGridViewComboBoxCell).Items.Add("A");
       else
          (row.Cells[0] as DataGridViewComboBoxCell).Items.Add("B");
    }
}

希望这会有所帮助编辑

(row.Cells[0] as DataGridViewComboBoxCell).Value = (row.Cells[0] as DataGridViewComboBoxCell).Items[0];

选中该单元格后,第一个值将显示为选中状态

When that cell is selected then the first value would be shown selected

这篇关于将项目添加到DataGridView中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆