我正在尝试使用以下代码使用存储过程填充gridview,但是发生的是gridview没有显示任何内容。
SqlConnection myConnectiona = new SqlConnection("user id=HOME-PC\\HOME;" +
"password=password;server=HOME-PC\\SQLEXPRESS;" +
"Trusted_Connection=yes;" +
"database=tabrem; " +
"connection timeout=30");
SqlCommand pro = new SqlCommand("[dbo].[doctor]", myConnectiona);
pro.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(pro);
DataTable dt = new DataTable();
try
{
myConnectiona.Open();
da.Fill(dt);
dataGridView1.DataSource = dt;
}
catch (Exception w)
{
throw;
}
finally
{
if (myConnectiona.State == ConnectionState.Open)
myConnectiona.Close();
}
答案 0 :(得分:1)
设置数据源后:
dataGridView1.DataSource = dt;
dataGridView1.DataBind();
编辑:
在这种情况下,您需要使用BindingSource:
bindingSource.DataSource = dt;
dataGridView1.DataSource = bindingSource;
https://msdn.microsoft.com/en-us/library/fbk67b6z%28v=vs.110%29.aspx
答案 1 :(得分:0)
您需要将数据表绑定到gridview,如下所示:
<div class="header"></div>