DataGrid部分功能实现[原创]

我把我原来写的一个关于DataGrid的功能的程序公布出来,都是些使用的东西,希望能给.net的初学者一点点帮助……,写的不好的地方请大家多多指教!
                                                                                                        ——缘易(Hermes Liu)

//DataGrid_1.aspx
**************************************************************************
None.gif < body  MS_POSITIONING ="GridLayout" >
None.gif        
< form  id ="Form1"  method ="post"  runat ="server" >
None.gif            
< asp:datagrid  id ="Dg1"  style ="Z-INDEX: 101; LEFT: 7px; POSITION: absolute; TOP: 8px"  runat ="server"
None.gif                HeaderStyle-HorizontalAlign
="Center"  HeaderStyle-Font-Bold ="True"  HeaderStyle-BackColor ="#6699cc"
None.gif                AutoGenerateColumns
="false"  ItemStyle-HorizontalAlign ="Center" >
None.gif                
< Columns >
None.gif                    
< asp:EditCommandColumn  HeaderText ="编辑"  EditText ="编辑"  CancelText ="取消"  UpdateText ="更新"  ItemStyle-Width ="100" ></ asp:EditCommandColumn >
None.gif                    
< asp:ButtonColumn  HeaderText ="删除"  ButtonType ="LinkButton"  Text ="删除"  CommandName ="Delete"  ItemStyle-Width ="60" ></ asp:ButtonColumn >
None.gif                    
< asp:BoundColumn  ReadOnly ="True"  HeaderText ="用户编号"  DataField ="customerid" ></ asp:BoundColumn >
None.gif                    
< asp:BoundColumn  DataField ="companyname"  HeaderText ="公司名" ></ asp:BoundColumn >
None.gif                    
< asp:BoundColumn  DataField ="contactname"  HeaderText ="联系名" ></ asp:BoundColumn >
None.gif                
</ Columns >
None.gif            
</ asp:datagrid >
None.gif            
< asp:Label  id ="Label1"  style ="Z-INDEX: 102; LEFT: 14px; POSITION: absolute; TOP: 237px"  runat ="server" > Label </ asp:Label ></ form >
None.gif    
</ body >
//DataGrid_1.aspx.cs
**************************************************************************
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
using  DBF_Center;
None.gif
namespace  TestCenter
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// *******************************DataGrid全部功能实现****************************************
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class DataGrid_1 : System.Web.UI.Page
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
protected System.Web.UI.WebControls.Label Label1;
InBlock.gif        
protected System.Web.UI.WebControls.DataGrid Dg1;
InBlock.gif
InBlock.gif    
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 数据邦定函数
InBlock.gif        
/// </summary>
ExpandedSubBlockEnd.gif        
/// <param name="sortField">用于排序的字段</param>

InBlock.gif        public void BindGrid(string sortField)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//连接数据库,填充DataSet
InBlock.gif
            string constr=System.Configuration.ConfigurationSettings.AppSettings["Connectstring"];
InBlock.gif            SqlConnection conn
=new SqlConnection(constr);
InBlock.gif            
string commstr="select customerid,companyname,contactname from customer";            
InBlock.gif            SqlDataAdapter ad
=new SqlDataAdapter(commstr,conn);
InBlock.gif            DataSet dst
=new DataSet();
InBlock.gif            ad.Fill(dst);
InBlock.gif            
//string pd_a="select * from newstype order by t_id desc";
InBlock.gif
            DataView source=new DataView(dst.Tables[0]);
InBlock.gif            source.Sort
=sortField;
InBlock.gif            Dg1.DataSource
=source;
InBlock.gif            Dg1.DataBind();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
private void Page_Load(object sender, System.EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(!IsPostBack)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif            
this.BindGrid("customerid");//数据邦定,按customerid排序
ExpandedSubBlockEnd.gif
            }

InBlock.gif
InBlock.gif            Dg1.PagerStyle.Mode
=PagerMode.NumericPages;//显示默认的页码为数字
InBlock.gif

ExpandedSubBlockStart.gifContractedSubBlock.gif            
/**//*
InBlock.gif             * 以上下页的形式显示页码
InBlock.gif            Dg1.PagerStyle.Mode=PagerMode.NextPrev;
InBlock.gif            Dg1.PagerStyle.NextPageText="下一页";
InBlock.gif            Dg1.PagerStyle.PrevPageText="上一页";
ExpandedSubBlockEnd.gif            
*/
            
InBlock.gif            Dg1.PagerStyle.Position
=0;//显示起始页码
InBlock.gif
            Dg1.PagerStyle.PageButtonCount=10;//显示显示页数
InBlock.gif
            Dg1.PagerStyle.Visible=true;//显示默认页码数
InBlock.gif
            Dg1.PageSize=6;//每页显示信息数
InBlock.gif
            Dg1.AllowPaging=true;
InBlock.gif            Dg1.AllowSorting
=true;
InBlock.gif            Dg1.AutoGenerateColumns
=false;
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.Dg1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.ItemCreate_Grid);
InBlock.gif            
this.Dg1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.Pager_Grid);
InBlock.gif            
this.Dg1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Cancel_Grid);
InBlock.gif            
this.Dg1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Edit_Grid);
InBlock.gif            
this.Dg1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.Sort_Grid);
InBlock.gif            
this.Dg1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Update_Grid);
InBlock.gif            
this.Dg1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.Delete_Grid);
InBlock.gif            
this.Dg1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.ItemBound_Grid);
InBlock.gif            
this.Load += new System.EventHandler(this.Page_Load);
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
//实现自动排序
InBlock.gif
        private void Sort_Grid(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.BindGrid((string)e.SortExpression);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//编辑按钮的实现
InBlock.gif
        private void Edit_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dg1.EditItemIndex
=e.Item.ItemIndex;
InBlock.gif            
this.BindGrid("customerid");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//取消按钮的实现
InBlock.gif
        private void Cancel_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dg1.EditItemIndex
=-1;
InBlock.gif            
this.BindGrid("customerid");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//更新数据
InBlock.gif
        private void Update_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            TextBox C_name
=(TextBox)e.Item.Cells[3].Controls[0];
InBlock.gif            
//TextBox C_nid=(TextBox)e.Item.Cells[2].Controls[0];
InBlock.gif
            TextBox C_cname=(TextBox)e.Item.Cells[4].Controls[0];
InBlock.gif            
string C_id=e.Item.Cells[2].Text.ToString();            
InBlock.gif            
if(C_name.Text.ToString()=="")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                    Response.Write(
"<script LANGUAGE='javascript'>alert('请输入确认输入正确!');history.go(-1);</script>");
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
string up_sql="update customer set Companyname='"+C_name.Text.ToString()+"',contactname='"+C_cname.Text.ToString()+"'  where customerid='"+C_id+"'";
InBlock.gif                dbcontrol.ExecuteSql(up_sql);    
InBlock.gif                Label1.Text
=up_sql;
ExpandedSubBlockEnd.gif            }
    
InBlock.gif            Dg1.EditItemIndex
=-1;
InBlock.gif            
this.BindGrid("customerid");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//自动分页
InBlock.gif
        private void Pager_Grid(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Dg1.CurrentPageIndex
=e.NewPageIndex;
InBlock.gif            BindGrid(
"customerid");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//Item事件
InBlock.gif
        private void ItemCreate_Grid(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType==ListItemType.AlternatingItem||e.Item.ItemType==ListItemType.EditItem||e.Item.ItemType==ListItemType.Item)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                LinkButton btnDel
=(LinkButton)e.Item.Cells[1].Controls[0];
InBlock.gif                btnDel.Attributes.Add(
"onClick","JavaScript:return confirm('确认要删除此行的信息?')");
InBlock.gif                btnDel.Text
="删除";
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//执行删除
InBlock.gif
        private void Delete_Grid(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
string del_sql="delete from customer where customerid='"+e.Item.Cells[2].Text+"'";
InBlock.gif            dbcontrol.ExecuteSql(del_sql);
InBlock.gif            Dg1.EditItemIndex
=-1;
InBlock.gif            BindGrid(
"customerid");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//行随鼠标移动变色
InBlock.gif
        private void ItemBound_Grid(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                e.Item.Attributes.Add(
"onmouseover","this.style.backgroundColor='#fafafa'");
InBlock.gif                e.Item.Attributes.Add(
"onmouseout","this.style.backgroundColor='white'");
ExpandedSubBlockEnd.gif            }
        
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/hermes262/archive/2006/03/03/342293.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值