所以我不知道我会怎么做这个我已经乱了好几个小时无济于事。
我想从数据库中获取所有数据并将其显示在网格视图中。
到目前为止,我已经有了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>
而且这个。
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();
}
}
}
我从哪里开始,我已经阅读了许多其他帖子和教程,但我似乎无法将其纳入我自己的工作中。
感谢任何帮助,谢谢!
答案 0 :(得分:3)
首先,您需要从db:
获取数据java.io.FileNotFoundException: C:\Users\600010209\workarea\Translate\translate\src\main\props\engprops.txt (The system cannot find the file specified)
获得DataTable后,将其绑定到GridView:
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);
}
}
您希望确保DataTable中的列与GridView中的列匹配。