C#里的DataGrid的使用

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Web.Caching;
using System.Web.Security;

namespace EduAdmin.Teacher.MainTeacher
{
 ///


 /// Add 的摘要说明。
 ///

 public class Add : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox Textbox5;
  protected System.Web.UI.WebControls.RadioButton RadioButton1;
  protected System.Web.UI.WebControls.RadioButton RadioButton2;
  protected System.Web.UI.WebControls.DropDownList DropDownList1;
  protected System.Web.UI.WebControls.DropDownList DropDownList2;
  protected System.Web.UI.WebControls.DropDownList DropDownList3;
  protected System.Web.UI.WebControls.ImageButton ImageButton1;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.ImageButton ImageButton2;
  protected System.Web.UI.WebControls.ImageButton Imagebutton2;
  protected System.Web.UI.WebControls.Panel Panel1;
 
  private DataTable dtTeachers;  //教师表
  private DataTable dtDepartments; //系表
  private DataTable dtMajors;   //专业表

  private ArrayList arrListNative; //籍贯
  private ArrayList arrListDiploma; //学历

  protected System.Web.UI.WebControls.TextBox tbTid;
  protected System.Web.UI.WebControls.DropDownList DropDownList4;
  protected System.Web.UI.WebControls.DataList DataList1;
  protected System.Web.UI.WebControls.TextBox tbTname;   

  //构造内存数据表
  private void BuildDataTable()
  {
   dtTeachers=(DataTable)this.Cache["Teachers"];
   dtDepartments=(DataTable)this.Cache["Departments"];
   dtMajors=(DataTable)this.Cache["Majors"];
   arrListNative=(ArrayList)this.Cache["Native"];
   arrListDiploma=(ArrayList)this.Cache["Diploma"];
   
   if(dtTeachers==null)
   {
    DataSet dsEduAdmin=new DataSet();

    dtTeachers=new DataTable();
    dtDepartments=new DataTable();
    dtMajors=new DataTable();

    //构造教师表 
    DataColumn dcID=new DataColumn();
    dcID.ColumnName="Tid";
    dcID.DataType=System.Type.GetType("System.String");
    dcID.MaxLength=5;
    dcID.Unique=true;
    dcID.AllowDBNull=false;

    DataColumn dcName=new DataColumn();
    dcName.ColumnName="Tname";
    dcName.DataType=System.Type.GetType("System.String");
    dcName.MaxLength=20;

    DataColumn dcSex=new DataColumn();
    dcSex.ColumnName="sex";
    dcSex.DataType=System.Type.GetType("System.String");
    dcSex.MaxLength=2;

    DataColumn dcNative=new DataColumn();
    dcNative.ColumnName="Native";
    dcNative.DataType=System.Type.GetType("System.String");
    dcNative.MaxLength=20;

    DataColumn dcAddress=new DataColumn();
    dcAddress.ColumnName="Address";
    dcAddress.DataType=System.Type.GetType("System.String");
    dcAddress.MaxLength=100;

    DataColumn dcDname=new DataColumn();
    dcDname.ColumnName="Dname";
    dcDname.DataType=System.Type.GetType("System.String");
    dcDname.MaxLength=20;

    DataColumn dcMname=new DataColumn();
    dcMname.ColumnName="Mname";
    dcMname.DataType=System.Type.GetType("System.String");
    dcMname.MaxLength=20;

    DataColumn dcDiploma=new DataColumn();
    dcDiploma.ColumnName="Diploma";
    dcDiploma.MaxLength=10;

    dtTeachers.Columns.Add(dcID);
    dtTeachers.Columns.Add(dcName);
    dtTeachers.Columns.Add(dcSex);
    dtTeachers.Columns.Add(dcNative);
    dtTeachers.Columns.Add(dcAddress);
    dtTeachers.Columns.Add(dcDname);
    dtTeachers.Columns.Add(dcMname);
    dtTeachers.Columns.Add(dcDiploma);

    dsEduAdmin.Tables.Add(dtTeachers);

    //构造系表和专业表
    SqlDataAdapter daDepartments=new SqlDataAdapter("select distinct Dname from DepartmentMajors",ConfigurationSettings.AppSettings["ConnectionString"]);
    SqlDataAdapter daMajors=new SqlDataAdapter("select Dname,Mname from DepartmentMajors",ConfigurationSettings.AppSettings["ConnectionString"]);    
    
    daDepartments.Fill(dsEduAdmin,"Departments");
    daMajors.Fill(dsEduAdmin,"Majors");
    dtDepartments=dsEduAdmin.Tables["Departments"];
    dtMajors=dsEduAdmin.Tables["Majors"];

    this.Cache["Teachers"]=dtTeachers;
    this.Cache["Departments"]=dtDepartments;
    this.Cache["Majors"]=dtMajors;

    arrListNative=new ArrayList();
    arrListNative.Insert(0,"===请选择籍贯===");
    arrListNative.Insert(1,"北京");
    arrListNative.Insert(2,"上海");
    arrListNative.Insert(3,"天津");
    arrListNative.Insert(4,"重庆");
    arrListNative.Insert(5,"广东");
    arrListNative.Insert(6,"广西");
    arrListNative.Insert(7,"湖南");
    arrListNative.Insert(8,"湖北");
    this.Cache["Native"]=arrListNative;

    arrListDiploma=new ArrayList();
    arrListDiploma.Insert(0,"===请选择学历===");
    arrListDiploma.Insert(1,"本科");
    arrListDiploma.Insert(2,"硕士");
    arrListDiploma.Insert(3,"博士");
    arrListDiploma.Insert(4,"博士后");
    this.Cache["Diploma"]=arrListDiploma;
   }

  }


  private void Page_Load(object sender, System.EventArgs e)
  {
   this.BuildDataTable();

   if(!this.Page.IsPostBack)
   {
    this.Panel1.Visible=false;

    this.DropDownList4.DataSource=dtDepartments;
    this.DropDownList4.DataTextField="Dname";
    this.DropDownList4.DataValueField="Dname";
    this.DropDownList4.DataBind();
    this.DropDownList4.Items.Insert(0,"===请选择所在系===");

    DataView dvMajors=new DataView(dtMajors);
    dvMajors.RowFilter="Dname='" + this.DropDownList4.SelectedValue + "'";
    this.DropDownList2.DataSource=dvMajors;
    this.DropDownList2.DataTextField="Mname";
    this.DropDownList2.DataValueField="Mname";
    this.DropDownList2.DataBind();
    this.DropDownList2.Items.Insert(0,"===请选择所在教研室===");
   }
   else
   {
    this.Panel1.Visible=true;
   }

  }

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


  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  ///

  private void InitializeComponent()
  {   
   this.DropDownList4.SelectedIndexChanged += new System.EventHandler(this.DropDownList4_SelectedIndexChanged);
   this.ImageButton1.Click += new System.Web.UI.ImageClickEventHandler(this.ImageButton1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void ImageButton1_Click(object sender, System.Web.UI.ImageClickEventArgs e)
  {
   DataRow drTemp=dtTeachers.NewRow();
            drTemp[0]=this.tbTid.Text;
   drTemp[1]=this.tbTname.Text;

   if(this.RadioButton1.Checked)
    drTemp[2]="男";
   else
    drTemp[2]="女";

   drTemp[3]=this.DropDownList1.SelectedValue;
   drTemp[4]=this.Textbox5.Text;
   drTemp[5]=this.DropDownList4.SelectedValue;
   drTemp[6]=this.DropDownList2.SelectedValue;
   drTemp[7]=this.DropDownList3.SelectedValue;

   dtTeachers.Rows.Add(drTemp);
   int i=dtTeachers.Rows.Count;

   //创建一个绝对过期策略
   this.Cache.Insert("iPos",0,null,DateTime.Now.AddSeconds(1),Cache.NoSlidingExpiration);

   this.DataGrid1.DataSource=dtTeachers.DefaultView;
   this.DataGrid1.DataBind();

   this.tbTid.Text="";
   this.tbTname.Text="";
   this.Textbox5.Text="";
   this.RadioButton1.Checked=true;
   this.DropDownList1.SelectedIndex=0;
   this.DropDownList2.Items.Insert(0,"===请选择所在教研室===");
   this.DropDownList2.SelectedIndex=0;
   this.DropDownList3.SelectedIndex=0;
   this.DropDownList4.SelectedIndex=0;
  }

  private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {   
   if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
   {
    ImageButton btnDelete=(ImageButton)e.Item.FindControl("btnDelete");
    btnDelete.Attributes.Add("onclick","return confirm('您真的要删除该行吗?');");

    RadioButton rbFemale=(RadioButton)e.Item.FindControl("rbFemale");  
    RadioButton rbMale=(RadioButton)e.Item.FindControl("rbMale");
    
    int i=(int)this.Cache["iPos"];
    DataRow dr=dtTeachers.Rows[i];
    if(dr["sex"].ToString()=="男")
    {
     rbMale.Checked=true;
    }
    else if(dr["sex"].ToString()=="女")
    {
     rbFemale.Checked=true;
    }
    
    this.Cache["iPos"]=((int)this.Cache["iPos"])+1;

    DropDownList ddlNative=(DropDownList)e.Item.FindControl("ddlNative");
    ddlNative.DataSource=(ArrayList)this.Cache["Native"];
    ddlNative.DataBind();
    ddlNative.SelectedIndex=((ArrayList)this.Cache["Native"]).IndexOf(dr["Native"].ToString(),0);

    DropDownList ddlDepartment=(DropDownList)e.Item.FindControl("ddlDepartment");
    ddlDepartment.DataSource=dtDepartments;
    ddlDepartment.DataTextField="Dname";
    ddlDepartment.DataValueField="Dname";
    ddlDepartment.DataBind();
    ddlDepartment.SelectedValue=dr["Dname"].ToString();

    DropDownList ddlMajor=(DropDownList)e.Item.FindControl("ddlMajor");
    DataView dvMajors=new DataView(dtMajors);
    dvMajors.RowFilter="Dname='" + ddlDepartment.SelectedValue + "'";
    ddlMajor.DataSource=dvMajors;
    ddlMajor.DataTextField="Mname";
    ddlMajor.DataValueField="Mname";
    ddlMajor.DataBind();
    ddlMajor.SelectedValue=dr["Mname"].ToString();

    DropDownList ddlDiploma=(DropDownList)e.Item.FindControl("ddlDiploma");
    ddlDiploma.DataSource=(ArrayList)this.Cache["Diploma"];
    ddlDiploma.DataBind();
    ddlDiploma.SelectedIndex=((ArrayList)this.Cache["Diploma"]).IndexOf(dr["Diploma"].ToString(),0);    

    foreach(TableCell tc in e.Item.Cells)
    {
     tc.Attributes.Add("nowrap","true");
    }
   }
  }


  private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if(e.CommandName=="Edit")
   {
    foreach(DataGridItem dgi in this.DataGrid1.Items)
    {
     if(dgi.ItemIndex==e.Item.ItemIndex)
     {      
      TextBox tbID=(TextBox)dgi.FindControl("tbID");
      tbID.ReadOnly=false;

      TextBox tbName=(TextBox)dgi.FindControl("tbName");
      tbName.ReadOnly=false;

      RadioButton rbMale=(RadioButton)dgi.FindControl("rbMale");
      rbMale.Enabled=true;

      RadioButton rbFemale=(RadioButton)dgi.FindControl("rbFemale");
      rbFemale.Enabled=true;

      DropDownList ddlNative=(DropDownList)dgi.FindControl("ddlNative");
      ddlNative.Enabled=true;

      TextBox tbAddress=(TextBox)dgi.FindControl("tbAddress");
      tbAddress.ReadOnly=false;

      DropDownList ddlDepartment=(DropDownList)dgi.FindControl("ddlDepartment");
      ddlDepartment.Enabled=true;

      DropDownList ddlMajor=(DropDownList)dgi.FindControl("ddlMajor");
      ddlMajor.Enabled=true;

      DropDownList ddlDiploma=(DropDownList)dgi.FindControl("ddlDiploma");
      ddlDiploma.Enabled=true;     
     }
     else
     {
      TextBox tbID=(TextBox)dgi.FindControl("tbID");
      tbID.ReadOnly=true;

      TextBox tbName=(TextBox)dgi.FindControl("tbName");
      tbName.ReadOnly=true;

      RadioButton rbMale=(RadioButton)dgi.FindControl("rbMale");
      rbMale.Enabled=false;

      RadioButton rbFemale=(RadioButton)dgi.FindControl("rbFemale");
      rbFemale.Enabled=false;

      DropDownList ddlNative=(DropDownList)dgi.FindControl("ddlNative");
      ddlNative.Enabled=false;

      TextBox tbAddress=(TextBox)dgi.FindControl("tbAddress");
      tbAddress.ReadOnly=true;

      DropDownList ddlDepartment=(DropDownList)dgi.FindControl("ddlDepartment");
      ddlDepartment.Enabled=false;

      DropDownList ddlMajor=(DropDownList)dgi.FindControl("ddlMajor");
      ddlMajor.Enabled=false;

      DropDownList ddlDiploma=(DropDownList)dgi.FindControl("ddlDiploma");
      ddlDiploma.Enabled=false;      
     }
    }    

   }
   else if(e.CommandName=="Delete")
   {
    dtTeachers.Rows.RemoveAt(e.Item.ItemIndex);
    if(dtTeachers.Rows.Count<1)
    {
     this.Panel1.Visible=false;
    }
    else
    {
     this.Cache["iPos"]=0;
     this.DataGrid1.DataSource=dtTeachers;
     this.DataGrid1.DataBind();
    }
   }
   else if(e.CommandName=="Cancel")
   {
    //
   }
   else if(e.CommandName=="OK")
   {
    //
   }
  }

  private void DropDownList4_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   DataView dvMajors=new DataView(dtMajors);
   dvMajors.RowFilter="Dname='" + this.DropDownList4.SelectedValue + "'";
   this.DropDownList2.DataSource=dvMajors;
   this.DropDownList2.DataTextField="Mname";
   this.DropDownList2.DataValueField="Mname";
   this.DropDownList2.DataBind();     
  }
 }
}
 

你可能感兴趣的:(C#)