Gridview表未在asp.net webform中显示

时间:2017-06-04 17:34:52

标签: c# asp.net visual-studio gridview webforms

我只想用一些特定的查询来显示gridview。 我试过在下面做。 在查询中传递错误参数时,此代码显示“无数据匹配”,但即使参数正确,也不显示任何内容。 我是asp.net的新手。请告诉我,重要的是什么,我很想念

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["Filter"] = "ALL";
            BindGrid();
        }
    }



private void BindGrid()
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString1"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        string query = "SELECT ContactName, City, Country, PostalCode FROM Customers";
        SqlCommand cmd = new SqlCommand("SELECT ContactName, City, Country, PostalCode FROM Customers WHERE Country='UK'" );
        cmd.Connection = con;
        sda.SelectCommand = cmd;
        sda.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

1 个答案:

答案 0 :(得分:0)

代码正在运行并从northwind数据库返回7行到gridview。

enter image description here                   

你的连接字符串在web.config文件中应该是这样的:

<connectionStrings>
      <add name="NorthwindConnectionString1" connectionString="server=.; database=northwnd; integrated security=true" providerName="System.Data.SqlClient"/>
</connectionStrings>