数据网格视图不显示列/数据
本文关键字:数据 显示 视图 数据网 网格 | 更新日期: 2023-09-27 18:31:44
我正在尝试将一些数据加载到DataGridView中,但是DataGridView没有显示数据(即使它在调试模式下说它有项目)
protected void PopulateGrid()
{
string dataSource = "test.db";
String connectionString = "Data Source=" + dataSource;
String selectCommand = "SELECT * FROM test_run";
SQLiteDataAdapter dataAdapter =
new SQLiteDataAdapter(selectCommand, connectionString);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.Refresh();
}
或者它不起作用并出现错误:"DataReader 已在此命令上处于活动状态"
protected void PopulateGrid()
{
SQLiteConnection SQLconnect = new SQLiteConnection();
SQLiteCommand SQLcommand = default(SQLiteCommand);
SQLiteDataAdapter SQLAdapt = new SQLiteDataAdapter();
SQLiteDataReader SQLreader = default(SQLiteDataReader);
DataTable db = new DataTable();
SQLconnect.ConnectionString = "Data Source=test.db;MultipleActiveResultSets=True";
SQLconnect.Open();
SQLcommand = SQLconnect.CreateCommand();
SQLcommand.CommandText = "SELECT * FROM test_run;";
SQLreader = SQLcommand.ExecuteReader();
SQLAdapt.SelectCommand = SQLcommand;
SQLAdapt.Fill(db);
dataGridView1.DataSource = db.DataSet;
dataGridView1.Refresh();
dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
//or if you want to use DataTable as DataSource for your grid
dataGridView1.DataSource = ds.Tables[0];