Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET gridview binding doesn't work / control doesn't show up

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.

like image 659
noisecoder Avatar asked Apr 22 '26 22:04

noisecoder


2 Answers

You should call DataBind().

like image 111
Zachary Avatar answered Apr 25 '26 15:04

Zachary


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();
}
like image 31
TheTXI Avatar answered Apr 25 '26 15:04

TheTXI



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!