DataGridView does not display DataTable

I have the following code, which I think should bind the DataTable to the DataGridView, but the DataGridView is showing blank. The DataTable definitely has rows, so my guess is that I am not binding the DataSource correctly in some way. Does anyone see what is wrong:

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 just gets a handle to the database I am fetching data from.
  • The next line is a class to read from the same db - in particular, it provides a GetDataTable method with the return of the data table that I intend to put in the DataGridView.
  • The next line is not interesting ...
  • 4th line tries to capture the DataTable - QuickWatch indicates it works ...
  • On the final line, I assume I messed up ... I understand that this binds the DataTable to the DataGridView GUI, but nothing appears.

Any thoughts?

0


a source to share


5 answers


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

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 kludge hack, but it works. Suggestions on how to clean up this mess are more than welcome.

Brian

0


a source


Try to bind the DataGridView directly to the BindingSource rather than the BindingSource data source:



this.dataGridView1.DataSource = bindingSource;

      

+2


a source


You need to attach your BindingSource to your grid before you get your 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();

      

+1


a source


Along with the above solutions, the datamember property "bindingSource" is also fixed. as:

bindingSource.DataMember = yourDataSet.DataTable;

      

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

Good luck.

+1


a source


DataGridView

takes a DataTable

as base. Columns DataTable

retain their type as a property.

If this type is an interface, all you see are empty cells.

0


a source







All Articles