我实现了对我的嵌套gridview的搜索,并且All运行良好。但是当gridview加载时,它会在父表中显示重复的行。Select statement显示不正确的结果
正如你可以在图片中看到,还有AC107的CourseID下2本书。但是我的Gridview在课程中为每本教科书显示一行。我搞砸了这个选择语句,无论如何我改变它看看是否有任何工作,gridview不加载。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//i'm using a datatable for storing all the data
DataTable dt = new DataTable();
string query = "select * from Course inner join textBooks on textBooks.CourseID = Course.CourseID";
//wrapping in 'using' means the connection is closed an disposed when done
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
{
try
{
//fill the datatable with the contents from the database
adapter.Fill(dt);
}
catch
{
}
}
//save the datatable into a viewstate for later use
ViewState["allBooks"] = dt;
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
下面是我的数据表格的布局。
千万不要使用空的try-catch。 – LarsTech