c#(winform)中如何用sqldatareader数据绑定到datagridview
StringconnectionString="DataSource=local;InitialCatalog=TotalManagement;IntegratedSec...
String connectionString = "Data Source=local;Initial Catalog=TotalManagement;Integrated Security=True";
string selectCommand = "select ID,Name,LawRank,ExecuteDate,ContentType FROM LawsAndRegulations";
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(selectCommand, cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dataGridViewLaw.DataSource = dr;
dataGridViewLaw.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
这样在datagridview 没有任何显示, 展开
string selectCommand = "select ID,Name,LawRank,ExecuteDate,ContentType FROM LawsAndRegulations";
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand(selectCommand, cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
dataGridViewLaw.DataSource = dr;
dataGridViewLaw.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
这样在datagridview 没有任何显示, 展开
2个回答
展开全部
String connectionString = "Data Source=local;Initial Catalog=TotalManagement;Integrated Security=True";
string selectCommand = "select ID,Name,LawRank,ExecuteDate,ContentType FROM LawsAndRegulations";
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlDataAdapter sda = new SqlDataAdapter(selectCommand , cn);
cn.Open();
DataSet dr = cmd.DataAdapter();
sda.Fill(dr);
dataGridViewLaw.DataSource = dr;
dataGridViewLaw.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
string selectCommand = "select ID,Name,LawRank,ExecuteDate,ContentType FROM LawsAndRegulations";
using (SqlConnection cn = new SqlConnection(connectionString))
{
SqlDataAdapter sda = new SqlDataAdapter(selectCommand , cn);
cn.Open();
DataSet dr = cmd.DataAdapter();
sda.Fill(dr);
dataGridViewLaw.DataSource = dr;
dataGridViewLaw.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询