简体   繁体   English

动态将列添加到dataGridView

[英]Dynamically ADD Columns to dataGridView

Lets say I have over 300 cols in my database table, as each col after col # 3 are the same data type, Is there an efficient way to load the cols from the SqlCeConnection/SqlCeDatabase ? 可以说我的数据库表中有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
  }
}

I found online that the DataGridView has this facility by setting a single property. 我在网上发现DataGridView通过设置单个属性具有此功能。

dataGridView1.AutoGenerateColumns = true; //as long as the Data is defined in DB it works perfectly. //只要在DB中定义了数据,它就可以完美地工作。

This was found here 在这里找到

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

Hope this helps you guys too :) 希望这对你们也有帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM