zoukankan      html  css  js  c++  java
  • ASP.NET中DataGrid的简单用法

      1using System;
      2using System.Collections;
      3using System.ComponentModel;
      4using System.Data;
      5using System.Drawing;
      6using System.Web;
      7using System.Web.SessionState;
      8using System.Web.UI;
      9using System.Web.UI.WebControls;
     10using System.Web.UI.HtmlControls;
     11using System.Data.SqlClient;
     12namespace DataGridTest
     13{
     14    /// <summary>
     15    /// WebForm1 的摘要说明。
     16    /// </summary>

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

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

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

     42        Web 窗体设计器生成的代码
     67
     68        private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
     69        {
     70        }

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

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

     84            else
     85            {
     86                this.DataGrid1.Columns[1].Visible=true;
     87            }

     88        }

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

     97        }

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

    105            else
    106            {
    107                if(ViewState["Order"].ToString()=="ASC")
    108                {
    109                    ViewState["Order"]="DESC";
    110                }

    111                else
    112                {
    113                    ViewState["Order"]="ASC";
    114                }

    115                
    116            }

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

    127    }

    128}

    129
  • 相关阅读:
    rMATs分析single-end 数据,结果文件为空?
    Error in inherits(x, "theme") : argument "e2" is missing, with no default
    R 变量名开头不能为数字
    linux 下的通配符和正则表达式不一样
    samtools的一些问题
    Error in C(1, 2) : object not interpretable as a factor
    grep -w 正确使用,结果却不正确的原因之一
    慎用rm命令和*
    定义默认字典值为列表类型
    C语言考题:Find the key in the picture,good luck..
  • 原文地址:https://www.cnblogs.com/icejd/p/373756.html
Copyright © 2011-2022 走看看