GridView之学习一

Default.aspx

<%@ 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">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            BackColor
="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            CellPadding
="3" OnPageIndexChanging="GridView1_PageIndexChanging" PageSize="5">
            
<PagerSettings FirstPageImageUrl="~/Images/First.gif" FirstPageText="|&amp;lt;" LastPageImageUrl="~/Images/Last.gif"
                LastPageText
="&amp;gt;|" Mode="NumericFirstLast" NextPageImageUrl="~/Images/Next.gif"
                PageButtonCount
="5" PreviousPageImageUrl="~/Images/Previous.gif" />
            
<FooterStyle BackColor="White" ForeColor="#000066" />
            
<Columns>
                
<asp:BoundField DataField="ClientName" HeaderText="姓名" SortExpression="Name" />
                
<asp:BoundField DataField="AddressStr" HeaderText="地址" SortExpression="Address" />
                
<asp:BoundField DataField="PostCode" HeaderText="邮编" SortExpression="PostCode" />
            
</Columns>
            
<RowStyle ForeColor="#000066" />
            
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
            
<PagerStyle BackColor="#FFC080" BorderColor="Blue" Font-Bold="True" ForeColor="#000066"
                HorizontalAlign
="Left" />
            
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
        
</asp:GridView>
    
    
</div>
    
</form>
</body>
</html>



ClientInfo.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// ClientInfo 的摘要说明
/// </summary>

public class ClientInfo
{
    
private int clientID;//客户编号

    
public int ClientID
    
{
        
get return clientID; }
        
set { clientID = value; }
    }


    
private string clientName;//客户姓名

    
public string ClientName
    
{
        
get return clientName; }
        
set { clientName = value; }
    }


    
private string addressStr;//客户地址

    
public string AddressStr
    
{
        
get return addressStr; }
        
set { addressStr = value; }
    }


    
private string postCode;//客户邮编

    
public string PostCode
    
{
        
get return postCode; }
        
set { postCode = value; }
    }


    
private string telephone;//客户电话

    
public string Telephone
    
{
        
get return telephone; }
        
set { telephone = value; }
    }


    
private string email;//客户邮件

    
public string Email
    
{
        
get return email; }
        
set { email = value; }
    }

}




ClientInfoAccessObj.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Data.SqlClient;

/// <summary>
/// ClientInfoAccessObj 的摘要说明
/// </summary>

public class ClientInfoAccessObj
{
    
//连接字符串
    private readonly string connString = "server=.\\sqlexpress;database=Clients;uid=sa;pwd=123456;";
    
//获得所有的客户信息
    public List<ClientInfo> GetAllClients()
    
{
        List
<ClientInfo> clients = new List<ClientInfo>();
        
using (SqlConnection conn=new SqlConnection(connString))
        
{
            
string sql = "select * from orderClient";
            
using (SqlCommand comm=new SqlCommand(sql,conn))
            
{
                conn.Open();
                
using (SqlDataReader sdr=comm.ExecuteReader())
                
{
                    
while (sdr.Read())
                    
{
                        ClientInfo ci 
= new ClientInfo();
                        ci.ClientID 
= int.Parse(sdr["ClientID"].ToString());
                        ci.ClientName 
= sdr["ClientName"].ToString();
                        ci.AddressStr 
= sdr["AddressStr"].ToString();
                        ci.PostCode 
= sdr["PostCode"].ToString();
                        ci.Telephone 
= sdr["Telephone"].ToString();
                        ci.Email 
= sdr["Email"].ToString();
                        clients.Add(ci);
                    }

                    
return clients;
                }

            }

        }

    }

}




Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            ClientInfoAccessObj obj 
= new ClientInfoAccessObj();
            GridView1.DataSource 
= obj.GetAllClients();
            GridView1.DataBind();
        }

    }

    
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        ClientInfoAccessObj obj 
= new ClientInfoAccessObj();
        GridView1.DataSource 
= obj.GetAllClients();
        GridView1.PageIndex 
= e.NewPageIndex;//将点击的新页下标赋给GridView
        GridView1.DataBind();
    }

}

原文地址:https://www.cnblogs.com/mdy41034264/p/1351427.html