未从数据库asp.net中删除项目

本文关键字:删除项目 net asp 数据库 | 更新日期: 2023-09-27 18:21:20

我正在使用实体框架来填充网格,但我试图删除该项,但没有出现任何错误,只是没有采取任何行动。

我的网格如下,老实说,问题是什么,因为我使用的是类型播放器,所以它应该删除。

我对此没有任何疑问,有人能帮帮我吗。

    <telerik:RadGrid ID="grdSoccerPlayers" runat="server" AutoGenerateColumns="False"    GroupPanelPosition="Top" AutoGenerateDeleteColumn="True" AutoGenerateEditColumn="True" OnItemCommand="grdSoccerPlayers_ItemCommand" CellSpacing="-1" GridLines="Both" AllowAutomaticDeletes="True">
        <MasterTableView    CommandItemDisplay="Top">
            <Columns>
                <telerik:GridBoundColumn DataField="id" DataType="System.Guid" FilterControlAltText="Filter id column" HeaderText="id" ReadOnly="True" SortExpression="id" UniqueName="id">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Name" FilterControlAltText="Filter Name column" HeaderText="Name" ReadOnly="True" SortExpression="Name" UniqueName="Name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="player_id" FilterControlAltText="Filter player_id column" HeaderText="player_id" ReadOnly="True" SortExpression="player_id" UniqueName="player_id">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="gender" FilterControlAltText="Filter gender column" HeaderText="gender" ReadOnly="True" SortExpression="gender" UniqueName="gender">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="town" FilterControlAltText="Filter town column" HeaderText="town" ReadOnly="True" SortExpression="town" UniqueName="town">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="telephone" FilterControlAltText="Filter telephone column" HeaderText="telephone" ReadOnly="True" SortExpression="telephone" UniqueName="telephone">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="email" FilterControlAltText="Filter email column" HeaderText="email" ReadOnly="True" SortExpression="email" UniqueName="email">
                </telerik:GridBoundColumn>
                <telerik:GridCheckBoxColumn DataField="active" DataType="System.Boolean" FilterControlAltText="Filter active column" HeaderText="active" ReadOnly="True" SortExpression="active" UniqueName="active">
                </telerik:GridCheckBoxColumn>
                <telerik:GridBoundColumn DataField="createdDate" DataType="System.DateTime" FilterControlAltText="Filter createdDate column" HeaderText="createdDate" ReadOnly="True" SortExpression="createdDate" UniqueName="createdDate">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="author" FilterControlAltText="Filter author column" HeaderText="author" ReadOnly="True" SortExpression="author" UniqueName="author">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="teamId" DataType="System.Guid" FilterControlAltText="Filter teamId column" HeaderText="teamId" ReadOnly="True" SortExpression="teamId" UniqueName="teamId">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="address" FilterControlAltText="Filter address column" HeaderText="address" ReadOnly="True" SortExpression="address" UniqueName="address">
                </telerik:GridBoundColumn>
                <telerik:GridButtonColumn CommandName="Delete" Text="Delete" UniqueName="Delete"
     ButtonType="ImageButton" ImageUrl="~/Icons/delete.png" AutoPostBackOnFilter="true" HeaderText="Actions"
     ConfirmTitle="Delete" ConfirmDialogType="Classic"   
     ConfirmText="Are you sure want to delete the selected player?" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid> 
        if (e.CommandName == "Delete")
            {
                GridDataItem item = e.Item as GridDataItem;
                Guid strId = new Guid(item.GetDataKeyValue("id").ToString());
                player _player = _dal.GetPlayerBYID(strId);
                _dal.SoccerEntities.AddToplayers(_player);
                _dal.SoccerEntities.DeleteObject(_player);
                _dal.SoccerEntities.SaveChanges();
                grdSoccerPlayers.DataBind();
                grdSoccerPlayers.Rebind();
            }

我的数据源是一个linq查询,如下

     public player GetPlayerBYID(Guid _playerId)
        {
            try
            {
                if (_playerId == Guid.Empty)
                {
                    player _player = new player();
                    _player.player_id = "16-56-232";
                    return _player;
                }
                else
                {
                    var q = SoccerEntities.players.Where(p => p.id == _playerId);
                    if (q == null)
                        throw new EntityContextException(string.Format("A player could not be found {0}!", _playerId));
                    else
                        return q.ToList()[0];
                }
            }
            catch (Exception ex)
            {
                throw new EntityContextException("GetPlayerBYID failed.", ex);
            }
        }

未从数据库asp.net中删除项目

我猜您缺少一个OnDeleteCommand。查看此控件的文档。http://docs.telerik.com/devtools/aspnet-ajax/controls/grid/data-editing/delete-records/overview