I am trying to bind data to my DataGridView. (However, this is the easy explanation.)

Background on what I am trying to accomplish:
I have a rather large program here. This part of the code uses a DataGridView to display the data I have read and parsed (from another program through an Interop (not sql)). I bind the data using something like this:

private DataTable ConvertListToDataTable(List<List<object>> list)
{
    DataTable table = new DataTable();
    for (int i = 0; i < MyList.Count; i++)
    {
        table.Columns.Add(MyList[i], typeof(string));
    }
    table.Columns.Add("", typeof(object));

    // Add rows data
    List<object[]> tObj = new List<object[]>();
    for (int i = 0; i < list.Count; i++)
    {
        tObj.Add((object[])(list[i].ToArray()));
    }

    for (int i = 0; i < list.Count; i++)
    {
        table.Rows.Add(tObj[i]);
    }
    return table;
}

And it binds to the DataGrid with:

DataTable table2 = new DataTable();
table2 = ConvertListToDataTable(myList);

bindingSource1.DataSource = table2;
dataGridView1.DataSource = bindingSource1;

This works perfectly fine. The new problem is, MyList is no longer of type object. The data I read from the (Interop)source has both object and object[] in the same fields, but depends on which item it is reading. The data stored can be well over 1000 entries and the location of these internal lists (object[]) are not static.

I created a custom class to hold this data, it looks like this:

public class DataGridDataObject
{
    public object Data;
    public object[] DataList;
    public enum type { single, multi, none };
    public DataGridDataObject.type Type;
    public DataGridDataObject()
    {
        Data = null;
        DataList = null;
        Type = type.none;
    }
}

The ConvertListToDataTable(List<List<object>> list) changes to ConvertListToDataTable(List<List<DataGridDataObject>> list)

What I need to accomplish and hope can be done is within the DataGridView. When the enum.type of the object is single, make the cell in this row a TextBoxCell, when the enum.type is multi, make the cell a ComboBox.

I have read and been able to change an entire column to a combobox at creation but not every item is a list (which would require a combobox).

f7bbd226a849f93641efde621ffc1bf9

Here is the basic idea I am hoping to accomplish.
Is this even possible?

A side note, this is not sql, asp.net or a web based program.
I am using .net3.5

Thank you!

@ddanbe Thank you! That pointed me in the right direction. I used the second option mentioned in that article. I created custom objects that inherited the TextBox, ComboBox, etc., and then added them to the DataGridView.Control as needed.
New objects looks similar to this (minus secret data):

public class GSTTextBoxControl :  TextBox
{
    private DataGridDataObject _DGDO = null;
    public DataGridDataObject DGDO
    {
        get { return _DGDO; }
        set { _DGDO = value; }
    }

    private DataGridViewCell _DGVCell;
    public DataGridViewCell DGVCell
    {
        get { return _DGVCell; }
        set { _DGVCell = value; }
    }

    //Default Constructor
    public GSTTextBoxControl()
    {
        _DGVCell = null;
        _DGDO = null;
    }

    public GSTTextBoxControl(DataGridDataObject iDGDO, DataGridViewCell iCell)
    {
        _DGVCell = iCell;
        _DGDO = iDGDO;
        this.Dock = DockStyle.Fill;
        this.TextChanged += new EventHandler(Event_TextChanged);
    }

    //For text Editing
    private void Event_TextChanged(object sender, EventArgs e)
    {
        _DGVCell.Value = this.Text;
    }
}

and I add them to the control with:

this.dataGridView1.Controls.Add(GSTTextBox);
GSTTextBox.Location = this.dataGridView1.GetCellDisplayRectangle(j, i, false).Location;
GSTTextBox.Size = this.dataGridView1.GetCellDisplayRectangle(j, i, false).Size;

Works great and looks really good, however, both the scrollbar and sorting do not seem to have any effect on the added controls. I tried Docking the controls but the parent is the DataGridView. Is there a way to Dock the controls into a DataGridCell in the DataGridView?

I have only ever used DataGridView for very simple things. I am traveling in uncharted waters now (I didn't even know that there was a Control class for DataGridView before) so any references and/or pseudo-code (not really code even, maybe an algorithm idea of how to accomplish this) would be very welcome.

Thank you!

Well I found how to add controls to individual cells here:

Click Here

I will try to implement this today and go from there. Thank you!

This article has been dead for over six months. Start a new discussion instead.