Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GridView in ASP.NET is not displaying with or without data

I'm adding a GridView & then showing data in it from a SQL Server database. The problem is that the GridView is not displaying in the browser with or without data.

Here is my code:

<asp:GridView ID="GridAllStore"  runat="server" AutoGenerateColumns="False" Width="100%"  ViewStateMode="Enabled">

public partial class AdminPanel : System.Web.UI.Page
{
    storelocatorDataSetTableAdapters.storedbTableAdapter tastore = new storelocatorDataSetTableAdapters.storedbTableAdapter();
    storelocatorDataSetTableAdapters.View_1TableAdapter taview = new storelocatorDataSetTableAdapters.View_1TableAdapter();

    List<storelocatorDataSet.storedbRow> lststore = new List<storelocatorDataSet.storedbRow>();
    List<storelocatorDataSet.View_1Row> lstview = new List<storelocatorDataSet.View_1Row>();
    protected void Page_Load(object sender, EventArgs e)
    {
        lstview = taview.GetData().ToList();
        GridAllStore.DataSource = lstview; 
    }
}
like image 903
Fahim Ahmed Avatar asked Apr 23 '12 21:04

Fahim Ahmed


People also ask

Why is grid view not showing?

Once in the developer setting, tap the update button on the top and installed the Google Meet grid view extension update. If the manually installed gird view update does not work, users can try to disabling and re-enabling it on Google Chrome. Type chrome://extensions on the browser's address bar and hit the Enter key.

How do I check if GridView is empty?

You should check this by directly querying the data binding source of the gridview (see if the actual list that's grid view bound to is empty or not). Show activity on this post. Hi @Canavar, just wanted to know if GridView. DataBind(); is possible without binding to any datasource ?


1 Answers

I think the problem is that you haven't defined any columns to display. You have to explicitly define the columns when you set AutoGenerateColumns to false.

To make sure that the basics are working set AutoGenerateColumns to true:

<asp:GridView ID="GridAllStore"  runat="server" AutoGenerateColumns="true" Width="100%"  ViewStateMode="Enabled">

With AutoGenerateColumns set to true, the datasource assigned, and DataBind() called, you should start seeing some data. Once you start seeing the data, you can define the specific columns you want to display.

Since you only need to bind the grid on the first page load, utilize the !Page.IsPostBack condition:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GridAllStore.DataSource = lstview;
        GridAllStore.DataBind();
    }
}
like image 179
James Johnson Avatar answered Oct 02 '22 14:10

James Johnson