1

我有一个 ID、FirstName 和 LastName 的表。我想在 Telerik radgrid 中执行 CRUD(删除、插入、更新)操作。但我收到此错误:“索引超出范围。必须为非负数且小于集合的大小。参数名称:索引“请帮帮我。

    <body>
    <form id="form1" runat="server">
       <div dir=rtl>

           <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True" 
               AllowSorting="True" AutoGenerateColumns="False" CellSpacing="0" 
               DataSourceID="OpenAccessDataSource1" GridLines="None" 
               AllowAutomaticDeletes="True" AllowAutomaticInserts="True" 
               AllowAutomaticUpdates="True" ondeletecommand="RadGrid1_DeleteCommand" 
               onitemcommand="RadGrid1_ItemCommand" onitemcreated="RadGrid1_ItemCreated" 
               onneeddatasource="RadGrid1_NeedDataSource" 
               onupdatecommand="RadGrid1_UpdateCommand">
<ClientSettings>
<Selecting CellSelectionMode="None" AllowRowSelect="True"></Selecting>
</ClientSettings>

<MasterTableView DataSourceID="OpenAccessDataSource1">
<CommandItemSettings ExportToPdfText="Export to PDF"></CommandItemSettings>

<RowIndicatorColumn Visible="True" FilterControlAltText="Filter RowIndicator column">
<HeaderStyle Width="20px"></HeaderStyle>
</RowIndicatorColumn>

<ExpandCollapseColumn Visible="True" FilterControlAltText="Filter ExpandColumn column">
<HeaderStyle Width="20px"></HeaderStyle>
</ExpandCollapseColumn>

    <Columns>
        <telerik:GridBoundColumn DataField="ID" DataType="System.Int32" 
            FilterControlAltText="Filter ID column" HeaderText="ID" SortExpression="ID" 
            UniqueName="ID" Visible="False">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Fname" 
            FilterControlAltText="Filter Fname column" HeaderText="Fname" 
            SortExpression="Fname" UniqueName="Fname">
        </telerik:GridBoundColumn>
        <telerik:GridBoundColumn DataField="Lname" 
            FilterControlAltText="Filter Lname column" HeaderText="Lname" 
            SortExpression="Lname" UniqueName="Lname">
        </telerik:GridBoundColumn>
        <telerik:GridButtonColumn CommandName="Delete" 
            FilterControlAltText="Filter DeleteColumn column" Text="Delete" 
            UniqueName="DeleteColumn">
        </telerik:GridButtonColumn>
    </Columns>

<EditFormSettings>
<EditColumn FilterControlAltText="Filter EditCommandColumn column"></EditColumn>
</EditFormSettings>
</MasterTableView>

<FilterMenu EnableImageSprites="False"></FilterMenu>
           </telerik:RadGrid>



           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
           <telerik:OpenAccessDataSource ID="OpenAccessDataSource1" runat="server" 
               ObjectContextProvider="rgCRUD.dbm, rgCRUD" TypeName="rgCRUD.Person" 
               onselecting="OpenAccessDataSource1_Selecting">
           </telerik:OpenAccessDataSource>



           <br />
           <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
           </telerik:RadAjaxManager>
           <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
           </telerik:RadScriptManager>



       </div>
    </form>
</body>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.OpenAccess;
using Telerik.Web.UI;
using Telerik.Web.UI.Grid;
using System.Web.UI.WebControls;


namespace rgCRUD
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        dbm db=new dbm();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void RadButton1_Click(object sender, EventArgs e)
        {

        }

        protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
        {


            GridEditableItem item = e.Item as GridEditableItem;
            var id = item.GetDataKeyValue("ID").ToString();

            var query=(from p in db.People where p.ID==int.Parse(id) select p).First();
            db.Delete(query);
            db.SaveChanges();

        }



        protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridEditableItem && e.Item.IsInEditMode)
            {
                if (!e.Item.OwnerTableView.IsItemInserted)
                {
                    GridEditableItem item = e.Item as GridEditableItem;
                    GridEditManager manager = item.EditManager;
                    GridTextBoxColumnEditor editor = manager.GetColumnEditor("ID") as GridTextBoxColumnEditor;
                    editor.TextBoxControl.Enabled = false;
                }
            }
        }
4

1 回答 1

0

您没有访问所需的正确字段,您需要将 ID 分配给 DataKeyNames<MasterTableView DataSourceID="OpenAccessDataSource1" DataKeyNames="ID">或访问后面代码中的正确控件:

protected void RadGrid1_DeleteCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
{


    GridEditableItem item = e.Item as GridEditableItem;
    var id = item.OwnerTableView.Items[0].Item["ID"].Text;

    var query=(from p in db.People where p.ID==int.Parse(id) select p).First();
    db.Delete(query);
    db.SaveChanges();

}
于 2018-03-01T18:39:47.303 回答