簡體   English   中英

動態將列添加到dataGridView

[英]Dynamically ADD Columns to dataGridView

可以說我的數據庫表中有300多個 cols,因為col#3之后的每個col都是相同的數據類型,是否有一種有效的方法從SqlCeConnection/SqlCeDatabase加載cols?

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
  }
}

我在網上發現DataGridView通過設置單個屬性具有此功能。

dataGridView1.AutoGenerateColumns = true; //只要在DB中定義了數據,它就可以完美地工作。

在這里找到

http://www.codeproject.com/Questions/182662/c-net-fill-data-to-already-defined-columns-in-data

希望這對你們也有幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM