Gridview删除选定的行

Gridview删除选定的行

问题描述:

我想在更新时删除选定的gridview行,但不知道如何去做。Gridview删除选定的行

我的更新代码:

//Update selected assignment 
    protected void ButtonUpdateAssignmentClick(object sender, EventArgs e) 
    {   
     try 
     { 
      using (var db = new KnowItCvdbEntities()) 
      { 
       SPWeb theSite = SPControl.GetContextWeb(Context); 
       SPUser theUser = theSite.CurrentUser; 
       string strUserName = theUser.LoginName; 

       var theEmplAssignment = (
               from p 
                in db.EMPLOYEES 
               where p.username == strUserName 
               select p).FirstOrDefault(); 

       _emp = theEmplAssignment; 

       if (_emp != null) 
       { 
        int assignmentId = Convert.ToInt32(HiddenField_Assignment_Id.Value); 

        var theEmplAssignmentName = 
               (from p in db.EMPLOYEES_ASSIGNMENT 
               where p.employee_id == _emp.employee_id && p.assignment_id == assignmentId 
               select p).First(); 

        if (theEmplAssignmentName != null) 
        { 
         theEmplAssignmentName.company_name = TextBoxCompanyName.Text; 
         theEmplAssignmentName.sector = TextBoxSector.Text; 
         theEmplAssignmentName.area = TextBoxArea.Text; 
         theEmplAssignmentName.from_date = TextBoxFromDate.Text; 
         theEmplAssignmentName.to_date = TextBoxToDate.Text; 
         theEmplAssignmentName.reference_name = TextBoxReference.Text; 
         theEmplAssignmentName.description = TextBoxDesc.Text; 

         db.SaveChanges(); 

         //Populate gridview 
         if (Session["DataTableAssignment"] != null) 
         { 
          _dt = (DataTable)Session["DataTableAssignment"]; 
         } 
         else 
         { 
          _dt.Columns.Add("Company name"); 
          _dt.Columns.Add("Sector"); 
          _dt.Columns.Add("Area"); 
          _dt.Columns.Add("From"); 
          _dt.Columns.Add("To"); 
          _dt.Columns.Add("Tools"); 
          _dt.Columns.Add("Technology"); 
          _dt.Columns.Add("Description"); 
          _dt.Columns.Add("Reference"); 
          _dt.Columns.Add("AssignmentId"); 
         } 
         //dt.Rows.Clear(); 

         DataRow dr = _dt.NewRow(); 
         dr["Company name"] = theEmplAssignmentName.company_name; 
         dr["Sector"] = theEmplAssignmentName.sector; 
         dr["Area"] = theEmplAssignmentName.area; 
         dr["From"] = theEmplAssignmentName.from_date; 
         dr["To"] = theEmplAssignmentName.to_date; 
         dr["Description"] = theEmplAssignmentName.description; 
         dr["Reference"] = theEmplAssignmentName.reference_name; 
         dr["AssignmentId"] = theEmplAssignmentName.assignment_id; 

         List<ASSIGNMENT_TOOLS> assignmentTools = (from p in db.ASSIGNMENT_TOOLS 
                    where 
                     p.employee_id == _emp.employee_id && 
                     p.assignment_id == assignmentId 
                    select p).ToList(); 

         string sToolValue = string.Empty; 
         foreach (var vTool in assignmentTools) 
         { 
          sToolValue += vTool.tool_name + ", "; 
         } 
         dr["Tools"] = sToolValue; 


         List<ASSIGNMENT_TECHNOLOGY> assignmentTech = (from p in db.ASSIGNMENT_TECHNOLOGY 
                     where 
                      p.employee_id == _emp.employee_id && 
                      p.assignment_id == assignmentId 
                     select p).ToList(); 

         string sTechValue = string.Empty; 
         foreach (var vTech in assignmentTech) 
         { 
          sTechValue += vTech.technology_name + ", "; 
         } 
         dr["Technology"] = sTechValue; 

         _dt.Rows.Add(dr); 
         Session["DataTableAssignment"] = _dt; 

         GridViewShowAssignments.DataSource = _dt; 
         GridViewShowAssignments.DataBind(); 

         TextBoxCompanyName.Text = string.Empty; 
         TextBoxSector.Text = string.Empty; 
         TextBoxArea.Text = string.Empty; 
         TextBoxFromDate.Text = string.Empty; 
         TextBoxToDate.Text = string.Empty; 
         TextBoxDesc.Text = string.Empty; 
         TextBoxReference.Text = string.Empty; 
         ListBoxAssignmentTools.Items.Clear(); 
         ListBoxAssignmentTechnology.Items.Clear(); 
        } 
       } 
      } 
     } 
     catch (Exception x) 
     { 
      LabelProvAssignment.Text = x.Message; 
     } 
    } 

现在,我得到了一个任务,并要对其进行编辑,我点击编辑,输入一些新值,并单击更新: enter image description here

但是当更新完成后显示如下: enter image description here

代码中的问题是,您总是在 数据表中添加新行。

DataRow dr = _dt.NewRow(); 

enter image description here

当你正在回表会话创建它的问题。你需要从DT删除特定的行,然后重新绑定到gridview

if (Session["DataTableAssignment"] != null) 
{ 
    _dt = (DataTable)Session["DataTableAssignment"];     

     DataRow row = _dt.Select("the condition") 
     _dt.Rows.Remove(row); 


} 

我希望它会帮助你。

+0

我有一个调用回发DataTable的方法,完全忘了它:)因此,不是添加新行,而是将它称为'GetAssignment();'现在它可以工作。感谢您的提示=) – Kriistiian 2013-04-05 11:05:05