简体   繁体   中英

Datagridview not populating data from SQL Server CE

I'm having an issue populating my data into a datagridview control. Here is my code.

string sqlQuery = "SELECT * FROM Table;";
string source = "Data Source=E:\\Documents\\Database\\Database.sdf;" +
                "Password=password;Persist Security Info=False;";

SqlCeDataAdapter dataAdapter = new SqlCeDataAdapter(sqlQuery, source);
SqlCeCommandBuilder commBuilder = new SqlCeCommandBuilder(dataAdapter);

DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;

dataAdapter.Fill(table);

dbBindSource.DataSource = table;
dbGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
dbGridView.ReadOnly = true;
dbGridView.DataSource = dbBindSource;

This is running on form load to no avail. Anyone?

DataTable dataT;
BindingSource bindS;

using (SqlCeConnection yourConnection = new SqlCeConnection("Data Source=|DataDirectory|\\YourDatabase.sdf")) {
    dataT = new DataTable();
    bindS = new BindingSource();    

    string query = "SELECT * FROM table01";
    SqlCeDataAdapter dA = new SqlCeDataAdapter(query, yourConnection);
    SqlCeCommandBuilder cBuilder = new SqlCeCommandBuilder(dA);
    dA.Fill(dataT);

    bindS.DataSource = dataT;
    DataGridView1.DataSource = bindS;
}

Resurrecting this with a working example because of the pages rank on google and because it was never answered.

Path is relative to your executing directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM