GridView 綁定數據庫的方法主要有以下幾種:
示例代碼:
<asp:SqlDataSource ID="sqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YourConnectionString %>" SelectCommand="SELECT * FROM YourTable">
</asp:SqlDataSource>
<asp:GridView ID="gridView1" runat="server" DataSourceID="sqlDataSource1">
</asp:GridView>
示例代碼:
string connectionString = "YourConnectionString";
string query = "SELECT * FROM YourTable";
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter(query, connection);
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
gridView1.DataSource = dataTable;
gridView1.DataBind();
示例代碼:
List<YourEntity> dataList = GetDataFromDatabase(); // 從數據庫中獲取數據
gridView1.DataSource = dataList;
gridView1.DataBind();
以上是常用的 GridView 綁定數據庫的方法,根據具體情況選擇適合的方法進行數據綁定。