DataGridView不显示DataTable

问题描述 投票:0回答:6

我有以下代码,我认为应该将 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 中的数据表。
  • 下一行无趣...
  • 第四行尝试获取数据表 - QuickWatch 表明此操作正在运行...
  • 最后一行是我认为我搞砸了的地方...我的理解是,这将 DataTable 绑定到 DataGridView GUI,但没有显示任何内容。

有什么想法吗?

c# sqlite datagridview datatable system.data.sqlite
6个回答
2
投票

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

this.dataGridView1.DataSource = bindingSource;

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();

1
投票

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

bindingSource.DataMember = yourDataSet.DataTable;

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

祝你好运。


0
投票

这些对我来说都不起作用,尽管这看起来都是好建议。我最终所做的是地球上最大、最糟糕的黑客行为。我希望完成的只是从 SQLite 数据库加载数据库表并将其呈现在 DataGridView 中(只读,带有可排序的列)。实际的数据库将在运行时以编程方式指定。我通过向表单添加 DataGridView 来定义数据集,并使用向导静态定义数据库连接字符串。然后我进入 Settings.Designer.cs 文件并向数据库连接字符串属性添加一个

set
访问器:

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

这是一个笨拙的技巧,但它确实有效。非常欢迎有关如何清理这个混乱的建议。

布莱恩


0
投票

DataGridView
DataTable
作为基础。
DataTable
列将其类型保存为属性。

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


0
投票

这对我有用(放入 Form_Load 中):

this.dataGridView1.AutoGenerateColumns = true;

显然这是唯一的方法,因为 AutoGenerateColumns 未在网格属性表的任何位置列出。

© www.soinside.com 2019 - 2024. All rights reserved.