DataGrid

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>mingxiguanli</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <style type="text/css">BODY { BACKGROUND-COLOR: #def3ff }
  </style>
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:datagrid id="dgAddDelConfirm" style="Z-INDEX: 101; LEFT: 16px; POSITION: absolute; TOP: 56px"
     runat="server" AllowSorting="True" ShowFooter="True" AutoGenerateColumns="False" Width="816px"
     Height="176px" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" BackColor="White"
     CellPadding="4" AllowCustomPaging="True" PageSize="18" AllowPaging="True">
     <FooterStyle ForeColor="#003399" BackColor="#99CCCC"></FooterStyle>
     <SelectedItemStyle Font-Bold="True" ForeColor="#CCFF99" BackColor="#009999"></SelectedItemStyle>
     <ItemStyle ForeColor="#003399" BackColor="White"></ItemStyle>
     <HeaderStyle Font-Bold="True" ForeColor="#CCCCFF" BackColor="#003399"></HeaderStyle>
     <Columns>
      <asp:BoundColumn DataField="zhigongming" HeaderText="职工姓名"></asp:BoundColumn>
      <asp:BoundColumn DataField="suoshubumen" HeaderText="所属部门"></asp:BoundColumn>
      <asp:BoundColumn DataField="zhanghao" HeaderText="账号"></asp:BoundColumn>
      <asp:BoundColumn DataField="jine" HeaderText="金额"></asp:BoundColumn>
      <asp:BoundColumn DataField="leixing" HeaderText="类型"></asp:BoundColumn>
      <asp:BoundColumn DataField="dakehu" HeaderText="客户类型"></asp:BoundColumn>
      <asp:BoundColumn DataField="cunruriqi" HeaderText="存入日期"></asp:BoundColumn>
      <asp:BoundColumn DataField="lururiqi" HeaderText="录入日期"></asp:BoundColumn>
      <asp:ButtonColumn Text="删除" CommandName="Delete"></asp:ButtonColumn>
     </Columns>
     <PagerStyle HorizontalAlign="Left" ForeColor="#003399" BackColor="#99CCCC" Mode="NumericPages"></PagerStyle>
    </asp:datagrid>
    <asp:TextBox id="TextBox1" style="Z-INDEX: 102; LEFT: 16px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
    <asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 208px; POSITION: absolute; TOP: 16px" runat="server"
     Text="Button"></asp:Button></FONT></form>
 </body>
</HTML>

 

None.gif
None.gif
using  System;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Web;
None.gif
using  System.Web.SessionState;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
using  System.Data.SqlClient;
None.gif
using  System.Configuration;
None.gif
None.gif
namespace  lanchuxitong
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// mingxiguanli 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class mingxiguanli : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid dgAddDelConfirm;
InBlock.gif        
protected System.Web.UI.WebControls.TextBox TextBox1;
InBlock.gif        
protected System.Web.UI.WebControls.Button Button1;
InBlock.gif        
//定义全局变量用来保存每页的起始项索引
InBlock.gif
        int startIndex=0;
InBlock.gif    
InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
// 在此处放置用户代码以初始化页面
InBlock.gif
            if(!IsPostBack)
InBlock.gif                DataGridDataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
// ItemDataBound事件
InBlock.gif
        private void changeRowColor(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//如果是数据项并且是交替项
InBlock.gif
            if(e.Item.ItemType == ListItemType.Item  || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//添加自定义属性,当鼠标移过来时设置该行的背景色为"6699ff",并保存原背景色
InBlock.gif
                e.Item.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
InBlock.gif                
//添加自定义属性,当鼠标移走时还原该行的背景色
InBlock.gif
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=currentcolor");
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
InBlock.gif        
override protected void OnInit(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif            
base.OnInit(e);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{    
InBlock.gif            
this.dgAddDelConfirm.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.dgAddDelConfirm_ItemCreated);
InBlock.gif            
this.dgAddDelConfirm.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgAddDelConfirm_PageIndexChanged);
InBlock.gif            
this.dgAddDelConfirm.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_DeleteCommand);
InBlock.gif            
this.dgAddDelConfirm.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.changeRowColor);
InBlock.gif            
this.Button1.Click += new System.EventHandler(this.Button1_Click);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
private void dg_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//输出信息
InBlock.gif
            Response.Write("删除");
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void DataGridDataBind()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//定义数据连接对象,其中数据库连接字符串是在Web.Config文件中定义的
InBlock.gif
            SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionSqlServer"].ToString());
InBlock.gif            
//创建数据适配器对象
InBlock.gif
            SqlDataAdapter da = new SqlDataAdapter("select zhigongming,suoshubumen,chuhuzhanghao,lanchujine,lanchuleixing,dakehu,cunruriqi,lururiqi from lanchudengji",conn);
InBlock.gif            
//创建DataSet对象
InBlock.gif
            DataSet ds = new DataSet();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//从指定的索引开始取PageSize条记录
InBlock.gif
                da.Fill(ds,startIndex,dgAddDelConfirm.PageSize,"CurDataTable");
InBlock.gif                
//填充数据集
InBlock.gif
                da.Fill(ds,"AllDataTable");
InBlock.gif                
//设置DataGrid控件实际要显示的项数
InBlock.gif
                dgAddDelConfirm.VirtualItemCount = ds.Tables["AllDataTable"].Rows.Count;
InBlock.gif                
//进行数据绑定
InBlock.gif
                dgAddDelConfirm.DataSource = ds.Tables["CurDataTable"];
InBlock.gif                dgAddDelConfirm.DataBind();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception error)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//输出异常信息
InBlock.gif
                Response.Write(error.ToString());
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void dgAddDelConfirm_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//判断如果是DataGrid控件中的项、交替项以及编辑项
InBlock.gif
            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem || e.Item.ItemType==ListItemType.EditItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                TableCell myTableCell;
InBlock.gif                
//定义对“删除”单元格的引用
InBlock.gif
                myTableCell = e.Item.Cells[8];
InBlock.gif                
//定义对“删除”单元格中控件的引用
InBlock.gif
                LinkButton btnDel = (LinkButton)myTableCell.Controls[0];
InBlock.gif                
//添加当单击时弹出确认对话框的自定义属性
InBlock.gif
                btnDel.Attributes.Add("onclick""return confirm('您真的要删除此行吗?');");
InBlock.gif                btnDel.Text 
= "删除";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void dgAddDelConfirm_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//设置DataGrid当前页的索引值为用户选择的页的索引
InBlock.gif
            dgAddDelConfirm.CurrentPageIndex = e.NewPageIndex;
InBlock.gif            
//取得当前页为止总共有多少条记录,以便在下一页就从该记录开始读取
InBlock.gif
            startIndex = dgAddDelConfirm.PageSize * dgAddDelConfirm.CurrentPageIndex;
InBlock.gif            
//重新绑定数据
InBlock.gif
            DataGridDataBind();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

转载于:https://www.cnblogs.com/cuancuan/archive/2006/01/26/323444.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值