当前位置: 代码迷 >> 综合 >> 课程设计,自己写的DataGrid的几个功能
  详细解决方案

课程设计,自己写的DataGrid的几个功能

热度:22   发布时间:2023-12-12 04:32:59.0


当初为了课程设计,自己写的DataGrid的几个功能,费了好多时间弄懂的。
现在贴出来

########################################

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
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.Data.SqlClient;

using System.Data.OleDb;

namespace WebApplication1
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class WebForm1 : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Label Label2;
  protected System.Web.UI.WebControls.Label Label3;
  protected System.Web.UI.WebControls.TextBox TextBox2;
  protected System.Web.UI.WebControls.TextBox TextBox3;
  protected System.Web.UI.WebControls.TextBox TextBox4;
  protected System.Web.UI.WebControls.TextBox TextBox5;
  protected System.Web.UI.WebControls.Label Label5;
  protected System.Web.UI.WebControls.Label Label6;
  protected System.Web.UI.WebControls.Label Label7;
  protected System.Web.UI.WebControls.Label Label8;
  protected System.Web.UI.WebControls.Button Button2;
  protected System.Web.UI.WebControls.Label Label9;
  protected System.Web.UI.WebControls.TextBox TextBox6;
  protected System.Web.UI.WebControls.Label Label10;
  protected System.Web.UI.WebControls.DataGrid DataGrid1;
  protected System.Web.UI.WebControls.Button Button3; 
  protected System.Web.UI.WebControls.Label Label11;
  protected System.Web.UI.WebControls.Label Label12;
  protected System.Web.UI.WebControls.Label Label13;
  protected System.Web.UI.WebControls.Label Label14;
  protected System.Web.UI.WebControls.Button Button5;
  protected System.Web.UI.WebControls.Button Button6;
  protected System.Web.UI.WebControls.Label Label15;
  protected System.Web.UI.WebControls.Label Label1;

  SqlConnection conn=new SqlConnection("data source=fish_in_sky;database=stu_info;uid=sa;pwd=");
  SqlCommand myCommand=new SqlCommand();

  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
  }

  private void DataGridBind()
  {
   if(this.TextBox6.Text.Length==6||this.TextBox6.Text.Length==0)
   {
    conn.Open();
    SqlDataAdapter myAdapter=new SqlDataAdapter();
    if(this.TextBox6.Text.Length==0)
     myAdapter.SelectCommand=new SqlCommand("select * from info",conn);
    else
     myAdapter.SelectCommand=new SqlCommand("select * from info where 学号="+"'"+this.TextBox6.Text+"'",conn);
    DataSet ds=new DataSet();
    myAdapter.Fill(ds,"Categories");
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    conn.Close();
   }
   else
   {
    Response.Write("<script>alert('学号错误!');</script>");
      }
  }


  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button2.Click += new System.EventHandler(this.Button2_Click);
   this.Button3.Click += new System.EventHandler(this.Button3_Click);
   this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
   this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
   this.DataGrid1.Init += new System.EventHandler(this.Page_Load);
   this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
   this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
   this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
   this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
   this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
   this.Button5.Click += new System.EventHandler(this.Button5_Click);
   this.Button6.Click += new System.EventHandler(this.Button6_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  //"添加"按钮
  private void Button2_Click(object sender, System.EventArgs e)
  {
   if(this.TextBox2.Text.Length!=6) Response.Write("<script>alert('请输入学号!');</script>");
   else
   {
    if(this.TextBox3.Text.Length==0) Response.Write("<script>alert('请输入姓名!');</script>");
       else
    {
     if(this.TextBox4.Text.Length==0) Response.Write("<script>alert('请输入专业名!');</script>");
     else
     {
      if(this.TextBox5.Text.Length==0) Response.Write("<script>alert('请输入学分!');</script>");
      else
      {
       myCommand.CommandText="Insert into info values('"+Convert.ToString(this.TextBox2.Text)+"','"+Convert.ToString(this.TextBox3.Text)+"','"+Convert.ToString(this.TextBox4.Text)+"','"+Convert.ToString(this.TextBox5.Text)+"')";
       myCommand.Connection =conn;
       conn.Open();
       myCommand.ExecuteNonQuery();
       Response.Write("<script>alert('数据添加成功!');</script>");
       conn.Close();
       DataBind();
       clear_add_text();
      }
     }
       }
   }
  }

  //查询
  private void Button3_Click(object sender, System.EventArgs e)
  {
   DataGridBind();
  }

  private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   DataGrid1.CurrentPageIndex=e.NewPageIndex;
   DataGridBind();
  }

  //重置添加数据框
  private void Button5_Click(object sender, System.EventArgs e)
  {
            clear_add_text();
  }

  private void clear_add_text()
  {
   this.TextBox2.Text="";
   this.TextBox3.Text="";
   this.TextBox4.Text="";
   this.TextBox5.Text="";
  }

  //重置查询数据框
  private void Button6_Click(object sender, System.EventArgs e)
  {
   this.TextBox6.Text="";
  }

  //删除记录确认框
  private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   LinkButton myDeleteButton;
   switch(e.Item.ItemType)
   {
    case ListItemType.Item:
    case ListItemType.AlternatingItem:
     myDeleteButton = (LinkButton) e.Item.Cells[0].Controls[0];
     myDeleteButton.Attributes.Add("onclick", @"return confirm('记录将会被删除,你确认要删除吗?');");
     break;
   }
  }

  //删除记录
  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string key=(DataGrid1.DataKeys[(int)e.Item.ItemIndex]).ToString();
   myCommand.CommandText="DELETE FROM info WHERE (学号 = "+"'"+key+"'"+")";
   myCommand.Connection =conn;
   conn.Open();
   myCommand.ExecuteNonQuery();
   Response.Write("<script>alert('成功删除数据!');</script>");  
   conn.Close();
   if(DataGrid1.CurrentPageIndex-1>1)
   {
    DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex-1;
   }
   else
   {
    DataGrid1.CurrentPageIndex = 0;
   }
   DataGrid1.EditItemIndex=-1;
   DataGridBind();
  }

  //编辑功能
  private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex = e.Item.ItemIndex;
   DataGridBind();
  }

  //更新数据
  private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string key=(DataGrid1.DataKeys[(int)e.Item.ItemIndex]).ToString();
   TextBox txt_f2=(TextBox)e.Item.Cells[3].Controls[0];//姓名
   TextBox txt_f3=(TextBox)e.Item.Cells[4].Controls[0];//专业名
   TextBox txt_f4=(TextBox)e.Item.Cells[5].Controls[0];//总学分
   myCommand.CommandText="update info set 姓名="+"'"+txt_f2.Text+"'"+",专业名="+"'"+txt_f3.Text+"'"+",总学分="+"'"+txt_f4.Text+"'"+" where 学号="+"'"+key+"'";       
   myCommand.Connection =conn;
   conn.Open();
   myCommand.ExecuteNonQuery();
   conn.Close();
   Response.Write("<script>alert('数据修改成功!');</script>");
   DataGrid1.EditItemIndex=-1;
   DataGridBind();
  }

  //DG取消
  private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   DataGrid1.EditItemIndex=-1;
   DataGridBind();
  }
 }
}

 
  相关解决方案