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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10288800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:11:44  来源:igfitidea点击:

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

c#asp.netgridview

提问by Fahim Ahmed

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.

我正在添加一个 GridView,然后在其中显示来自 SQL Server 数据库的数据。问题是 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; 
    }
}

采纳答案by James Johnson

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

我认为问题在于您没有定义任何要显示的列。当您设置AutoGenerateColumns为 false时,您必须明确定义列。

To make sure that the basics are working set AutoGenerateColumnsto true:

要确保基础工作设置AutoGenerateColumns为 true:

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

With AutoGenerateColumnsset 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.

随着AutoGenerateColumns设置为true,数据源分配和DataBind()调用,你应该开始看到一些数据。开始查看数据后,您可以定义要显示的特定列。

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

由于您只需要在第一页加载时绑定网格,请利用!Page.IsPostBack条件:

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

回答by Beenish Khan

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

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

GridAllStore.DataBind();

回答by Tim B James

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. 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.

请注意,现在AutoGenerateColumns="True"将显示数据并生成列。不过,您可能需要自定义显示的内容。要做到这一点,既然你真的不知道你在做什么,切换到设计视图,你可以编辑 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