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; 
    }
}

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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();
    }
}

Method 2

Change your code to:

protected void Page_Load(object sender, EventArgs e)
{
    lstview = taview.GetData().ToList();
    GridAllStore.DataSource = lstview; 
    GridAllStore.DataBind();
}

And change your GridView markup to:

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

Noticed that it is now AutoGenerateColumns="True" as this will show the data and generate the columns. You might need to customise what is being shown though. To do this, since you dont really know what you are doing just now, switch to the design view and you can edit the gridview template.

Check out this post for some help with customizing the columns and data that you output. http://msdn.microsoft.com/en-us/library/bb288032.aspx

Method 3

Have you tried adding following line immediately after setting the Datasource ?

GridAllStore.DataBind();


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x