1

我在将数据填充到 datagridview 控件时遇到问题。这是我的代码。

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;

这在表单加载时运行无济于事。任何人?

4

1 回答 1

0
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;
}

由于页面在谷歌上的排名并且从未得到回答,因此通过一个工作示例来恢复这一点。

路径相对于您的执行目录。

于 2015-12-01T13:34:03.570 回答