我正在添加一个 GridView,然后在其中显示来自 SQL Server 数据库的数据。问题是 GridView 没有显示在有或没有数据的浏览器中。
这是我的代码:
<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 时,您必须明确定义列。
为确保基础工作正常,将 AutoGenerateColumns
设置为 true:
<asp:GridView ID="GridAllStore" runat="server" AutoGenerateColumns="true" Width="100%" ViewStateMode="Enabled">
将 AutoGenerateColumns
设置为 true,分配数据源并调用 DataBind()
,您应该开始看到一些数据。开始查看数据后,您可以定义要显示的特定列。
因为你只需要在第一个页面加载时绑定(bind)网格,利用 !Page.IsPostBack
条件:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridAllStore.DataSource = lstview;
GridAllStore.DataBind();
}
}
关于c# - ASP.NET 中的 GridView 不显示有或没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10288800/