ASP.NET中DataGrid的简单用法

  1None.gifusing System;
  2None.gifusing System.Collections;
  3None.gifusing System.ComponentModel;
  4None.gifusing System.Data;
  5None.gifusing System.Drawing;
  6None.gifusing System.Web;
  7None.gifusing System.Web.SessionState;
  8None.gifusing System.Web.UI;
  9None.gifusing System.Web.UI.WebControls;
 10None.gifusing System.Web.UI.HtmlControls;
 11None.gifusing System.Data.SqlClient;
 12None.gifnamespace DataGridTest
 13ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 14ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 15InBlock.gif    /// WebForm1 的摘要说明。
 16ExpandedSubBlockEnd.gif    /// </summary>

 17InBlock.gif    public class WebForm1 : System.Web.UI.Page
 18ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
 19InBlock.gif        protected System.Web.UI.WebControls.Button Button1;
 20InBlock.gif        protected System.Web.UI.WebControls.HyperLink HyperLink1;
 21InBlock.gif        protected System.Web.UI.WebControls.DataGrid DataGrid1;
 22InBlock.gif    
 23InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
 24ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 25InBlock.gif            if(!this.IsPostBack)
 26ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 27InBlock.gif                this.BindToDataGrid();
 28ExpandedSubBlockEnd.gif            }

 29InBlock.gif                
 30InBlock.gif            // 在此处放置用户代码以初始化页面
 31ExpandedSubBlockEnd.gif        }

 32InBlock.gif        private void BindToDataGrid()
 33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 34InBlock.gif            SqlConnection con=DB.createCon();
 35InBlock.gif            SqlDataAdapter sda=new SqlDataAdapter(); //数据适配器
 36InBlock.gif            sda.SelectCommand=new SqlCommand("select * from employees",con);
 37InBlock.gif            DataSet ds=new DataSet();
 38InBlock.gif            sda.Fill(ds,"emp");
 39InBlock.gif            this.DataGrid1.DataSource=ds.Tables["emp"];
 40InBlock.gif            this.DataGrid1.DataBind();
 41ExpandedSubBlockEnd.gif        }

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

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

 56InBlock.gif        private void InitializeComponent()
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{    
 58InBlock.gif            this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
 59InBlock.gif            this.DataGrid1.SortCommand += new System.Web.UI.WebControls.DataGridSortCommandEventHandler(this.DataGrid1_SortCommand);
 60InBlock.gif            this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
 61InBlock.gif            this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
 62InBlock.gif            this.Button1.Click += new System.EventHandler(this.Button1_Click);
 63InBlock.gif            this.Load += new System.EventHandler(this.Page_Load);
 64InBlock.gif
 65ExpandedSubBlockEnd.gif        }

 66ExpandedSubBlockEnd.gif        #endregion

 67InBlock.gif
 68InBlock.gif        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
 69ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 70ExpandedSubBlockEnd.gif        }

 71InBlock.gif
 72InBlock.gif        private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 74InBlock.gif            this.DataGrid1.CurrentPageIndex=e.NewPageIndex;
 75InBlock.gif            this.BindToDataGrid();
 76ExpandedSubBlockEnd.gif        }

 77InBlock.gif
 78InBlock.gif        private void Button1_Click(object sender, System.EventArgs e)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 80InBlock.gif            if(DataGrid1.Columns[1].Visible==true)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 82InBlock.gif                this.DataGrid1.Columns[1].Visible=false;
 83ExpandedSubBlockEnd.gif            }

 84InBlock.gif            else
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 86InBlock.gif                this.DataGrid1.Columns[1].Visible=true;
 87ExpandedSubBlockEnd.gif            }

 88ExpandedSubBlockEnd.gif        }

 89InBlock.gif
 90InBlock.gif        private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
 91ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
 92InBlock.gif            if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
 93ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{   //添加脚本,使鼠标移动到项上发生变化
 94InBlock.gif                e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#eeeeee'");
 95InBlock.gif                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
 96ExpandedSubBlockEnd.gif            }

 97ExpandedSubBlockEnd.gif        }

 98InBlock.gif
 99InBlock.gif        private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
100ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{//列值进行排序
101InBlock.gif            if(ViewState["Order"]==null)
102ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
103InBlock.gif                ViewState["Order"]="ASC";
104ExpandedSubBlockEnd.gif            }

105InBlock.gif            else
106ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
107InBlock.gif                if(ViewState["Order"].ToString()=="ASC")
108ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
109InBlock.gif                    ViewState["Order"]="DESC";
110ExpandedSubBlockEnd.gif                }

111InBlock.gif                else
112ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
113InBlock.gif                    ViewState["Order"]="ASC";
114ExpandedSubBlockEnd.gif                }

115InBlock.gif                
116ExpandedSubBlockEnd.gif            }

117InBlock.gif            //数据绑定显示
118InBlock.gif            SqlConnection con=DB.createCon();
119InBlock.gif            SqlDataAdapter sda=new SqlDataAdapter(); //数据适配器
120InBlock.gif            sda.SelectCommand=new SqlCommand("select * from employees",con);
121InBlock.gif            DataSet ds=new DataSet();
122InBlock.gif            sda.Fill(ds,"emp");
123InBlock.gif            ds.Tables["emp"].DefaultView.Sort=e.SortExpression+" "+ViewState["Order"].ToString();
124InBlock.gif            this.DataGrid1.DataSource=ds.Tables["emp"].DefaultView;
125InBlock.gif            this.DataGrid1.DataBind();
126ExpandedSubBlockEnd.gif        }

127ExpandedSubBlockEnd.gif    }

128ExpandedBlockEnd.gif}

129None.gif

转载于:https://www.cnblogs.com/icejd/archive/2006/04/12/373756.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值