【问题标题】:Table created dynamically and DataGridView动态创建的表和 DataGridView
【发布时间】:2012-08-28 11:20:06
【问题描述】:

在我的 Windows 窗体中,我必须创建一个由用户名(在文本框中)指定的表(在运行时),然后将此表用作 DataGridView 的数据源。最后,必须将填好的表插入到数据库中。我尝试通过两种方式解决这个问题:

  1. 我创建了一个 DataTable,指定了它的列等,但我无法将 DataTable 插入数据库,即使使用 SqlDataAdapter.Update() - 可能我误用了 DataAdapter。我与C# 合作了1 周,我并不完全理解DataSets、TableAdapters 等的想法。所以我决定使用SQL 命令。

  2. 使用ExecuteNonQuery() 我已经在我的数据库中创建了表,然后我意识到我必须使用魔术 SqlDataAdapter,我放弃了。

我想以同样的方式管理DataGridView 的内容,就像使用配置向导将DataTable 添加到DataSet 的情况一样。

我知道,我可以跳过 DataGridView,创建文本框列表、扩展此列表的按钮,以及在确认按钮后将所有字段插入我的表格的功能,但我的主管希望 DataGridView

我无法在此处粘贴代码,因为我在公司计算机上只留下了源代码的副本。如果有必要,我明天会在这里粘贴我的代码。

【问题讨论】:

    标签: c# datagridview dataset sqldataadapter


    【解决方案1】:

    尝试使用RowValidatingCellValidating的方法DataGridView

      private void dataGridViewEnterTab1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            if (dataGridViewEnterTab1.IsCurrentRowDirty) // Trigger when the row is dirty or has changed
            {
                string userName = dataGridViewEnterTab1["UserNameCol", e.RowIndex].EditedFormattedValue.ToString();
    
                //...Your SqlDataAdapter or codes
            }
        }
    

    我希望它对这个想法有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 2014-05-08
      • 2021-03-14
      • 2012-05-03
      • 2018-08-12
      相关资源
      最近更新 更多