如何从网格面板中删除选定的行以及数据库中的相应数据?
Posted
技术标签:
【中文标题】如何从网格面板中删除选定的行以及数据库中的相应数据?【英文标题】:How to delete selected rows from a gridpanel and, corresponding data from database? 【发布时间】:2014-01-23 08:05:07 【问题描述】:我的代码是:
<ext:GridPanel ID="GridEmployees" runat="server" Width="700" Title="Employees">
<Store>
<ext:Store ID="StoreEmployees" runat="server">
<Model>
<ext:Model ID="ModelEmployees" runat="server" IDProperty="ID">
<Fields>
<ext:ModelField Name="ID" Type="Int" />
<ext:ModelField Name="Name" Type="String" />
</Fields>
</ext:Model>
</Model>
</ext:Store>
</Store>
<ColumnModel>
<Columns>
<ext:Column ID="ColumnName" Text="Name" runat="server" Flex="1" DataIndex="Name"/>
</Columns>
</ColumnModel>
<SelectionModel>
<ext:CheckboxSelectionModel runat="server" Mode="Multi" />
</SelectionModel>
</ext:GridPanel>
我想从网格面板中删除选定的行,并在单击按钮时从“员工”表中删除具有选定 ID 的员工。请问我怎么能用linq to sql(有/没有lambda)做到这一点?谢谢。
【问题讨论】:
【参考方案1】:将此标记添加到网格中:
<TopBar>
<ext:Toolbar runat="server">
<items>
<ext:Button runat="server" Handler="
var idArray = new Array();
var selection = #GridEmployees.getSelectionModel().getSelection();
for(i in selection)
idArray.push(selection[i].data.id);
App.direct.DeleteSelected(idArray);" />
</items>
</ext:Toolbar>
</TopBar>
并将此代码转换为隐藏代码:
dataContext ctx = new dataContext(); // your data context
[DirectMethod]
public void DeleteSelected(int[] idArray)
ctx.Employees.DeleteAllOnSubmit(
ctx.Employees.Where(x => idArray.Contains(x.id)).ToArray()
);
ctx.SubmitChanges();
或者这个代码:
[DirectMethod]
public void DeleteSelected(int[] idArray)
ctx.ExecuteCommand(
string.Format(
"DELETE FROM Employees WHERE ID IN (0)",
string.Join(",", idArray.Select(x => x.ToString()).ToArray())
)
);
【讨论】:
以上是关于如何从网格面板中删除选定的行以及数据库中的相应数据?的主要内容,如果未能解决你的问题,请参考以下文章