Fah*_*med 6 c# asp.net gridview
我正在添加一个GridView,然后从SQL Server数据库中显示数据.问题是GridView没有在带有或没有数据的浏览器中显示.
这是我的代码:
<asp:GridView ID="GridAllStore" runat="server" AutoGenerateColumns="False" Width="100%" ViewStateMode="Enabled">
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*son 18
我认为问题是你没有定义要显示的任何列.设置AutoGenerateColumns为false 时,必须显式定义列.
要确保基础工作设置AutoGenerateColumns为true:
<asp:GridView ID="GridAllStore" runat="server" AutoGenerateColumns="true" Width="100%" ViewStateMode="Enabled">
Run Code Online (Sandbox Code Playgroud)
随着AutoGenerateColumns设置为true,数据源分配和DataBind()调用,你应该开始看到一些数据.一旦开始查看数据,就可以定义要显示的特定列.
由于您只需要在第一页加载时绑定网格,因此请使用以下!Page.IsPostBack条件:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridAllStore.DataSource = lstview;
GridAllStore.DataBind();
}
}
Run Code Online (Sandbox Code Playgroud)