[英]Delete Individual row of gridview without deleting from database
我在ASP.NET和C#中使用Linq-to-SQL。 我想通過單擊帶有gridTrainingNeed_RowDeleting
事件的刪除按鈕(即CommandField
或TemplateField
)來刪除gridview的單個行。
這是我在頁面加載時加載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();
}
現在,我要保留我的設計代碼:
<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>
因此,我想通過RowDeleting事件從顯示在gridview上的數據而不是從數據庫中刪除一行。
protected void gridTrainingNeed_RowDeleting(object sender, GridViewDeleteEventArgs e)
{//Row delete code
}
謝謝。
不用像您那樣直接分配DataSource
:
gridTrainingNeed.DataSource = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
將結果存儲在頁面級別列表中。 定義頁面級別列表,例如:
//Define List at page/class level
List<Employee> gridList = new List<Employee>();
然后在你的方法中Initialize
gridList = TrainingManager.GetAllEmployees(departmentId, branchId, positionId, month, levelId).ToList();
gridTrainingNeed.DataSource = gridList;
gridTrainingNeed.DataBind();
您需要將List<Employee>
保留在PostBack的內存中,使用Session / ViewState等。(請參見ASP.Net狀態管理 )。
稍后在gridTrainingNeed_RowDeleting
,從行或唯一標識員工的任何字段中獲取ID
。 查詢您的列表,然后從內存中的列表中刪除該對象。 將數據源重新分配給修改后的列表,然后調用DataBind。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.