Reputation: 10777
I have a gridview in my page and i show some search results in it. Here is how i do it:
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:";
But i have already done the same query before, and kept the results in a list, let's say the name of the list is searchResults. My question is, instead of querying the database again to fill the table, can i use the items in that list to fill the gridview? Or should i use any other thing instead of gridview to do this?
Thank you
Upvotes: 1
Views: 2363
Reputation: 11433
I misunderstood what you were asking. You can use a List<T>
of objects as your datasource in the same way that you are using the DataTable in your example above. You just have to make sure that you expose the class-variables that you want your GridView to see as public properties - like this:
public class Course
{
public string courseCode { get; set; }
public string courseName { get; set; }
public string courseNumber { get; set; }
//...the rest of your class code...//
}
If you are going to be reusing that DataTable, I would just store it in the Session. So where you set it to your datasource:
DataTable dt = new DataTable();
da.Fill(dt);
studList.DataSource = dt;
Also, add it to the Session:
Session["mySearchResults"] = dt;
Then you can reference that to populate your other list:
myOtherList.DataSource = (DataTable)Session["mySearchResults"];
myOtherList.DataBind();
Note that if this DataTable is really huge, or if you're going to have a lot of users accessing this application at once, this approach might bring problems (resource-wise on your web server). But it works as a simple solution.
Upvotes: 1
Reputation: 2120
You can use your list (IEnumerable) as the datasource of your gridview. No need to query again
studList.DataSource = searchResults;
studList.DataBind();
Upvotes: 1