DataGridView不显示DataTable

发布于 2024-07-21 00:39:00 字数 736 浏览 4 评论 0原文

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

有什么想法吗?

I've got the following code which I think ought to be binding a DataTable to a DataGridView, but the DataGridView shows up empty. The DataTable definately has rows, so I assume that I am binding the DataSource incorrectly some how. Does anyone see what is wrong with this:

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;
  • The first line simply gets a handle to the DB that I'm pulling data from.
  • The next line is a provides a class for reading from that same db - in particular it exposes the GetDataTable method with returns the data table that I intend to put into the DataGridView.
  • The next line is uninteresting...
  • The 4th line attempts to grab the DataTable - QuickWatch indicates that this is working...
  • The final line is where I assume i've screwed up...my understanding is that this binds the DataTable to the DataGridView GUI, but nothing shows up.

Any thoughts?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

寒尘 2024-07-28 00:39:00

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

this.dataGridView1.DataSource = bindingSource;

Try binding the DataGridView directly to the BindingSource, and not the BindingSource's DataSource:

this.dataGridView1.DataSource = bindingSource;
我的痛♀有谁懂 2024-07-28 00:39:00

在获取数据表之前,您需要将 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();

You need to attach your BindingSource to your grid, before you get the data table.

Try switching the last two lines of code:

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();
弥繁 2024-07-28 00:39:00

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

bindingSource.DataMember = yourDataSet.DataTable;

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

祝你好运。

Along with above solutions also fix the "bindingSource" datamember property. like:

bindingSource.DataMember = yourDataSet.DataTable;

I had the same problem with sql database and datagrid view. After a great deal of trouble I found out that I've forgot to set dataMember property of my binding source.

best of luck.

疯了 2024-07-28 00:39:00

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

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

布莱恩

None of this worked for me, though it all seemed like good advice. What I ended up doing was the biggest, worst hack on earth. What I was hoping to accomplish was simply to load a DB table from a SQLite db and present it (read only, with sortable columns) in the DataGridView. The actual DB would be programmatically specified at runtime. I defined the DataSet by adding a DataGridView to the form and used the Wizards to statically define the DB connection string. Then I went into the Settings.Designer.cs file and added a set accessor to the DB Connection string property:

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

This is a klugey hack, but it works. Suggestions about how to clean this mess up are more than welcome.

brian

甜味拾荒者 2024-07-28 00:39:00

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

this.dataGridView1.AutoGenerateColumns = true;

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

This worked for me (put in Form_Load):

this.dataGridView1.AutoGenerateColumns = true;

Apparently it's the only way because AutoGenerateColumns isn't listed anywhere on the grid's property sheet.

兲鉂ぱ嘚淚 2024-07-28 00:39:00

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

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

DataGridView takes a DataTable as a basis. The DataTable Columns save their Type as a property.

If this type is an Interface all you would see are empty cells.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文