C# DataGridView 不显示 DataTable

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/900112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 22:09:35  来源:igfitidea点击:

DataGridView does not display DataTable

c#sqlitedatagridviewdatatablesystem.data.sqlite

提问by Brian Sweeney

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:

我有以下代码,我认为应该将 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;
  • 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.
  • 第一行只是获取我从中提取数据的数据库的句柄。
  • 下一行是提供一个用于从同一个数据库读取的类 - 特别是它公开了 GetDataTable 方法,并返回我打算放入 DataGridView 的数据表。
  • 下一行是无趣的......
  • 第 4 行尝试抓取 DataTable - QuickWatch 表明这是有效的...
  • 最后一行是我假设我搞砸的地方...我的理解是这将 DataTable 绑定到 DataGridView GUI,但没有显示任何内容。

Any thoughts?

有什么想法吗?

采纳答案by Brian Sweeney

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 setaccessor to the DB Connection string property:

这些都不适合我,尽管这一切似乎都是很好的建议。我最终做的是地球上最大、最糟糕的黑客攻击。我希望完成的只是从 SQLite 数据库加载一个数据库表并在 DataGridView 中显示它(只读,带有可排序的列)。实际的 DB 将在运行时以编程方式指定。我通过向窗体添加 DataGridView 来定义数据集,并使用向导静态定义数据库连接字符串。然后我进入 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;
            }
        }
    }
}

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

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

brian

布莱恩

回答by Robert Harvey

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

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

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

回答by BFree

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

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

this.dataGridView1.DataSource = bindingSource;

回答by Asad Malik

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

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

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.

我在 sql 数据库和 datagrid 视图中遇到了同样的问题。经过一番努力,我发现我忘记设置绑定源的 dataMember 属性。

best of luck.

祝你好运。

回答by Johannes

DataGridViewtakes a DataTableas a basis. The DataTableColumns save their Type as a property.

DataGridView以 aDataTable为基础。该DataTable列的类型保存为一个属性。

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

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