I'm trying to fill DataGridView
from database with table data. Here is how:
adpt = new System.Data.SqlClient.SqlDataAdapter(query, conn);
DataTable dt = new DataTable();
adpt.Fill(dt);
BindingSource bs = new BindingSource();
bs.DataSource = dt;
dataGridView1.ReadOnly = true;
dataGridView1.DataSource = bs;
adpt.Update(dt);
But all I see is empty DataGridView
. Can some show me where I'm wrong or what I'm missing?
The basic Function to fill the data gridview is below ,
void FillData()
{
// 1
// Open connection
using (SqlCeConnection c = new SqlCeConnection(
Properties.Settings.Default.DataConnectionString))
{
c.Open();
// 2
// Create new DataAdapter
using (SqlCeDataAdapter a = new SqlCeDataAdapter(
"SELECT * FROM Animals", c))
{
// 3
// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);
// 4
// Render data onto the screen
dataGridView1.DataSource = t;
}
}
}
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.