填充用sql查询结果一个DataGridView [英] Populate a datagridview with sql query results

查看:128
本文介绍了填充用sql查询结果一个DataGridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想present查询结果,但我不断收到一个空数据网格。
这就像数据本身是不可见的。

I'm trying to present query results, but I keep getting a blank data grid. It's like the data itself is not visible

下面是我的code:

 private void Employee_Report_Load(object sender, EventArgs e)
 {
     string select = "SELECT * FROM tblEmployee";
     Connection c = new Connection();
     SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string
     SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

     DataTable table = new DataTable();
     table.Locale = System.Globalization.CultureInfo.InvariantCulture;
     dataAdapter.Fill(table);
     bindingSource1.DataSource = table;

     dataGridView1.ReadOnly = true;        
     dataGridView1.DataSource = bindingSource1;
}

有什么不对的code?

What's wrong with this code?

推荐答案

下面是你的code搞掂。接下来忘记的BindingSource

Here's your code fixed up. Next forget bindingsource

 string select = "SELECT * FROM tblEmployee";
 Connection c = new Connection();
 SqlDataAdapter dataAdapter = new SqlDataAdapter(select, c.con); //c.con is the connection string

 SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
 DataSet ds = new DataSet();
 dataAdapter.Fill(ds);
 dataGridView1.ReadOnly = true; 
 dataGridView.DataSource = ds.tables[0];

这篇关于填充用sql查询结果一个DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆