如何从gridpanel中删除选定的行,以及从数据库中删除相应的数据?

时间:2014-01-23 08:05:07

标签: c# linq-to-sql ext.net gridpanel

我的代码是:

<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 个答案:

答案 0 :(得分:0)

将此标记添加到网格中:

    <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())
        )
    );
}