0

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;
 }
user3254596
  • 43
  • 1
  • 5

1 Answers1

0

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);
}
Evan L
  • 3,805
  • 1
  • 22
  • 31
  • { var comboColumn = new DataGridComboBoxColumn(); comboColumn.Name = "Security Group"; comboColumn.DataSource = secgrps; dataGridView1.Columns.Insert(2, comboColumn); } I added the presentation framework dll as a reference, so I could add the using system.windows.control, however, I comboColumn does contain a definition/method for .Name or .Datasource. What am I missing? – user3254596 Feb 06 '14 at 18:20
  • dataGridView1.Columns.Insert(2,ComboColumn) says its an invalid arg – user3254596 Feb 06 '14 at 18:38
  • Is this Windows Forms? You shouldn't need presentation framework, just System.Windows.Forms for DataGridView. If this is ASP.Net or WPF then this will change the solution. Please verify. – Evan L Feb 06 '14 at 19:03
  • Its a windows form, but if I don't have using system.windows.controls referenced. comboColumn.NAME and .DATASOURCE aren't valid. In fact the only .Header and .Itemsource is what found for the comboBox type. – user3254596 Feb 06 '14 at 19:49
  • Can you please post your new code exactly as it is, including the comboBoxColumn code you are working on? I have a feeling you have syntax or type errors. Please post this as an addition to your question. Not in a comment. – Evan L Feb 06 '14 at 19:52
  • Okay. I've updated the code in this question. Errors are as follows -> Error 1 The best overloaded method match for 'System.Windows.Forms.DataGridViewColumnCollection.Insert(int, System.Windows.Forms.DataGridViewColumn)' has some invalid arguments Error 2 Argument 2: cannot convert from 'System.Windows.Controls.DataGridComboBoxColumn' to 'System.Windows.Forms.DataGridViewColumn' – user3254596 Feb 07 '14 at 19:41
  • Remove Presentation Framework from your project. You don't need it, that is only for WPF applications. Make sure in your Form class you have `using System.Windows.Forms`. Once those are done, this should work. The only thing I can think of is that you used the wrong `DataGridView` when you created your project. – Evan L Feb 07 '14 at 20:31
  • Which version of .NET are you using? – user3254596 Feb 07 '14 at 22:44
  • .NET FrameWork 4 is what I have as well. Visual Studio 2010. Output type Windows Application. My base OS is Windows 8.1. this.dataGridView1 = new System.Windows.Forms.DataGridView(); Is where its creating from. if I change DataGridComboBoxColumn to DataGridViewComboBoxColumn, then .name and .datasource work. – user3254596 Feb 08 '14 at 04:00
  • Yes thats what my example showed. Not datagrid but datagridview – Evan L Feb 08 '14 at 16:21