2012-10-26 169 views
2

可以说我的数据库表中有多于 cols,因为col#3之后的每个col是相同的数据类型,有没有一种有效的方法来从SqlCeConnection/SqlCeDatabase加载col?将数据列动态添加到dataGridView

private void loadDataGridView() { 
    String CmdString = "...... FROM MyDatabaseTable"; //some sql to get col info 
    SqlCeCommand cmd = new SqlCeCommand(CmdString, con); 
    SqlCeDataAdapter sda = new SqlCeDataAdapter(cmd); 
    DataTable dt = new DataTable("mastertable"); 
    sda.Fill(dt); 
    foreach(Col col in dt) { 
    DataGridViewColumn c = new DataGridViewColumn(..); //Create the Col 
    c.DataPropertyName = col.Name; //Bind the col and property name 
    dataGridView1.Cols.Add(new dataGridViewColumn(...)); //Add the col 
    } 
} 

回答