another beginner problem. Why isn't the following code with an asp.net page not working?
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
}
GridView is the GridView control on that asp page. However, no grid shows up at all. It's both enabled and visible. Plus when I debug, the GridView.Rows.Count is 0. I always assumed you can just add generic Lists and all classes implementing IList as a DataSource, and the gridview will then display the content automatically? Or is the reason here that it's been done in the page_load event handler. and if, how can I fill a grid without any user interaction at startup?
Thanks again.
You should call DataBind().
You forgot to call the GridView's .DataBind() method. This is what will link the control to its data source and load the results.
Example:
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Teststring");
this.GridView.DataSource = list;
this.GridView.DataBind();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With