Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# DataGridView不显示DataTable_C#_Sqlite_Datagridview_Datatable_System.data.sqlite - Fatal编程技术网

C# DataGridView不显示DataTable

C# DataGridView不显示DataTable,c#,sqlite,datagridview,datatable,system.data.sqlite,C#,Sqlite,Datagridview,Datatable,System.data.sqlite,我有以下代码,我认为应该将DataTable绑定到DataGridView,但DataGridView显示为空。DataTable肯定有行,因此我假设我以某种方式错误地绑定了数据源。有人知道这有什么问题吗: DataBase db = new DataBase(re.OutputDir+"\\Matches.db"); MatchDBReader reader = new MatchDBReader(db.NewConnection()); BindingSource bindingSourc

我有以下代码,我认为应该将DataTable绑定到DataGridView,但DataGridView显示为空。DataTable肯定有行,因此我假设我以某种方式错误地绑定了数据源。有人知道这有什么问题吗:

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;
  • 第一行只是获取我从中提取数据的DB的句柄
  • 下一行是提供了一个类,用于从同一个db读取数据,特别是它公开了GetDataTable方法,并返回了我打算放入DataGridView的数据表
  • 下一行没意思
  • 第四行尝试获取数据表-QuickWatch表示这正在工作
  • 最后一行是我假设我搞砸了的地方…我的理解是,这将DataTable绑定到DataGridView GUI,但什么都没有显示

有什么想法吗?

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

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

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

this.dataGridView1.DataSource = bindingSource;

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

this.dataGridView1.DataSource = bindingSource;

除了上述解决方案之外,还修复了“bindingSource”datamember属性。比如:

bindingSource.DataMember = yourDataSet.DataTable;
我对sql数据库和datagrid视图也有同样的问题。经过大量的麻烦之后,我发现我忘记设置绑定源的dataMember属性


祝您好运。

与上述解决方案一起还修复了“bindingSource”datamember属性。比如:

bindingSource.DataMember = yourDataSet.DataTable;
我对sql数据库和datagrid视图也有同样的问题。经过大量的麻烦之后,我发现我忘记设置绑定源的dataMember属性


祝你好运。

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

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;
            }
        }
    }
}
这是一个克鲁基黑客,但它的工作。关于如何清理这个烂摊子的建议非常受欢迎


布莱恩

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

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;
            }
        }
    }
}
这是一个克鲁基黑客,但它的工作。关于如何清理这个烂摊子的建议非常受欢迎


brian

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


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

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


如果该类型是一个接口,那么您将看到的都是空单元格。

这实际上就是我在这行代码中所做的:bindingSource.DataSource=reader.GetDataTable();您正在设置bindingSource的datasoure属性。。。不是数据源的DataMember属性。。。这是有区别的。我在一个古老的Q中意识到了这一点,但我偶然发现了它。我在尝试您的建议时出错,VS抱怨“无法将System.Data.DataTable类型的值转换为字符串”@Dan try.ToString()方法,类似bindingSource.DataMember=yourDataSet.DataTable.ToString();这就是我在这里使用这一行所做的:bindingSource.DataSource=reader.GetDataTable();您正在设置bindingSource的datasoure属性。。。不是数据源的DataMember属性。。。这是有区别的。我在一个古老的Q中意识到了这一点,但我偶然发现了它。我在尝试您的建议时出错,VS抱怨“无法将System.Data.DataTable类型的值转换为字符串”@Dan try.ToString()方法,类似bindingSource.DataMember=yourDataSet.DataTable.ToString();