DataGridView不显示DataTable [英] DataGridView does not display DataTable
问题描述
我有以下代码,我认为应该将DataTable绑定到DataGridView,但是DataGridView显示为空。 DataTable肯定有行,因此我假设我以某种方式错误地绑定了DataSource。没有人看到这是怎么回事:
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;
- 第一行只是获得了我所在数据库的句柄
- 下一行是提供一个用于从同一数据库读取的类-特别是它公开了GetDataTable方法,并返回了我打算放入DataGridView的数据表。
- 下一行没意思...
- 第四行尝试获取DataTable-QuickWatch表示这行得通...
- 最后一行是我认为自己搞砸了的地方...我的理解是,这会将DataTable绑定到DataGridView GUI,但是什么都没有显示。
- 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.
有什么想法吗?
推荐答案
尽管这一切似乎都是很好的建议,但对我来说都没有用。我最终要做的是地球上最大,最糟糕的黑客事件。我希望完成的只是从SQLite数据库加载数据库表,并在DataGridView中显示它(只读,带有可排序的列)。实际的数据库将在运行时以编程方式指定。我通过将DataGridView添加到表单中来定义DataSet,并使用向导静态定义了数据库连接字符串。然后我进入Settings.Designer.cs文件,并向数据库连接字符串属性添加 set
访问器:
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;
}
}
}
}
此是一个klugey hack,但是可以。关于如何清理这种混乱的建议非常受欢迎。
This is a klugey hack, but it works. Suggestions about how to clean this mess up are more than welcome.
布莱恩
这篇关于DataGridView不显示DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!