当前位置: 首页 > 图文教程 > .Net技术 > C# > GridView 删除/更新/取消

C#
C#和Java的区别
提高C#编程水平的50个要诀
GridView 删除/更新/取消
c#线程
C#泛型有什么好处
总体了解C#
C#2.0匿名函数
GridView中添加一个CheckBox列
C#2.0介绍之Iterators(迭代器)
.NET与Java间进行Web Service交互的选择
C# 2010命名和可选参数的新特性
利用C#远程存取Access数据库
C#中foreach基础使用方法
C#中用鼠标移动页面功能的实现
C# 4.0中泛型协变性和逆变性详解
C#:C# .Net中的类型相互转换教程
C#:C#中的基元类型
C#:语言中的重要知识详细介绍与解释
C#:浅谈C#中的集合对象(Collections)
C#:C#发起邮件会议

C# 中的 GridView 删除/更新/取消


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 103 ::
收藏到网摘: n/a

//数据绑定 

private void InitData()
    {
 string sqlselect = "";
 XmlDocument document = new XmlDocument();
 document.Load(Server.MapPath("BannerList.xml"));
 XmlNodeList xn = document.SelectNodes("BannerList/item");
 string selch = "";selch = ddlchannel.SelectedItem.Value;
 if ( ddlchannel.SelectedItem.Value != "-1")
 {
 sqlselect = "select picid,picdiscription,piclink,picname,uploaddatetime,picchannel,picorder,isenjoin,banner,bannercode from flash where bannercode ='" selch " ' order by banner,picorder asc,picid desc";
 }
 else
 {
 sqlselect = "select picid,picdiscription,piclink,picname,uploaddatetime,picchannel,picorder,isenjoin,banner,bannercode from flash order by banner,picorder asc,picid desc";
 }
 string sqlselectnum = "select * from FlashItemNum";
 using (scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using ( sda = new SqlDataAdapter())
      {
   sda.SelectCommand = scn.CreateCommand();
   sda.SelectCommand.CommandType = CommandType.Text;
   sda.SelectCommand.CommandText = sqlselect;
   sda.SelectCommand.ExecuteNonQuery();
   ds = new DataSet();
   sda.Fill(ds,"list");
   gvFlashList.DataKeyNames = new string[] { "picid" };
   gvFlashList.DataSource = ds.Tables["list"];
   gvFlashList.DataBind();
   Label1.Text = "共" ds.Tables[0].Rows.Count.ToString() "条,共" gvFlashList.PageCount.ToString() "页,当前第" Convert.ToString(gvFlashList.PageIndex 1) "页";
   for ( int j=0; j<gvFlashList.Rows.Count; j )
       {   
    for (int i = 0; i <= 50; i )
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).Items.Add(new ListItem(i.ToString(), i.ToString()));
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).SelectedIndex = Convert.ToInt32(ds.Tables[0].Rows[j]["picorder"]);
    ((DropDownList)gvFlashList.Rows[j].FindControl("ddlorder")).Visible = false;
    for ( int k =0; k< xn.Count; k )
    {
        ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Items.Add(new ListItem(xn.Item(k).SelectSingleNode("option").InnerText, xn.Item(k).SelectSingleNode("option").Attributes["values"].Value.ToString()));
    }
    foreach ( ListItem li in ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Items)
        {
     if ( li.Value == ds.Tables[0].Rows[j]["bannercode"].ToString().Trim())
         {
      li.Selected = true;
      
         }
        }
    ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[j].FindControl("selcode")).Visible = false;
    if (((LinkButton)gvFlashList.Rows[j].FindControl("lnkbtn")).Text == "启用")
        {
     gvFlashList.Rows[j].Attributes.Add("style","background:#aaaaaa;");
     ((LinkButton)gvFlashList.Rows[j].Cells[5].Controls[0]).Enabled = false;
        }
     }
      sda.SelectCommand.CommandText = sqlselectnum;
      sda.SelectCommand.ExecuteNonQuery();
      sda.Fill(ds,"num");
      itemnum.Value = ds.Tables["num"].Rows[0]["itemnum"].ToString();
  }
     }
    }

 

//编辑
    protected void gvFlashList_RowEditing(object sender, GridViewEditEventArgs e)
    {
     gvFlashList.EditIndex = e.NewEditIndex;
     InitData();
     ((DropDownList)gvFlashList.Rows[e.NewEditIndex].FindControl("ddlorder")).Visible = true;
     ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.NewEditIndex].FindControl("spanorderid")).Visible = false;
     ((LinkButton)gvFlashList.Rows[e.NewEditIndex].FindControl("lnkbtn")).Visible = false;
     ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.NewEditIndex].FindControl("selcode")).Visible = true;
     ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.NewEditIndex].FindControl("lblcode")).Visible = false;

    }

 

//取消编辑
    protected void gvFlashList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
 gvFlashList.EditIndex = -1;
 InitData();
 ((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).Visible = false;
 ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.RowIndex].FindControl("spanorderid")).Visible = true;
 ((LinkButton)gvFlashList.Rows[e.RowIndex].FindControl("lnkbtn")).Visible = true;
 ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.RowIndex].FindControl("selcode")).Visible = false;
 ((System.Web.UI.HtmlControls.HtmlGenericControl)gvFlashList.Rows[e.RowIndex].FindControl("lblcode")).Visible = true;

    }

 

//更新
    protected void gvFlashList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
 string sqlupdate = "update flash_upload set picdiscription='" ((TextBox)gvFlashList.Rows[e.RowIndex].Cells[1].Controls[0]).Text "',piclink='" ((TextBox)gvFlashList.Rows[e.RowIndex].Cells[2].Controls[0]).Text "',picorder=" ((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).Items[((DropDownList)gvFlashList.Rows[e.RowIndex].FindControl("ddlorder")).SelectedIndex].Value ",banner='" ((System.Web.UI.HtmlControls.HtmlSelect)gvFlashList.Rows[e.RowIndex].FindControl("selcode")).Value.ToString() "' where picid=" gvFlashList.DataKeys[e.RowIndex].Value;
 int updnum;
 using ( scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using ( sda = new SqlDataAdapter())
  {
      sda.UpdateCommand = scn.CreateCommand();
      sda.UpdateCommand.CommandType = CommandType.Text;
      sda.UpdateCommand.CommandText = sqlupdate;
      updnum = sda.UpdateCommand.ExecuteNonQuery();
  }
     }
 if ( updnum > 0)
 {
 gvFlashList.EditIndex = -1;
 Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(\"更新成功!\");</script>");
 }
 else
 {
     Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(\"更新失败!\");</script>");
 }
 InitData();
    }

//删除
    protected void gvFlashList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
 string sqldelete = "delete from flash_upload  where picid=" gvFlashList.DataKeys[e.RowIndex].Value;
 int delnum;
 using (scn = new SqlConnection())
 {
     scn.ConnectionString = connectionstring;
     scn.Open();
     using (sda = new SqlDataAdapter())
     {
  sda.SelectCommand = scn.CreateCommand();
  sda.SelectCommand.CommandType = CommandType.Text;
  sda.SelectCommand.CommandText = sqldelete;
  delnum = sda.SelectCommand.ExecuteNonQuery();
     }
 }
 if (delnum > 0)
 {
     Page.ClientScript.RegisterStartupScript(Page.GetType(),"","<script>alert(\"删除成功!\");</script>");
 }
 InitData();
 
    }

 

//数据绑定
    protected void gvFlashList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
 if (e.Row.RowType == DataControlRowType.DataRow)
     {
  if (e.Row.RowState == DataControlRowState.Alternate || e.Row.RowState == DataControlRowState.Normal)
      {
   ((LinkButton)e.Row.Cells[6].Controls[0]).Attributes.Add("onclick","javascript:return confirm(\"您确定要删除“" e.Row.Cells[1].Text  "”吗?\");");
   LinkButton lnkb = new LinkButton();
   lnkb = ((LinkButton)e.Row.Cells[7].FindControl("lnkbtn"));
   lnkb.CommandArgument = e.Row.RowIndex.ToString();
      }
     }
    }

 

//命令
    protected void gvFlashList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
 if (e.CommandName == "lnkbtnenjone")
 {
     string btnname = ((LinkButton)gvFlashList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[7].FindControl("lnkbtn")).Text;
     int bitnum = 0;
     if ( btnname == "启用")
     {
  bitnum = 1;
     }
     string strupdateenjone = "update flash_upload set isenjoin=" bitnum.ToString() " where picid=" gvFlashList.DataKeys[Convert.ToInt32(e.CommandArgument)].Value;
     using (scn = new SqlConnection())
     {
  scn.ConnectionString = connectionstring;
  scn.Open();
  using (sda = new SqlDataAdapter())
  {
      sda.UpdateCommand = scn.CreateCommand();
      sda.UpdateCommand.CommandType = CommandType.Text;
      sda.UpdateCommand.CommandText = strupdateenjone;
      sda.UpdateCommand.ExecuteNonQuery();
  }
     }
InitData();
 }
 
    }