开发者

How create DataGridViewComboBoxColumn from simple DataTable

I create user control, editing, showing range DataGridView.

My user control takes DataTable

public static DataTable CreateDataTable()
{
    var myDataTable = new DataTable();

    DataColumn myDataColumn;

    myDataColumn = new DataColumn
    {
        DataType = Type.GetType("System.String"),
        ColumnName = "id",
        ReadOnly = true
    };
    myDataTable.Columns.Add(myDataColumn);

    myDataColumn = new DataColumn
    {
        DataType = Type.GetType("System.String"),
        ColumnNa开发者_运维问答me = "citys"
    };
    myDataTable.Columns.Add(myDataColumn);
    return myDataTable;
}

I set myDataTables to DataSource and all worked.

but I need to create column citys DataGridViewComboBoxColumn. How to do this?


try this

DataGridViewComboBoxColumn oCol = new DataGridViewComboBoxColumn();
oCol.Name = "cities";
oCol.DataSource = //your DataSource
myDataGridView.Columns.Add(oCol);

Hope it helped !


Try this way:

        DataTable table = CreateDataTable();
        foreach (DataColumn column in table.Columns)
        {
            dataGridView1.Columns.Add(column.ColumnName, column.ColumnName);
        }
        //there is you code too here.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜