2012-10-26 218 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 
    } 
} 

回答