8

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; 
    }
}
4
  • 7
    I think you forgot to call GridAllStore.DataBind() Commented Apr 23, 2012 at 21:48
  • You either have to set AutoGenerateColumns to true ot provide the columns as Bound- or TemplateFields that you want to show. You must DataBind irt either way. Commented Apr 23, 2012 at 21:50
  • Is that your complete markup for the GridView? Commented Apr 23, 2012 at 21:50
  • Still data is not displaying . Actually gridview is not displaying in browser from the beginning . I thought adding data source will solve the problem . :( Commented Apr 23, 2012 at 21:51

3 Answers 3

19

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Thanks ! :) I have just done that . It worked . But I really don't have any idea how to remove autogenereated columns . Can you help me please ?
If you open up the .aspx page in design view mode, you can click on the GridView control and edit it. It allows you to select what columns you want to display. Alternatively, you can use custom TemplateFields. Maybe have a look here for some help with it? msdn.microsoft.com/en-us/library/bb288032.aspx
If you need anymore help with that, give us a yell!
1

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

GridAllStore.DataBind();

6 Comments

Have you confirmed that you have data in lstview ?
And AutoGenerateColumns is set to true too ? Also check grid's visibility and it's parent control's visibility. Do a view source too and see if grid is there but not showing up for some reason.
It worked now after setting the AutogeneratedColumns to true . :D But , now I want to remove the autogeneratedcolumns. How van I do that ?
You can turn off the property and add your own columns. Here's a tutorial to get you started : c-sharpcorner.com/uploadfile/61b832/…
I just checked your tuto . It gives me error like this : Error Creating Control - GridAllStoreLiteral content ('<asp :BoundField HeaderText="Store" DataField="name" __designer:mapid="81" /> <asp :BoundField HeaderText="Contact No." DataField="contact" __designer:mapid="82" /> <asp :BoundField HeaderText="Address" DataField="address" __designer:mapid="83" />') is not allowed within a 'System.Web.UI.WebControls.DataControlFieldCollection'.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.