首页 文章

无法使用OnInsertCommand e.item插入Telerik RadGrid就地插入

提问于
浏览
1

我正在尝试使用teleriks rad grid执行插入 . 我正在进行就地插入并使用onInsertCommand方法设置要插入的值 . 我在telerik的documnetation中发现了这个陈述:

GridEditableItem editedItem = e.Item as GridEditableItem;当我使用它editItem得到空值,我不知道如何使它工作:

这是我在InsertCommand后面的代码

protected void RadGrid1_InsertCommand(Object Sender,Telerik.Web.UI.GridCommandEventArgs e){

GridEditableItem editedItem = e.Item as GridEditableItem;

        Hashtable newValues = new Hashtable();

        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);
        editedItem.Edit = false;

        Yieldco.RS.Libraries.BusinessObjects.UnitType u1 = new Yieldco.RS.Libraries.BusinessObjects.UnitType();

        u1.Description = newValues["Description"].ToString();

        u1.UnitTypeID = Convert.ToInt32(e.Item.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["UnitTypeID"]);
        u1.CommunityID = 1;
        u1.CompanyID = 1;
        u1.Bathrooms = (float)Convert.ToDouble(newValues["Bathrooms"]);
        u1.Bedrooms = Convert.ToInt32(newValues["Bedrooms"]);
        u1.SqFtHigh = (float)Convert.ToDouble(newValues["SqFtHigh"]);
        u1.SqFtLow = (float)Convert.ToDouble(newValues["SqFtLow"]);
        u1.NumOfUnits = Convert.ToInt32(newValues["NumOfUnits"]);
        u1.ProCon = Convert.ToInt32(newValues["ProCon"]);
        u1.OthCon = Convert.ToInt32(newValues["OthCon"]);
        u1.RentHigh = (float)Convert.ToDouble(newValues["RentHigh"]);
        u1.RentLow = (float)Convert.ToDouble(newValues["RentLow"]);
        u1.Status = 1;

        int id = MSController.SaveUnitTypes(u1);
}

和我的aspx radgrid

AutoGenerateColumns = "false" AllowAutomaticUpdates = "false" AllowAutomaticInserts = "false" DataKeyNames = "UnitTypeID" GridLines = "Both"
EditItemStyle-Width = "24px"> <% -

  • %>
    ' runat= 2730978 ID= 2730979 Text= 2730980 CommandName= 2730981 /> ' runat = "server" ID = "CancelAdd" Text = "Cancel" CommandName = "CancelAll" />'runat = "server" ID = "InsertNew" Text = "Perform Insert" CommandName = "PerformInsert" />
    ' runat= 2730991 ID= 2730992 Text= 2730993 CommandName= 2730994 /> 0 %>' runat = "server" ID = "CancelEdit" Text = "Cancel" CommandName = "CancelAll" /> 0%> ' runat= 2730999 ID= 2731000 Text= 2731001 CommandName= 2731002 OnClientClick=' javascript:return confirm("Are you sure you want to Update All Records?")'/>
<asp:ObjectDataSource ID="odsGetUnitTypes" runat="server"
    TypeName="Yieldco.RS.Libraries.Controllers.MSController" 
    DataObjectTypeName="Yieldco.RS.Libraries.BusinessObjects.UnitType" 
    SelectMethod="GetUnitTypesByCommunityID"
    InsertMethod="SaveUnitTypes"
    UpdateMethod="SaveUnitTypes"
    >
    <SelectParameters>          
        <asp:Parameter DefaultValue="1" Name="CommunityID" />            
    </SelectParameters>
    </asp:ObjectDataSource>

请帮我成功插入 .

另外在Telerik文档中我看到他们使用 e.ite.item 索引获取vdatakey值但是如果我使用它,索引总是显示为-1所以我使用 editedItem.ItemIndex 它工作正常

提前致谢

1 回答

  • 2

    要引用插入项,您应该使用以下语法(而不是编辑项):

    if(e.CommandName = RadGrid.PerformInsertCommandName)
    {
      GridEditableItem editItem = e.Item.OwnerTableView.GetInsertItem();
    }
    

相关问题