简体   繁体   English

ASP.NET中的GridView不显示有或没有数据

[英]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. 我正在添加一个GridView,然后从SQL Server数据库中显示数据。 The problem is that the GridView is not displaying in the browser with or without data. 问题是GridView没有在带有或没有数据的浏览器中显示。

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

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. AutoGenerateColumns设置为false时,必须显式定义列。

To make sure that the basics are working set AutoGenerateColumns to true: 要确保基础工作正常,请将AutoGenerateColumns设置为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. AutoGenerateColumns设置为true,分配的数据源和调用的DataBind() ,您应该开始看到一些数据。 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: 由于您只需要在第一页加载时绑定网格,因此使用!Page.IsPostBack条件:

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

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: 并将您的GridView标记更改为:

<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. 注意到它现在是AutoGenerateColumns="True"因为这将显示数据并生成列。 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. 要做到这一点,因为你现在还不知道你在做什么,切换到设计视图,你可以编辑gridview模板。

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 http://msdn.microsoft.com/en-us/library/bb288032.aspx

您是否尝试在设置数据源后立即添加以下行?

GridAllStore.DataBind();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM