zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    10.3 noip模拟试题
    9.30 noip模拟试题
    9.29 奶牛练习题
    9.29noip模拟试题
    9.28noip模拟试题
    9.27 noip模拟试题
    二维数据结构学习
    9.26 noip模拟试题
    ContentProvider ContentResolver ContentObserver 内容:提供、访问、监听
    Cursor 游标
  • 原文地址:https://www.cnblogs.com/lihaibo-Leao/p/3144055.html
Copyright © 2011-2022 走看看