您的位置:首页 > 其它

GridView那点事之【内置的删除、编辑使用】

2013-10-18 21:47 288 查看


 

删除提示:在RowDataBound中实现

//如果是绑定数据行

            if (e.Row.RowType == DataControlRowType.DataRow)

            {

                if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)

                {

                    ((LinkButton)e.Row.Cells[5].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除该信息吗?')");
                }

            }

***********

如图,如何实现编辑、更改、取消或删除呢,在GridView中添加内置的编辑和删除,则在每一行的列都会相应显示。

下面我们来具体实现这些功能,很简单的。我们分别激活对应的事件即可。

      (1) //进行删除表格中某一行
        protected void LinkButtonDelete_Click(object sender, EventArgs e)//删除

        {

            GridViewRow gvr = (GridViewRow)((DataControlFieldCell)(((LinkButton)(sender)).Parent)).Parent;

            int index = Convert.ToInt32(gvr.Cells[0].Text) - 1;
           

            ((DataSet)(ViewState["DS_PRODUCTIONPLAN"])).Tables[0].Rows.RemoveAt(index);

            //((DataSet)(ViewState["DS_PRODUCTIONPLAN"])).相当于ds

            bind();

        }

        //编辑表格
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)//编辑该项

        {

            GridView1.EditIndex = e.NewEditIndex;
            bind();

        }

        //取消编辑
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)//取消修改

        {

            GridView1.EditIndex = -1;
            bind();

        }

        //编辑更改保存
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)  //更新

        {

            //((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();

            GridViewRow gvr = (GridViewRow)((DataControlFieldCell)(((LinkButton)(sender)).Parent)).Parent;//获取行号

            gvr.Cells[1].Text = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();
        }

 、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

===============================================================================================================================     

  protected void GV_file_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            // ((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["Num"] = decimal.Parse(((TextBox)GV_file.Rows[e.RowIndex].Cells[0].Controls[0]).Text.Trim());

            ((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["File"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[1].Controls[0]).Text.Trim();

            ((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["Duty"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[2].Controls[0]).Text.Trim();

            ((DataSet)(ViewState["Gv"])).Tables[0].Rows[e.RowIndex]["FileNum"] = ((TextBox)GV_file.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();

        

            GV_file.EditIndex = -1;

            GV_file.DataSource = ViewState["Gv"];

            GV_file.DataBind();

        }

**************************************************************************************************************************************************************************

 倘若需要更新数据库的数据记录时,我们应该在上面的RowUpdating()中添加相关的SQL语句Update。才能进行修改。如自己定义并调用更新数据函数

public bool UpdateData()

{

///更新数据库记录SQL的实现,这里不再阐述,明白这个实现过程的思路就可以了

}

例如:》》》》》》》》》》》》》》》

 protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)

        {

            Entity.TB_UnionOrganization E_TB_UnionOrganization = new Entity.TB_UnionOrganization();  

     //Entity.TB_UnionOrganization是三层架构中的model类
            E_TB_UnionOrganization.Id = Convert.ToDecimal(GV_WSH.Rows[e.RowIndex].Cells[11].Text.Trim());

            E_TB_UnionOrganization.WChairman = ((TextBox)GV_WSH.Rows[e.RowIndex].Cells[3].Controls[0]).Text.Trim();

            E_TB_UnionOrganization.WCochairman = ((TextBox)GV_WSH.Rows[e.RowIndex].Cells[4].Controls[0]).Text.Trim();

          

            if (B_EditUnionOrganization.UpdateOrganization(E_TB_UnionOrganization))
//调用修改数据库的函数
            { msg.AjaxResponeSrcipt(UpdatePanel1, this.GetType(), "修改成功"); bindGV_WSH(); bindCode(); }

            else

                msg.AjaxResponeSrcipt(UpdatePanel1, this.GetType(), "修改失败");

        }

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

//////////////////////**********内置删除**************************////////////////////////////////

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

        {

            int ID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[0].Text.Trim());

            DataAccessSQL delete = new DataAccessSQL();

            delete.deleteValue(ID);//实行删除数据库内容记录

            BindData();        //重新绑定

        }

************************实行删除数据库内容记录

 public void deleteValue(int ID)

        {

            string strSql = "Data Source=VQJREZV7DVSK2QA;Initial Catalog=gridviewAPP;User ID=sa;Password=admin@123456";

            SqlConnection connew = new SqlConnection(strSql);

            connew.Open();

            StringBuilder strDeletet = new StringBuilder();

            strDeletet.Append("delete from userInfo ");

            strDeletet.Append(" where Id=" + ID + "");

            SqlCommand cmd = new SqlCommand(strDeletet.ToString(),connew);

            cmd.ExecuteNonQuery();

            connew.Close();

        }

***************************//重新绑定

 protected void BindData()

        {

            DataAccessSQL selectData = new DataAccessSQL();

            DataSet ds = new DataSet();

            ds = selectData.DataAdapter();

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0 && ds != null)

            {

                ds.Tables[0].Columns.Add("gradeName", typeof(string));

                foreach (DataRow dr in ds.Tables[0].Rows)

                {

                    if (dr["gradeID"] != DBNull.Value)

                    {

                        dr["gradeName"] = selectData.GradeAdapter(Convert.ToInt32(dr["gradeID"].ToString()));

                    }

                }

                GridView1.DataSource = ds;

                GridView1.DataBind();

            }

            else

            {

                GridView1.DataSource = null;

                GridView1.DataBind();

            }

 

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐