asp.net操作GridView添删改查的两种方法 及 光棒效果

     这部份小内容很想写下来了,因为是基础中的基础,但是近来用的比较少,又温习了一篇,发现有点陌生了,所以,还是写一下吧。

方法一:使用Gridview本身自带的事件处理,代码如下(注意:每次操作完都得重新绑定,且删除列要转为模板列): 

protected void Page_Load(object sender, EventArgs e)
   {
       if (!Page.IsPostBack)
       {
           ShowCategories();
           ShowProducts();
       }
   }
 
   private void ShowProducts()
   {
       DataTable dt = NorthWind.DBHelp.GetTable("SELECT Product.产品ID, Product.产品名称, Supply.公司名称, Supply.城市, Category.类别名称, Category.图片, Product.单位数量, Product.单价, Product.库存量, Product.中止 FROM Category INNER JOIN Product ON Category.类别ID = Product.类别ID INNER JOIN Supply ON Product.供应商ID = Supply.供应商ID where Product.类别ID="+int.Parse(this.DropDownList1.SelectedValue));
       this.GridView1.DataSource = dt;
       this.GridView1.DataKeyNames = new string[]{"产品ID"};//设置数据操作的主键列
       this.GridView1.DataBind();
   }
 
   private void ShowCategories()
   {
       DataTable dt = NorthWind.DBHelp.GetTable("select * from Category");
       this.DropDownList1.DataSource = dt;
       this.DropDownList1.DataTextField = "类别名称";
       this.DropDownList1.DataValueField = "类别ID";
       this.DropDownList1.DataBind();
   }
   protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
   {
       ShowProducts();
   }
   protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
 
   }
   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
   {
       //将当前要编辑的行的索引告诉GridView
       this.GridView1.EditIndex = e.NewEditIndex;
       ShowProducts();
   }
   protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
       //更新数据库
       decimal price = decimal.Parse(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].FindControl("TextBox1")).Text);
       int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
       //更新到数据库
       NorthWind.DBHelp.ExecuteNoQuery("update Product set 单价="+price+" where 产品ID="+productid);
       //重新绑定
       this.GridView1.EditIndex = -1;
       ShowProducts();
   }
   protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   {
       this.GridView1.EditIndex = -1;
       ShowProducts();
   }
   protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
   {
       //删除数据
       int productid = int.Parse(this.GridView1.DataKeys[e.RowIndex].Value.ToString());//获取当前行的主键
       //更新到数据库
       NorthWind.DBHelp.ExecuteNoQuery("delete from Product where 产品ID=" + productid);
       //重新绑定
       this.GridView1.EditIndex = -1;
       ShowProducts();
   }
   protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
   {
       //当我们对数据源中的每一行进行数据绑定时触发的事件
       if (e.Row.RowType == DataControlRowType.DataRow)//判断绑定时候该行的状态时数据行还是其他类型的模板
       {
           //添加客户端确认
           LinkButton lnkDelete = (LinkButton)e.Row.Cells[10].FindControl("lnkDelete");
           lnkDelete.Attributes.Add("onclick", "return confirm('你确认删除吗?')");
           //光棒效果
           e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#ff66cc';");
           e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
       }
   }
   protected void chkSelectAll_CheckedChanged(object sender, EventArgs e)
   {
       //获取全选的复选框
       CheckBox chkSelectAll = (CheckBox)sender;
       //获取每一行的复选框
       for (int i = 0; i < this.GridView1.Rows.Count; i++)
       {
           CheckBox chkSelect = (CheckBox)this.GridView1.Rows[i].Cells[11].FindControl("chkSelect");
           //修改状态
           chkSelect.Checked = chkSelectAll.Checked;
       }
   }
   protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
   {
       //告诉GridView当前显示的数据是第几页的数据
       this.GridView1.PageIndex= e.NewPageIndex;
       ShowProducts();
   }

 

posted @   酒沉吟  阅读(360)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示