GridView in ASP.NET does not display with or without data

I add a GridView and then show the data in it from the SQL Server database. The problem is that the GridView does not display 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; } } 
+6
source share
3 answers

I think the problem is that you did not define any columns to display. You must explicitly define columns if AutoGenerateColumns set to false.

To make sure the basics work, set AutoGenerateColumns to true:

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

With AutoGenerateColumns set to true, the assigned data source and DataBind() , you should start viewing some data. After you start browsing the data, you can define the specific columns that you want to display.

Since you only need to bind the grid to the loading of the first page, use the condition !Page.IsPostBack :

 protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridAllStore.DataSource = lstview; GridAllStore.DataBind(); } } 
+16
source

Change your code to:

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

And change the layout of the GridView to:

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

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

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

+2
source

Did you try to add the following line immediately after installing the data source?

 GridAllStore.DataBind(); 
+1
source

Source: https://habr.com/ru/post/913988/


All Articles