GridView 与自定义对象的绑定和分页
一、自定义对象和对象集合
 using System;
using System; using System.Collections;
using System.Collections; using System.Collections.Generic;
using System.Collections.Generic;

 /// <summary>
/// <summary> /// 手机模块定位
/// 手机模块定位 /// </summary>
/// </summary> public class Bus
public class Bus {
{ public Bus(string name, float longitude, float latitude)
    public Bus(string name, float longitude, float latitude) {
    { this.name = name;
        this.name = name; this.latitude = latitude;
        this.latitude = latitude; this.longitude = longitude;
        this.longitude = longitude; }
    }
 string name;
    string name; float longitude, latitude;
    float longitude, latitude;
 public string Name
    public string Name {
    { get { return name; }
        get { return name; } }
    }
 public float Longitude
    public float Longitude {
    { get { return longitude; }
        get { return longitude; } }
    }
 public float Latitude
    public float Latitude {
    { get { return latitude; }
        get { return latitude; } }
    } }
}

 /// <summary>
/// <summary> /// 手机模块集合
/// 手机模块集合 /// </summary>
/// </summary> public class BusCollection
public class BusCollection {
{ List<Bus> col;
    List<Bus> col;
 public BusCollection()
    public BusCollection() {
    { col = new List<Bus>();
        col = new List<Bus>(); for (int i = 0; i < 500; i++)
        for (int i = 0; i < 500; i++) {
        { Bus b = new Bus("13501401" + i.ToString("d3"), 118.333f, 32.1123f);
            Bus b = new Bus("13501401" + i.ToString("d3"), 118.333f, 32.1123f); col.Add(b);
            col.Add(b); }
        } }
    }

 public List<Bus> Collection
    public List<Bus> Collection {
    { get { return col; }
        get { return col; } }
    } }
}二、页面表现逻辑
 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml" >
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">
<head runat="server"> <title>自定义对象绑定与分页</title>
    <title>自定义对象绑定与分页</title> <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" /> </head>
</head> <body>
<body> <form id="form1" runat="server">
    <form id="form1" runat="server"> <div>
    <div> <asp:GridView ID="GridView1"
        <asp:GridView ID="GridView1"  runat="server"
            runat="server"  AutoGenerateColumns="False"
            AutoGenerateColumns="False"  CssClass="class2"
            CssClass="class2"  EnableViewState="False"
            EnableViewState="False"  AllowPaging="true"
            AllowPaging="true"  OnPageIndexChanging="GridView1_PageIndexChanging">
            OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns>
            <Columns> <asp:BoundField DataField="Name" HeaderText="手机号码" />
                <asp:BoundField DataField="Name" HeaderText="手机号码" /> <asp:BoundField DataField="Longitude" DataFormatString="{0:0.00000}" HeaderText="经度" />
                <asp:BoundField DataField="Longitude" DataFormatString="{0:0.00000}" HeaderText="经度" /> <asp:BoundField DataField="Latitude" DataFormatString="{0:0.00000}" HeaderText="纬度" />
                <asp:BoundField DataField="Latitude" DataFormatString="{0:0.00000}" HeaderText="纬度" /> </Columns>
            </Columns> </asp:GridView>
        </asp:GridView> </div>
    </div> </form>
    </form> </body>
</body> </html>
</html>
后台代码
 using System;
using System; using System.Data;
using System.Data; using System.Configuration;
using System.Configuration; using System.Web;
using System.Web; using System.Web.Security;
using System.Web.Security; using System.Web.UI;
using System.Web.UI; using System.Web.UI.WebControls;
using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts;
using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls;
using System.Web.UI.HtmlControls;
 public partial class _Default : System.Web.UI.Page
public partial class _Default : System.Web.UI.Page {
{ protected void Page_Load(object sender, EventArgs e)
    protected void Page_Load(object sender, EventArgs e) {
    { if (!IsPostBack)
        if (!IsPostBack) {
        { DataBind();
            DataBind(); }
        } }
    } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) {
    { GridView1.PageIndex = e.NewPageIndex;
        GridView1.PageIndex = e.NewPageIndex; DataBind();
        DataBind(); }
    }

 private void DataBind()
    private void DataBind() {
    { BusCollection c = new BusCollection();
        BusCollection c = new BusCollection(); GridView1.DataSource = c.Collection;
        GridView1.DataSource = c.Collection; GridView1.DataBind();
        GridView1.DataBind(); }
    } }
}
样式表StyleSheet.css
 *
* {
{ font-size: 9pt;
    font-size: 9pt; font-family: Verdana, 宋体, Tahoma;
    font-family: Verdana, 宋体, Tahoma; }
}
 .class2
.class2 {
{ border-collapse:collapse;
    border-collapse:collapse;  border: solid 2px #336666;
    border: solid 2px #336666; }
}
 .class2 td
.class2 td {
{ padding: 5px 10px;
    padding: 5px 10px; border: solid 1px #dcdcdc;
    border: solid 1px #dcdcdc; 
     }
} .class2 th
.class2 th {
{ background-color: #336666;
    background-color: #336666; color: #ffffff;
    color: #ffffff; height: 2.5em;
    height: 2.5em; }
}
 
                     
                    
                 
                    
                


 
     
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号