gpt4 book ai didi

c# - 使用 sql 查询结果填充 datagridview

转载 作者:IT王子 更新时间:2023-10-29 04:18:39 26 4
gpt4 key购买 nike

我正在尝试显示查询结果,但我总是得到一个空白的数据网格。就好像数据本身是不可见的

这是我的代码:

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

这段代码有什么问题?

最佳答案

您的代码已修复。接下来忘记绑定(bind)源

 var select = "SELECT * FROM tblEmployee";
var c = new SqlConnection(yourConnectionString); // Your Connection String here
var dataAdapter = new SqlDataAdapter(select, c);

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

关于c# - 使用 sql 查询结果填充 datagridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18113278/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com