[英]Delete Individual row of gridview without deleting from database
I am using Linq-to-SQL in ASP.NET & C#. 我在ASP.NET和C#中使用Linq-to-SQL。 I want to delete individual row of gridview by click on delete button (ie
CommandField
or TemplateField
) with gridTrainingNeed_RowDeleting
event. 我想通过单击带有
gridTrainingNeed_RowDeleting
事件的删除按钮(即CommandField
或TemplateField
)来删除gridview的单个行。
Here is my code for loading gridview on page load. 这是我在页面加载时加载gridview的代码。
int departmentId;
int branchId;
int positionId;
double month;
int levelId;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Initialize();
}
}
public void Initialize()
{
ddlBranch.DataSource = TrainingManager.GetBranchName();
ddlBranch.DataBind();
ddlDepartment.DataSource = DepartmentManager.GetAllDepartments();
ddlDepartment.DataBind();
ddlPosition.DataSource = TrainingManager.GetDesignationName();
ddlPosition.DataBind();
ddlLevel.DataSource = TrainingManager.GetLevelName();
ddlLevel.DataBind();
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataBind();
}
Now I am going to keep my design code: 现在,我要保留我的设计代码:
<div class="innerLR">
<div class="seperator bottom" />
<div class="widget">
<div class="widget-head">
<h4 class="heading">
Send Circulation To
</h4>
</div>
<div class="widget-body">
<table class="tableShow" cellpadding="3" cellspacing="0">
<tr>
<td>
<asp:DropDownList ID="ddlBranch" runat="server" DataTextField="Name"
DataValueField="BranchId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Branch---"/>
</asp:DropDownList>
</td>
<td>
<asp:DropDownList ID="ddlDepartment" runat="server" AppendDataBoundItems="True"
DataTextField="Name" Width="170px" DataValueField="DepartmentId">
<asp:ListItem Text="---Select Department---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlLevel" runat="server" DataTextField="Name"
DataValueField="LevelId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Level---"/>
</asp:DropDownList></td>
<td>
<asp:DropDownList ID="ddlPosition" runat="server" DataTextField="Name"
DataValueField="DesignationId" Width="170px" AppendDataBoundItems="True"
>
<asp:ListItem Text="---Select Position---"/>
</asp:DropDownList></td>
</tr>
<tr>
<td>
<br />
Send Circulation To</td>
<td>
<br />
Month</td>
<td>
</td>
<td>
</td>
</tr>
<tr>
<td>
<asp:DropDownList ID="ddlCirculation" runat="server" DataTextField="Name"
DataValueField="PositionId" Width="170px" AutoPostBack="true" AppendDataBoundItems="True"
onselectedindexchanged="ddlCirculation_SelectedIndexChanged">
<asp:ListItem Text="" Value="-1" />
<asp:ListItem Text="All" Value="0" />
<asp:ListItem Text="Must Have Worked For" Value="1" />
</asp:DropDownList></td>
<td>
<asp:TextBox ID="txtMonth" Width="150px" runat="server" Enabled="false"></asp:TextBox></td>
<td>
<asp:Button ID="btnShow" runat="server" Text="Show" onclick="btnShow_Click" /></td>
<td>
</td>
</tr>
</table>
</div>
</div>
<div>
<asp:GridView ID="gridTrainingNeed" runat="server" AutoGenerateColumns="False"
EnableModelValidation="True" CellPadding="4" ForeColor="#333333"
GridLines="None" >
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="EmployeeId" HeaderText="EID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Branch" HeaderText="Branch" />
<asp:BoundField DataField="Department" HeaderText="Department" />
<asp:BoundField DataField="Level" HeaderText="Level" />
<asp:BoundField DataField="Position" HeaderText="Position" />
<asp:CommandField HeaderText="Delete" ShowCancelButton="False"
ShowDeleteButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
</div>
</div>
Thus, I want to delete row one by one with RowDeleting Event from displaying data on gridview not from database. 因此,我想通过RowDeleting事件从显示在gridview上的数据而不是从数据库中删除一行。
protected void gridTrainingNeed_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//Row delete code
}
Thank You. 谢谢。
Instead of assigning the DataSource
directly like you are doing: 不用像您那样直接分配
DataSource
:
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
Store the result in Page level List. 将结果存储在页面级别列表中。 Define a page level List like:
定义页面级别列表,例如:
//Define List at page/class level
List<Employee> gridList = new List<Employee>();
Then in your method Initialize
然后在你的方法中
Initialize
gridList = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataSource = gridList;
gridTrainingNeed.DataBind();
You need to persist the List<Employee>
in memory on PostBack, Use Session/ViewState etc. (See ASP.Net State Management ). 您需要将
List<Employee>
保留在PostBack的内存中,使用Session / ViewState等。(请参见ASP.Net状态管理 )。
Later in your gridTrainingNeed_RowDeleting
, get the ID
from row or any field that uniquely identify an employee. 稍后在
gridTrainingNeed_RowDeleting
,从行或唯一标识员工的任何字段中获取ID
。 Query your list and remove that object from the List in memory. 查询您的列表,然后从内存中的列表中删除该对象。 Re-assign the DataSource to the modified list and call DataBind.
将数据源重新分配给修改后的列表,然后调用DataBind。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.