我的页面中有一个gridview,并在其中显示了一些搜索结果。这是我的做法:
int row = -1;
int.TryParse(e.CommandArgument as string, out row);
GridViewRow gdrow = instCourses.Rows[row];
// *** Get the underlying data item - in this case a DataRow
DataRow dr = ((DataTable)this.instCourses.DataSource).Rows[gdrow.DataItemIndex];
// *** Retrieve our context
string courseCode = dr["CourseCode"].ToString();
string courseNumber = dr["CourseNumber"].ToString();
string term = dr["Term"].ToString();
SqlConnection con = new SqlConnection();
con.ConnectionString = Userfunctions.GetConnectionString();
con.Open();
SqlCommand cmd=new SqlCommand("SELECT RegisterTable.StudentID, StudentTable.Name, StudentTable.Surname FROM RegisterTable JOIN StudentTable ON RegisterTable.StudentID = StudentTable.ID WHERE RegisterTable.CourseCode = @courseCode AND RegisterTable.Term = @term AND RegisterTable.CourseNumber = @courseNumber",con);
cmd.Parameters.AddWithValue("@courseCode", courseCode);
cmd.Parameters.AddWithValue("@courseNumber", courseNumber);
cmd.Parameters.AddWithValue("@term", term);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
studList.DataSource = dt;
studList.DataBind();
Label1.Visible = true;
Label1.Text = "Students who are registered to "+courseCode+ " " + courseNumber +" are listed below:";
但是我之前已经完成了相同的查询,并将结果保存在一个列表中,假设列表的名称是 searchResults。我的问题是,我可以使用该列表中的项目来填充gridview,而不是再次查询数据库来填充表格吗?或者我应该使用任何其他东西而不是 gridview 来做到这一点?
谢谢