简体   繁体   中英

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

           <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. What could be wrong here and how could it be fixed?

I tried changing detailsview to 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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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