简体   繁体   English

Gridview 仅显示 MySQL 数据库中的 1 行 - ASP.net ZD7EFA19FBE7D3972FD5ADB6024223D7

[英]Gridview is showing only 1 row from MySQL database - ASP.net C#

I want to retrieve all data from my datatable and bind them with a gridview.我想从我的数据表中检索所有数据并将它们与 gridview 绑定。 The data actually binds and it works, but it is only showing the first row from the table.数据实际绑定并且有效,但它仅显示表中的第一行。 Here's my gridview in my web form这是我的 web 表格中的 gridview

           <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="false" Width="100%" GridLines="None">
           <Fields>
           <asp:TemplateField ShowHeader="false">
           <ItemTemplate>
           <tr>
           <div>
           <h2>
           <asp:Label ID="lblPostTitle" runat="server" Text='<%#Eval("posttitle") %>'></asp:Label></h2>
           <div>
           <span>
           <asp:Label ID="lblPostUser" runat="server" Text='<%#Eval("postuser") %>'></asp:Label></span>
           <span>
           <asp:Label ID="lblPostTime" runat="server" Text='<%#Eval("posttime") %>'></asp:Label></span>
           </div>
           <div style="text-align: justify;">
           <p>
           <asp:Label ID="lblPostContent" runat="server" Text='<%#Eval("postcontent") %>'></asp:Label></p>
           </div>
           </div>
           </tr>
           </ItemTemplate>
           </asp:TemplateField>
           </Fields>
           </asp:DetailsView>

and here's the Bind code这是绑定代码

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
                    this.PostUpd();
        }
    }

    public void PostUpd()
    {
        MaconOpen();
        MACONNEW.Open();

        MySqlCommand Postcmd = new MySqlCommand("SELECT * FROM postretv", MACONNEW);
        MySqlDataAdapter adp = new MySqlDataAdapter(Postcmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        DetailsView1.DataSource = ds;
        DetailsView1.DataBind();
        Postcmd.Dispose();
    }

However, my gridview is showing only the first row of the datatable.但是,我的 gridview 只显示数据表的第一行。 What could be wrong here and how could it be fixed?这里可能出了什么问题,如何解决?

I tried changing detailsview to ListView我尝试将 detailsview 更改为 ListView

           <asp:ListView ID="DetailsView12" runat="server" AutoGenerateRows="false"  GridLines="None">
           <ItemTemplate>
           <tr>
           <div>
           <h2>
           <asp:Label ID="lblPostTitle" runat="server" Text='<%#Eval("posttitle") %>'></asp:Label></h2>
           <div>
           <span>
           <asp:Label ID="lblPostUser" runat="server" Text='<%#Eval("postuser") %>'></asp:Label></span>
           <span>
           <asp:Label ID="lblPostTime" runat="server" Text='<%#Eval("posttime") %>'></asp:Label></span>
           </div>
           <div style="text-align: justify;">
           <p>
           <asp:Label ID="lblPostContent" runat="server" Text='<%#Eval("postcontent") %>'></asp:Label></p>
           </div>
           </div>
           </tr>
           </ItemTemplate>
           </asp:ListView>

and it worked.它奏效了。 Thank you anyways!还是谢谢你!

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

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