Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 删除gridview中的行获取错误_C#_Asp.net - Fatal编程技术网

C# 删除gridview中的行获取错误

C# 删除gridview中的行获取错误,c#,asp.net,C#,Asp.net,我想删除网格视图中的行,这是我从row命令中得到的后端代码。 我已经创建了一个OnRowDeleting,但函数中没有任何内容 protected void grid2_RowCommand(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) // check the incoming command name { case "remove":

我想删除网格视图中的行,这是我从row命令中得到的后端代码。 我已经创建了一个OnRowDeleting,但函数中没有任何内容

protected void grid2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName) // check the incoming command name
            {
case "remove":
                    if (grid2.Rows.Count > 1)
                    {
                        int rowIndex = Convert.ToInt32(e.CommandArgument);
                        //GridViewRow row = (GridViewRow)grid2.Rows[rowIndex];
                        grid2.DeleteRow(rowIndex); //error pointing to this
                        grid2.DataBind();
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "Scripts", "
<script>alert('Last row');</script>");
                        }
                        break;
    }
我的前端代码

 <ItemTemplate>
 <asp:Button ID="BtnDelete" runat="server" Text="Delete" 
 CssClass="btn btn-primary" CommandName="remove" CommandArgument='<%# Container.DataItemIndex %>'/>
 </ItemTemplate>
单击“删除”按钮时出现此错误

中发生“System.NotSupportedException”类型的异常 System.Web.dll,但未在用户代码中处理

其他信息:数据源不支持删除 “SqlDataSource2”,除非指定了DeleteCommand

数据源

我的网格


您必须将CommandName设置为Delete而不是remove 例如:

<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>

请用数据源更新前端代码。如果您使用了,谷歌会在第一次搜索结果中为您提供解决方案。@Chandru hi,感应电动机updated@Jan你还没有更新网格…@Chandru hi im刚更新你的函数也发生了变化,什么都没有发生你正在从gridview中删除该行,但是你要再次调用databind,这只是将gridview刷新到与原始数据源相同的状态。要么从数据源中删除它,然后进行数据绑定,要么在不使用redatabinding的情况下从gridview中删除它。谢谢你的建议。我可以举个例子吗?我真的不明白你的意思为什么不使用Gridview1\u行删除事件?
 <grd:MultiSelectGridView ID="grid2" runat="server" Width="500px" 
                        CssClass="paging_gridview" AllowPaging="True" 
                        AutoGenerateColumns ="false" PageSize="10" PagerType="Custom"  
                        ShowFooter="true" OnRowDeleting="grid2_RowDeleting" 
                        MultiSelectDataKeyName="Urid,Name" ShowHeaderWhenEmpty="true"
                        MultiSelectColumnIndex="0" EnableMultiSelect="false" GridLines="None" BorderStyle="None"  OnRowCommand="grid2_RowCommand"
                        >
<asp:ButtonColumn CommandName="Delete" Text="Delete"></asp:ButtonColumn>