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

C#
C#:小编详谈ASP.NET和JSP技术
C#:小编详谈StringBuilder
C#:使用CSS的8种技巧
C#:C#开发技巧之将图片存入数据库
C#:C#技术点之利用Image制作小动画
C#:C#开发技巧之如何根据年份判断十二生肖
C#:如何制作自动播放的MP3播放器
c#:C#技术利用鼠标绘图
C#:禁用鼠标左键
C#:如何使用匿名方法
C#:小编教你如何实现特殊形状的窗体
C#:在C#应用程序控制输入法
C#:小编教大家实现堆栈
C#:C#中数组知识点的精华
C#:小编谈C#中TextBox控件的应用技巧
C#:小编教大家设置货币值中使用的小数位数
C#:C#中实现倒计时功能
C#:小编教大家创建一个数字时钟
C#:小编教大家如何向ListView控件添加搜索功能
C#:小编浅谈如何在DataGridView控件中验证数据输入

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-08-14   浏览: 104 ::
收藏到网摘: 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();
 }
 
    }