DataGridView does not display DataTable

狂风中的少年 提交于 2019-12-08 12:06:14

问题


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?


回答1:


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

this.dataGridView1.DataSource = bindingSource;



回答2:


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



回答3:


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.




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/900112/datagridview-does-not-display-datatable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!