DataGridColumn更改为ComboBox

时间:2014-02-05 23:21:48

标签: c# datagridview datagridcomboboxcolumn

我有usrdetails列表,我将其用作DataGridview的数据源。 两个问题:

  1. 如何将DataGridview中第3列的列类型从DataGridTextBoxColumn更改为DataGridComboBoxColumn

  2. 一旦我将第3列更改为ComboBox,我将如何填充字符串列表?


  3. public class usrInfo
    {
        public string userID { get; set; }
        public string username { get; set; }
        public string group { get; set; }
        public string seclev { get; set; }
        public string isext { get; set; }
    
        public usrInfo(string userID, string username, string group, string seclev, string isext)
        {
            this.userID = userID;
            this.username = username;
            this.group = group;
            this.seclev = seclev;
            this.isext = isext;
        }
    }
    
    public static List<usrInfo> usrdetails = new List<usrInfo>();
    
    private void Form5_Load(object sender, EventArgs e)
    {
        var comboColumn = new DataGridViewComboBoxColumn();
    
    
            dataGridView1.DataSource = null;
            dataGridView1.DataSource = usrdetails;
            for (int i = 0; i < secgrps.Count; i++)
              groups.Add(secgrps[i].ToString());
            comboColumn.Name ="Security Group";
            comboColumn.DataSource = groups;
            dataGridView1.Columns.Insert(2, comboColumn);
            usrdetails.Add(new usrInfo("domain\\userID", "User Name", "RIGSITE ONLY Wellsite Leader", "7", "Y"));
            dataGridView1.Refresh();
            if (usrdetails.Count > -1)
                num_users = true;
     }
    

1 个答案:

答案 0 :(得分:0)

您需要向DataGridViewComboBoxColumn控件添加dataGridView1,并将其DataSource属性分配给您想要的字符串集合。

var myStringCollection = new[] {"String1", "String2", "String3"};

var comboColumn = new DataGridViewComboBoxColumn();
comboColumn.Name = "MyComboColumn";
comboColumn.DataSource = myStringCollection; //This sets the source of drop down items

然后将其插入网格:

if (dataGridView1.Columns["MyComboColumn"] == null)
{
    //The int value as first parameter of Insert() is the desired Column Index
    dataGridView1.Columns.Insert(0, comboColumn);
}