【问题标题】:Insert/Update/Delete is disabled for this control. message of EntityDataSource此控件禁用插入/更新/删除。 EntityDataSource 的消息
【发布时间】:2012-01-25 12:43:07
【问题描述】:

我使用ASPxGridViewEntityDataSource 作为它的数据源。在EntityDataSource 中,我编写了CommandText,因此无法将“EnableInsert”、“EnableUpdate”或“EnableDelete”设置为true。这就是我手动操作(插入、更新、删除)数据的原因。更改是手动传递到数据库的。但是在 GridView 的一侧给出了这些错误:

用于插入:"Insert is disabled for this control."
更新:"Update is disabled for this control."
删除:"Delete is disabled for this control."

我该如何解决这个问题?

(使用CommandText的原因是在GridView中显示的参数和连接超过1个表格。)

【问题讨论】:

  • 大约在那个时候 (2012) 带有 EF 的 ASPxGriView 是只读的。这要好得多,因为即使按列排序也不起作用。

标签: asp.net devexpress aspxgridview entitydatasource


【解决方案1】:

首先,您必须在表中具有主 ID,并且必须在表单中具有它以便插入它,或者理想情况下,您设置递增键,然后只输入值。 然后你必须为控制设置值属性。 然后它应该可以工作了。

【讨论】:

  • 我已经插入、更新、删除数据。但即使我可以操纵数据,也会出现错误。现在,我做了一个技巧:我编辑数据,最后我调用方法“e.Cancel=true;”这里的“e”是“DevExpress.Web.Data.ASPxDataUpdatingEventArgs e”。所以gridview认为“ok,结果编辑操作被取消了,所以我不需要报错”。也就是说,我没有用正式的方式解决问题,我用棘手的方式解决了。
【解决方案2】:

我解决了这个问题。 在 gridview 我有模板列(我在 CommandArgument 中传递了两个参数):

<asp:TemplateField>
                   <ItemTemplate>                           
                       <asp:ImageButton ToolTip="Delete" ID="button4" ButtonType="Image" ImageUrl="~/Projectimages/img_del.png" Text="" CommandName="Select" CommandArgument='<%#Eval("ID") + ";" +"Delete"%>' runat="server"/>
                   </ItemTemplate>
               </asp:TemplateField>

在后面的代码中,我正在拆分 CommandArgument 并将值保存在变量中,然后在 SelectedIndexChanged 事件中使用它们:

string selectCommand = "";
    int selectCommandID = -1;
    protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {                
            if (!e.CommandArgument.ToString().Contains(";"))
                selectCommand = "Select";
            else
            {
                selectCommandID = Convert.ToInt32(e.CommandArgument.ToString().Split(';')[0]);
                selectCommand = e.CommandArgument.ToString().Split(';')[1];
            }
        }            
    }

    protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (selectCommand == "Select")
        {
            //Select Code Here
        }
        else if (selectCommand == "Delete")
        {
            MyTestEntities context = new MyTestEntities();
            Table1 selectedRow = context.Table1.Single(a => a.ID == selectCommandID);
            context.Table1.DeleteObject(selectedRow);
            context.SaveChanges();

            EntityDataSource1.DataBind();
        }
    }

这行得通。您也可以使用它来更新 gridview 行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-10
    • 2016-01-06
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多