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

时间:2011-05-10 18:04:20

标签: insert telerik radgrid

我正在尝试使用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 = “24像素” >                                                                                                                                                                                                                                                                                                  <% -
                     - %GT;                                                                                                                                          
                                     'runat =“server”ID =“Addnew”Text =“Add New”CommandName =“InitInsert”/>                     'runat =“server”ID =“CancelAdd”Text =“Cancel”CommandName =“CancelAll”/>                     'runat =“server”ID =“InsertNew”Text =“执行插入”CommandName =“PerformInsert”/>
                    'runat =“server”ID =“EditAll”Text =“全部编辑”CommandName =“EditAll”/>                      0%>' runat =“server”ID =“CancelEdit”Text =“Cancel”CommandName =“CancelAll”/>                      0%>' runat =“server”ID =“UpdateAll”Text =“全部更新”CommandName =“UpdateAll”OnClientClick ='javascript:return confirm(“您确定要更新所有记录吗?”)'/>
                                     

     <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 个答案:

答案 0 :(得分:2)

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

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