如何绑定radgrid中的dropdownlist

时间:2014-05-29 04:58:49

标签: telerik-grid

我是新手使用telerik控件。我想绑定telerik RadGrid控件中的dropdownlist中的数据。我已经应用但它出错了

  

对象引用未设置为对象的实例。

以下是我的代码:

源代码

<telerik:RadGrid runat="server" ID="RadGrid1" AutoGenerateColumns="false"              AllowPaging="true"
    OnNeedDataSource="RadGrid1_NeedDataSource" OnUpdateCommand="RadGrid1_UpdateCommand"
    OnItemCreated="RadGrid1_ItemCreated" OnDeleteCommand="RadGrid1_DeleteCommand"
    OnInsertCommand="RadGrid1_InsertCommand" 
    onitemdatabound="RadGrid1_ItemDataBound" >
    <MasterTableView DataKeyNames="id" CommandItemDisplay="Top" InsertItemPageIndexAction="ShowItemOnCurrentPage">
        <Columns>
            <telerik:GridEditCommandColumn ButtonType="ImageButton" />
            <telerik:GridBoundColumn DataField="id" HeaderText="ID" ReadOnly="true"
                ForceExtractValue="Always" ConvertEmptyStringToNull="true" />
                <telerik:GridTemplateColumn>
 <ItemTemplate>
         <asp:DropDownList ID="ddldept"  DataTextField="name" DataValueField="id"   runat="server"></asp:DropDownList>
  </ItemTemplate>
  </telerik:GridTemplateColumn>
            <telerik:GridBoundColumn DataField="code" HeaderText="Branch Code" />
            <telerik:GridBoundColumn DataField="name" HeaderText="Branch Name" />
            <telerik:GridBoundColumn DataField="shortname" HeaderText="Branch Shorth Name" />
            <telerik:GridBoundColumn DataField="hod" HeaderText="Head of Department"/>
            <telerik:GridBoundColumn DataField="location" HeaderText="Location Address" />
            <telerik:GridButtonColumn ConfirmText="Are you sure want to delete this Branch?" ConfirmDialogType="RadWindow"
                ConfirmTitle="Delete" ButtonType="ImageButton" CommandName="Delete" />
        </Columns>
        <EditFormSettings>
            <EditColumn ButtonType="ImageButton" />
        </EditFormSettings>
    </MasterTableView>
    <PagerStyle Mode="NextPrevAndNumeric" />
    <ClientSettings>
        <ClientEvents OnRowDblClick="rowDblClick" />
    </ClientSettings>
</telerik:RadGrid>

主要代码

  protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
   {
            DataTable dt=obj.display_branch();
            GridEditableItem item = e.Item as GridEditableItem;
            DropDownList list = item.FindControl("ddldept") as DropDownList;
            list.DataSource = dt;
            list.DataTextField = "name";
            list.DataValueField = "id";
            list.DataBind();
}

1 个答案:

答案 0 :(得分:1)

尝试使用RadGrid1_ItemCreated。这就是我实现它的方式。