gpt4 book ai didi

c# - Gridview 删除选中的行

转载 作者:行者123 更新时间:2023-11-30 18:34:45 27 4
gpt4 key购买 nike

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

当您将表取回 session 时会产生问题。在将其重新绑定(bind)到 gridview 之前,您需要从 dt 中删除特定行

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

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


}

希望对您有所帮助。

关于c# - Gridview 删除选中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15829287/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com