开发者

How create DataGridViewComboBoxColumn from simple DataTable

开发者 https://www.devze.com 2023-03-16 20:41 出处:网络
I create user control, editing, showing range DataGridView. My user control takes DataTable public static DataTable CreateDataTable()

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

精彩评论

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