1

ID、FirstName、および LastName を持つテーブルがあります。telek radgrid で CRUD(削除、挿入、更新) 操作を実行したいのですが、次のエラーが表示されます: "インデックスが範囲外でした。負でなく、コレクションのサイズ. パラメータ名: index " 助けてください.

    <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 に答える