ASP.NET 中的 GridView 不显示有或没有数据 [英] GridView in ASP.NET is not displaying with or without data
问题描述
我正在添加一个 GridView &然后显示其中来自 SQL Server 数据库的数据.问题是 GridView 没有在浏览器中显示有或没有数据.
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.
这是我的代码:
<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;
}
}
推荐答案
我认为问题在于您没有定义任何要显示的列.当您将 AutoGenerateColumns
设置为 false 时,您必须明确定义列.
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
设置为 true:
To make sure that the basics are working set AutoGenerateColumns
to true:
<asp:GridView ID="GridAllStore" runat="server" AutoGenerateColumns="true" Width="100%" ViewStateMode="Enabled">
将 AutoGenerateColumns
设置为 true、分配数据源并调用 DataBind()
后,您应该开始看到一些数据.开始查看数据后,您可以定义要显示的特定列.
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.
因为你只需要在第一页加载时绑定网格,利用 !Page.IsPostBack
条件:
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();
}
}
这篇关于ASP.NET 中的 GridView 不显示有或没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!