3

我想在更新时删除选定的 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;
        }
    }

现在我有一个作业,想编辑它,我点击编辑,输入一些新值,然后点击更新: 在此处输入图像描述

但是更新完成后会显示以下内容: 在此处输入图像描述

4

1 回答 1

0

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

 DataRow dr = _dt.NewRow();

在此处输入图像描述

当您将表带回会话时会产生问题。您需要先从 dt 中删除特定行,然后再将其重新绑定到 gridview

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

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


 }

我希望它会帮助你。

于 2013-04-05T08:58:59.410 回答