简体   繁体   English

如何从数据库检索所有行并将其存储在gridview中?

[英]How do I retrieve all rows from a database and store in a gridview?

So I am not sure how I would do this I've been messing around with it for hours to no avail. 因此,我不确定如何处理我已经花了好几个小时都没有用。

I want to get all the data from the database and display it in a grid view. 我想从数据库中获取所有数据并将其显示在网格视图中。

So far I've got the HTML side 到目前为止,我有HTML方面

<%@ Page Title="Add a pet type" Language="C#" MasterPageFile="~/Site.Master"  AutoEventWireup="true" CodeBehind="PetView.aspx.cs" Inherits="Pets_In_Need.PetView" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
    <div class="starter-template">
        <h1>View Pets</h1>
        <asp:GridView ID="grdViewPets" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ProductID" EnableViewState="False">
          <Columns>
            <asp:BoundField DataField="name" HeaderText="Name" SortExpression="Pet Name" />
            <asp:BoundField DataField="age" HeaderText="Date of Birth" ReadOnly="True" SortExpression="Pet Date of Birth" />
            <asp:BoundField DataField="gender" HeaderText="Gender" ReadOnly="True" SortExpression="Pet Gender" />
            <asp:BoundField DataField="breed" HeaderText="Breed" ReadOnly="True" SortExpression="Pet Breed" />
            <asp:BoundField DataField="weight" HeaderText="Weight(lbs)" ReadOnly="True" SortExpression="Pet Weight" />
            <asp:BoundField DataField="friendliness" HeaderText="Friendliness(1-10)" ReadOnly="True" SortExpression="Pet Friendliness" />
          </Columns>
        </asp:GridView>
     </div>
</asp:Content>

And this. 和这个。

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace Pets_In_Need
{
    public partial class PetView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Create new DB connection
            DB pinDB = new DB();
            //Open DB connection
            pinDB.Open();


        }
    }
}

Where do I go from here I've read many other posts and tutorials however I can't seem to put that into my own work. 我从这里去哪里,我已经阅读了许多其他文章和教程,但是我似乎无法将其纳入自己的工作中。

Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

First you need to acquire the data from the db: 首先,您需要从数据库获取数据:

        string SelectQuery = "SELECT * FROM [tableName]";
        DataTable table = new DataTable();
        using (var con = new SqlConnection(ConnectionString))
        {
            using (var da = new SqlDataAdapter(SelectQuery, con))
            {
                //Populate the datatable with the results
                da.Fill(table);
            }
        }

Once you have a DataTable you bind it to the GridView: 一旦有了DataTable,就将其绑定到GridView:

        grdViewPets.DataSource = table;
        grdViewPets.DataBind();

You'll want to ensure the columns in the DataTable match with the columns in the GridView. 您将要确保DataTable中的列与GridView中的列匹配。

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

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