我想在更新时删除选定的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;
}
}
现在我有一个任务,想要编辑它,我点击编辑,输入一些新值并点击更新:
但是更新完成后会显示以下内容:
答案 0 :(得分:0)
您的代码中的问题是您始终在中添加新行 数据表。
DataRow dr = _dt.NewRow();
当您将表格收回会话时会产生问题。在将其重新绑定到gridview
之前,您需要从dt中删除特定行 if (Session["DataTableAssignment"] != null)
{
_dt = (DataTable)Session["DataTableAssignment"];
DataRow row = _dt.Select("the condition")
_dt.Rows.Remove(row);
}
我希望它会对你有所帮助。