【问题标题】:C# - "Dynamic" DataGridViewC# - “动态” DataGridView
【发布时间】:2018-08-24 10:57:30
【问题描述】:

我正在尝试构建一个用于将数据从 CSV 导入数据库的表单。 现在,我正在努力完成以下任务:

根据应将数据导入到的表,我们在数据库中有许多列: DB_COL_AMOUNT = X

CSV 文件可以有不同数量的“列”。 它可能比我们的数据库表有更多或更少的列。 CSV_COL_AMOUNT = Y

现在,我想要一个 DataGridView,它应该显示 CSV 文件的内容。 这部分很好,可以按预期工作。

如果我的 CSV 文件中有三个“列”,我将在我的 DataGrid 中获得三个列 - 并且还取决于 CSV 文件是否有标题行,我会将标题的值作为列标题用于 DataGridView。

现在,我需要一些魔法:

我已经想通了,如何将 ComboBox 元素与 DataGrid 视图的 ColumnHeder 结合起来 - 以获得 ColumnHeader 名称的选择。

我想要这个,将 DataGridView 的列分配给我的数据库中的列。

f.e.:

数据库:

name | surename | birthdate | postalcode |

数据网格视图:

col1 | col2 | col3 | col4 |

此时,CSV文件的结构可能与我们数据库中的结构不同->我需要指定,应该将哪一列插入到表的哪一列中。

现在,我有了表的名称,可以做作业了:

col1 => surename, col2 => name, col3 => postalcode, col4 => birthdate

为此,我找到了以下代码:

List<string> ColumnHeaders = new List<string>();

using (SQLiteConnection dbConnection = new SQLiteConnection("Data Source=" + GetDBFile))
{
   try
   {
      dbConnection.Open();
   }
   catch (Exception ex)
   { }

   string SQL = "PRAGMA table_info (`contacts`)";

   using (SQLiteCommand command = new SQLiteCommand(SQL, dbConnection))
   {
      try
      {
         SQLiteDataReader reader = command.ExecuteReader();

         while (reader.Read())
         {
            if(reader.GetString(1) != "id")
            {
               ColumnHeaders.Add(reader.GetString(1));
            }
         }
      }
      catch (Exception ex)
      {
         MessageBox.Show(ex.Message);
      }
   }
}

ComboBox comboBoxHeaderCell = new ComboBox();
comboBoxHeaderCell.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxHeaderCell.Visible = true;

foreach (string Header in ColumnHeaders)
{
    comboBoxHeaderCell.Items.Add(Header);
    comboBoxHeaderCell.Text = Header;
 }

dataGridView1.Controls.Add(comboBoxHeaderCell);
comboBoxHeaderCell.Location = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
comboBoxHeaderCell.Size = this.dataGridView1.Columns[0].HeaderCell.Size;

这绝对可以正常工作: 如果我知道,我需要创建多少列。 但由于我不知道 DataGridView 将有多少列,直到用户导入 csv 文件。

有没有人有想法,我怎样才能让它工作?

我已尝试插入此部分

ComboBox comboBoxHeaderCell = new ComboBox();
comboBoxHeaderCell.DropDownStyle = ComboBoxStyle.DropDownList;
comboBoxHeaderCell.Visible = true;

foreach (string Header in ColumnHeaders)
{
    comboBoxHeaderCell.Items.Add(Header);
    comboBoxHeaderCell.Text = Header;
 }

 dataGridView1.Controls.Add(comboBoxHeaderCell);
 comboBoxHeaderCell.Location = this.dataGridView1.GetCellDisplayRectangle(0, -1, true).Location;
 comboBoxHeaderCell.Size = this.dataGridView1.Columns[0].HeaderCell.Size;

进入一个 for 循环并计算生成的 DataGrid 的 columnNumber - 但选择只会为第一列创建。

我想,我需要更改 ComboBox 元素的名称,但我不能通过使用 counter-Var 或类似的东西来做到这一点。

【问题讨论】:

    标签: c# datagridview combobox datagrid csv-import


    【解决方案1】:

    抱歉 - 我直接在答案中找到了解决方案,在那里我找到了获取列标题的 ComboBox 选择的解决方案。

    修改this MSDN discussion 中给出的代码后,它应该对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-30
      • 2020-06-13
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多