1

我有以下代码,我认为应该将 DataTable 绑定到 DataGridView,但 DataGridView 显示为空。DataTable 肯定有行,所以我假设我以某种方式错误地绑定了 DataSource。有谁看到这有什么问题:

DataBase db = new DataBase(re.OutputDir+"\\Matches.db");
MatchDBReader reader = new MatchDBReader(db.NewConnection());

BindingSource bindingSource = new BindingSource();
bindingSource.DataSource = reader.GetDataTable();

this.dataGridView1.DataSource = bindingSource.DataSource;
  • 第一行只是获取我从中提取数据的数据库的句柄。
  • 下一行是提供了一个用于从同一个数据库读取的类 - 特别是它公开了 GetDataTable 方法,并返回我打算放入 DataGridView 的数据表。
  • 下一行是无趣的...
  • 第 4 行尝试抓取 DataTable - QuickWatch 表明这正在工作......
  • 最后一行是我假设我搞砸了......我的理解是这将 DataTable 绑定到 DataGridView GUI,但没有显示任何内容。

有什么想法吗?

4

5 回答 5

2

尝试将 DataGridView 直接绑定到 BindingSource,而不是 BindingSource 的 DataSource:

this.dataGridView1.DataSource = bindingSource;
于 2009-05-22T22:52:19.603 回答
1

在获取数据表之前,您需要将 BindingSource 附加到网格。

尝试切换最后两行代码:

DataBase db = new DataBase(re.OutputDir+"\\Matches.db");
MatchDBReader reader = new MatchDBReader(db.NewConnection());
BindingSource bindingSource = new BindingSource();
this.dataGridView1.DataSource = bindingSource.DataSource;
bindingSource.DataSource = reader.GetDataTable();
于 2009-05-22T21:58:06.933 回答
1

除上述解决方案外,还修复了“bindingSource”数据成员属性。像:

bindingSource.DataMember = yourDataSet.DataTable;

我对 sql 数据库和 datagrid 视图有同样的问题。经过一番麻烦后,我发现我忘记设置绑定源的 dataMember 属性。

祝你好运。

于 2009-05-23T07:02:59.103 回答
0

这些都不适合我,尽管这一切似乎都是好的建议。我最终做的是世界上最大、最糟糕的黑客攻击。我希望完成的只是从 SQLite 数据库加载数据库表并将其呈现在 DataGridView 中(只读,带有可排序的列)。实际的数据库将在运行时以编程方式指定。我通过向表单添加 DataGridView 来定义 DataSet,并使用向导静态定义 DB 连接字符串。然后我进入 Settings.Designer.cs 文件并set为 DB Connection 字符串属性添加了一个访问器:

namespace FormatDetector.Properties {


    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
    internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {

        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.ApplicationScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]
        [global::System.Configuration.DefaultSettingValueAttribute("data source=E:\\workspace\\Test\\Matches.db;useutf16encoding=True")]
        public string MatchesConnectionString {
            get {
                return ((string)(this["MatchesConnectionString"]));
            }
            set
            {
                (this["MatchesConnectionString"]) = value;
            }
        }
    }
}

这是一个 klugey hack,但它有效。关于如何清理这个烂摊子的建议非常受欢迎。

布莱恩

于 2009-05-25T20:12:02.807 回答
0

DataGridView以aDataTable为基础。列DataTable将它们的类型保存为属性。

如果此类型是一个接口,您将看到的只是空单元格。

于 2012-03-15T17:56:07.493 回答