本文介绍了我想填写gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里有什么错误?
what error here?
private void fillgrid()
{
try
{
DataSet ds = new DataSet();
ds.Clear();
SqlConnection con = new SqlConnection(@" kkkk");
SqlCommand comm = new SqlCommand("select * from client", con);
SqlDataAdapter adapt = new SqlDataAdapter();
con.Open();
adapt.SelectCommand = comm;
adapt.Fill(ds);
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "client";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
推荐答案
try
{
using (SqlConnection con = new SqlConnection(YOUR CONNECTION STRING))
{
con.Open();
// Create new DataAdapter
using (SqlDataAdapter adapt = new SqlDataAdapter("select * from client", con))
{
// Use DataAdapter to fill DataTable
DataTable ds = new DataTable();
adapt.Fill(ds);
// fill your grid
dataGridView1.DataSource = ds;
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
这篇关于我想填写gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!