Default.aspx
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>用TableAdapter创建DataTable定义及查询
Default.aspx.cs
View Code
using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page { dsInfoTableAdapters.tb_infTableAdapter adapter; protected void Page_Load(object sender, EventArgs e) { if (adapter == null) { adapter = new dsInfoTableAdapters.tb_infTableAdapter();//实例化TableAdapter } if (!IsPostBack) { showEmployees();//显示GridView控件中数据 } } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { gvInfo.PageIndex = e.NewPageIndex; gvInfo.DataBind(); showEmployees(); } //用GridView显示数据 protected void showEmployees() { gvInfo.DataSource = adapter.GetData();//调用TableAdapter类中的GetData()执行查询数据操作 gvInfo.DataBind();//绑定GridView控件 } protected void gvInfo_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string gIntro = e.Row.Cells[3].Text; e.Row.Cells[3].Text = StrHelper.GetFirstString(gIntro, 12);//指定显示的内容并限定其长度 } }}