【问题标题】:Dynamically ADD Columns to dataGridView将列动态添加到 dataGridView
【发布时间】:2012-10-26 01:12:04
【问题描述】:

假设我的数据库表中有超过 300 个列,因为列 #3 之后的每个列都是相同的数据类型,有没有一种有效的方法可以从 SqlCeConnection/SqlCeDatabase 加载列?

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

【问题讨论】:

    标签: c# winforms datagridview


    【解决方案1】:

    我在网上发现DataGridView通过设置一个属性就有这个功能。

    dataGridView1.AutoGenerateColumns = true; //只要在数据库中定义了数据,它就可以完美地工作。

    这是在这里找到的

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

    希望这对你们也有帮助:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多