大家帮我看看为什么GRIDVIEW显示不出来
我在页面上拉了一个gridview进去,然后在后台写上代码
代码如下:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = "Data Source=zonghua;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=sa ";
string commandString = "select * from Customers ";
SqlConnection conn = new SqlConnection(connectionString);
try
{
conn.Open();
SqlDataAdapter dataAdapter = new SqlDataAdapter(commandString, connectionString);
DataSet dataset = new DataSet();
dataAdapter.Fill(dataset);
DataTable datatable = dataset.Tables[ "Customers "];
GridView1.DataSource = datatable;
GridView1.DataBind();
}
catch(Exception exp)
{
Response.Write(exp.Message);
}
finally
{
conn.Close();
}
}
}
我用的是SQL自带数据库northwind中的cunstomers表,为什么运行之后页面上就是显示不出来gridview,大家帮我看看哈
------解决方案--------------------是不是没有数据?
------解决方案--------------------dataset.Tables[ "Customers "];
错了,dataset.Tables[0];
------解决方案--------------------修改为
DataTable datatable = dataset.Tables[0];
GridView1.DataSource = datatable;
试试看
------解决方案----------