1
我只想顯示一些特定查詢的gridview。 我試過下面。 當查詢中傳遞了錯誤的參數時,此代碼顯示「無數據匹配」,但即使參數正確,也不顯示任何數據。 我是新來asp.net。請告訴我,有什麼重要的線,我錯過Gridview表不在asp.net中顯示webform
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();
}