Asp SqlDataSource将数据库数据绑定在 GridView
生活随笔
收集整理的這篇文章主要介紹了
Asp SqlDataSource将数据库数据绑定在 GridView
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.首先認識一下GridView的幾條屬性
?AllowPaging? 確定是否可以分頁
?AllowSorting? 確定是否可以進行排序
?AlternatingRowStyle? 指定奇數行樣式
?AutoGenerateColumns? 確定是否根據綁定的數據源自動生成對應的列
?DataSource 指定綁定到控件的數據源
?DataSourceID 指定綁定到控件的數據源控件的ID
?2.關鍵代碼
<body><form id="form1" runat="server"><div><h1>實驗數據</h1></div><asp:GridView ID="GridView1" runat="server" Width="823px" AutoGenerateColumns="False" EnableModelValidation="True" DataSourceID="SqlDataSource1" AllowPaging="True" AllowSorting="True" GridLines="None" BorderColor="Silver" BorderStyle="Solid" BorderWidth="1px" ><%--綁定Test數據庫的幾個屬性ID Name Age--%><Columns><asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="true" SortExpression="ID"><ItemStyle HorizontalAlign="Center" /></asp:BoundField><asp:BoundField DataField="Name" HeaderText="Name" ReadOnly="true" SortExpression="ID"><ItemStyle HorizontalAlign="Center" /></asp:BoundField><asp:BoundField DataField="Age" HeaderText="Age" ReadOnly="true" SortExpression="ID"><ItemStyle HorizontalAlign="Center" /></asp:BoundField></Columns><RowStyle Height="42px" /><HeaderStyle BackColor="#33CC33" Height="40px"/><AlternatingRowStyle BackColor="#E0E0E0" ForeColor="#003366" BorderColor="Black" BorderStyle="None" /></asp:GridView><%--連接數據庫--%><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="Data Source=IP;Initial Catalog=數據庫名稱;Persist Security Info=True;User ID=**; Password=**"ProviderName="System.Data.SqlClient" SelectCommand="SELECT [ID], [Name], [Age] FROM [Test]"></asp:SqlDataSource></form> </body>?
?
總結
以上是生活随笔為你收集整理的Asp SqlDataSource将数据库数据绑定在 GridView的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决SQL Server 2008安装时
- 下一篇: mysql优化一