SQL Server数据库中的数据未显示在DataGridView中 [英] Data from SQL Server databse not showing in a DataGridView
本文介绍了SQL Server数据库中的数据未显示在DataGridView中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对于下面的populateDataGridView()
方法,我有一个名为dtGridView的DataGridView实例和一个名为bndSource的BindingSource实例.
我可以看到数据已写入bndSource,但是数据没有从bndSource传递到dtGridView.
我的问题是:
为什么bndSource中的数据不能显示在dtGridView中?
最好的问候,
KL
For the populateDataGridView()
-method below I have an instance of DataGridView called dtGridView and an instance of a BindingSource called bndSource.
I can see that the data is written to bndSource, but the data does not get from the bndSource to the dtGridView.
My question is:
Why do the data from bndSource not display in dtGridView ?
Best regards,
KL
private void populateDataGridView()
{
string connectionString = "Data Source=MY-PC;Initial Catalog=TestDataBase;Integrated Security=SSPI";
string sqlQuery = "SELECT * From dataTable";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter dataAdapter = new SqlDataAdapter(sqlQuery, connection);
DataTable dataTable = new DataTable();
connection.Open();
dataAdapter.Fill(dataTable);
connection.Close();
bndSource.DataSource = dataTable;
dtGridView.DataSource = bndSource;
}
推荐答案
嗨
请尝试使用以下代码替换您的代码:
Hi
Please try replace your code with this one below:
string connectionString = "Data Source=MY-PC;Initial Catalog=TestDataBase;Integrated Security=SSPI";
string sqlQuery = "SELECT * From dataTable";
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand dbCmd = new SqlCommand(sqlQuery, connection);
connection.Open();
DataSet ds = new DataSet();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
dbAdapter.SelectCommand = dbCmd;
dataAdapter.Fill(ds,"dataTable");
dtGridView.DataSource = ds.Tables["dataTable"];
connection.Close();
设置DataSource属性后,您需要调用GridView.DataBind()
After setting the DataSource property, you need to call GridView.DataBind()
这篇关于SQL Server数据库中的数据未显示在DataGridView中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文