Insert/Update/Delete is disabled for this control.

2019-07-19 18:26发布

问题:

I use ASPxGridView and EntityDataSource as its datasource. In EntityDataSource, I write CommandText, so I can not set "EnableInsert", "EnableUpdate", or "EnableDelete" to true. That's why, I manipulate (insert, update, delete) data manually. Changes are made manually pass through to database. But at the side of the GridView these errors are given:

For inserting: "Insert is disabled for this control."
For updating: "Update is disabled for this control."
For deleting: "Delete is disabled for this control."

How can I solve this problem?

(The reason of using CommandText is where parameters and joining of more than 1 tables for showing in GridView.)

回答1:

First you must have primary ID in table, and you must have it in form so you can insert it, or ideally, you set incrementing key, and just input values. Then you have to set value property for control. And then it should work.



回答2:

I made workaround to this problem. in gridview I have template column (I'm passing two arguments in 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>

in code behind I'm splitting CommandArgument and saving values in variables and then using them in SelectedIndexChanged event:

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

this works. You can use this for updating gridview row too.