質問

I have list usrdetails that I'm using as my data source for my DataGridview. Two questions:

  1. How can I change the column type for the 3rd Column in my DataGridview from a DataGridTextBoxColumn to a DataGridComboBoxColumn?

  2. Once I've changed the 3rd column to a ComboBox how would I populate with list of strings?


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;
 }
役に立ちましたか?

解決

You need to add a DataGridViewComboBoxColumn to your dataGridView1 control and assign its DataSource property to the collection of strings you want.

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

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

Then insert it into your grid:

if (dataGridView1.Columns["MyComboColumn"] == null)
{
    //The int value as first parameter of Insert() is the desired Column Index
    dataGridView1.Columns.Insert(0, comboColumn);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top