Items on RadGrid become to zero inside ItemDataBound only

2 posts, 0 answers
  1. Poaro
    Poaro avatar
    1 posts
    Member since:
    Jun 2014

    Posted 18 Jul 2014 Link to this post

    Hi, I try to add new row to radGrid by this code (Add button)

    PartDt.Rows.InsertAt(dr, 0);
    dgdPart.CurrentPageIndex = 0;
    dgdPart.DataSource = PartDt;
    dgdPart.DataBind();
    dgdPart.MasterTableView.Items[0].Edit = true;
    dgdPart.Rebind();



       

    After             dgdPart.Rebind();   code step into 

    protected void dgdPart_ItemDataBound(object sender, GridItemEventArgs e)
    {
               if (e.Item.IsInEditMode && e.Item is GridEditFormItem)
                {
                    if (e.Item.ItemIndex > -1)
                    {
                             RadComboBox cmbPart = (RadComboBox)dgdPart.Items[e.Item.ItemIndex].FindControl("radcmbPart");    //Error line
                              ....
    }



    I try to debug and see while e.Item.ItemIndex is 0 and the dgdPart.Items.Count is 0 at line "//Error line" so I got error "Index was out of range. Must be non-negative and less than the size of the collection."

    I also found the dgdPart.Items.Count is 0 only in dgdPart_ItemDataBound, If I skip the erorr line above and continue until finish dgdPart_ItemDataBound, It comeback to dgdPart.Items.Count = 1

    Could you suggest me to solve this problem?  Thanks!!



    Below is my aspx code for this radGrid

    <telerik:RadGrid ID="dgdPart" runat="server" AllowPaging="True" CellSpacing="0" GridLines="None" AutoGenerateColumns="False" OnEditCommand="dgdPart_EditCommand" OnNeedDataSource="dgdPart_NeedDataSource" OnCancelCommand="dgdPart_CancelCommand" OnItemDataBound="dgdPart_ItemDataBound" OnUpdateCommand="dgdPart_UpdateCommand">
                                                               <ClientSettings>
                                                                   <Selecting CellSelectionMode="None">
                                                                   </Selecting>
                                                               </ClientSettings>
                                                               <MasterTableView>
                                                                   <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:GridTemplateColumn FilterControlAltText="Filter CheckBoxTemplateColumn column" UniqueName="CheckBoxTemplateColumn">
                                                                                           <ItemTemplate>
                                                                                               <asp:CheckBox ID="chkToDelete" runat="server"/>
                                                                                           </ItemTemplate>
                                                                                           <ItemStyle HorizontalAlign="Center" VerticalAlign="Top" Width="20px" />
                                                                        </telerik:GridTemplateColumn>  
                                                                       <telerik:GridBoundColumn DataField="t_name" FilterControlAltText="Filter column column" HeaderText="อะไหล่" UniqueName="column">
                                                                       </telerik:GridBoundColumn>
                                                                       <telerik:GridBoundColumn DataField="life_time" FilterControlAltText="Filter column1 column" HeaderText="อายุการใช้งาน" UniqueName="column1">
                                                                       </telerik:GridBoundColumn>
                                                                       <telerik:GridBoundColumn DataField="start_date" FilterControlAltText="Filter column2 column" HeaderText="วันเริ่มต้นใช้งาน" UniqueName="column2">
                                                                       </telerik:GridBoundColumn>
                                                                       <telerik:GridBoundColumn DataField="expire_date" FilterControlAltText="Filter column3 column" HeaderText="วันสิ้นสุดใช้งาน" UniqueName="column3">
                                                                       </telerik:GridBoundColumn>
                                                                       <telerik:GridEditCommandColumn FilterControlAltText="Filter EditCommandColumn column">
                                                                       </telerik:GridEditCommandColumn>
                                                                   </Columns>
                                                                   <EditFormSettings EditFormType="Template">
                                                                                     <EditColumn uniquename="edit_data_form"/>
                                                                                       <FormTemplate>
                                                                                          
                                                                                           <table>
                                                                                               <tr>
                                                                                                   <td style="width:100px;height:20px">
                                                                                                       อะไหล่ :
                                                                                                   </td>
                                                                                                   <td>
                                                                                                       <telerik:RadComboBox ID="radcmbPart" runat="server"   Width="300px"></telerik:RadComboBox>
                                                                                                   </td>
                                                                                               </tr>
                                                                                               <tr>
                                                                                                   <td style="width:100px;height:20px">
                                                                                                       อายุการใช้งาน :
                                                                                                   </td>
                                                                                                   <td>
                                                                                                       <telerik:RadNumericTextBox ID="radtxtLiftTime" runat="server" Text='<%# Bind("life_time") %>' Width="100px"></telerik:RadNumericTextBox>
                                                                                                   </td>
                                                                                               </tr>
                                                                                                  <tr>
                                                                                                   <td style="width:100px;height:20px">
                                                                                                       วันเริ่มต้นใช้งาน :
                                                                                                   </td>
                                                                                                   <td>
                                                                                                       <asp:TextBox ID="txtPartStartDate" Text='<%# Bind("start_date") %>' runat="server" ></asp:TextBox>
                                                                                                       <asp:CalendarExtender ID="calendarPartStartDate" Format="dd/MM/yyyy" runat="server" TargetControlID="txtPartStartDate"></asp:CalendarExtender>
                                                                                                       
                                                                                                   </td>
                                                                                               </tr>
                                                                                                </tr>
                                                                                                  <tr>
                                                                                                   <td style="width:100px;height:20px">
                                                                                                       วันสิ้นสุดใช้งาน :
                                                                                                   </td>
                                                                                                   <td>
                                                                                                       <asp:TextBox ID="txtPartExpireDate" Text='<%# Bind("expire_date") %>' runat="server" ></asp:TextBox>
                                                                                                       <asp:CalendarExtender ID="calendarPartExpireDate" Format="dd/MM/yyyy" runat="server" TargetControlID="txtPartExpireDate"></asp:CalendarExtender>
                                                                                                   </td>
                                                                                               </tr>
     
                                                                                               <tr>
                                                                                                   <td>                                                                                       
                                                                                                   </td>
                                                                                                   <td>
                                                                                                       <asp:Button ID="btnUpdate" CommandName="Update" Text="Update" runat="server" />  <asp:Button ID="btnCancel" CommandName="Cancel" Text="Cancel" runat="server" />                                           
                                                                                                   </td>
                                                                                               </tr>
                                                                                           </table>                                                               
                                                                                       </FormTemplate>
                                                                                       <PopUpSettings ScrollBars="None" />                           
                                                                               </EditFormSettings>
                                                               </MasterTableView>
                                                               <FilterMenu EnableImageSprites="False">
                                                               </FilterMenu>
                                                           </telerik:RadGrid>

  2. Viktor Tachev
    Admin
    Viktor Tachev avatar
    1488 posts

    Posted 22 Jul 2014 Link to this post

    Hi Poaro,

    I have inspected the provided code and noticed that the DataBind() method is used for RadGrid. Please note that this method is used for simple data binding only. Also NeedDataSource event is handled to populate the data for the RadGrid. Please note that DataBind() must never be used when advanced data binding is implemented.

    In order to have the grid behave as expected try using only advanced data binding (handling NeedDataSource) to populate data for the RadGrid. Also remove all calls to the DataBind() method.

    Check out the following resources that illustrate how advanced data binding could be implemented for the RadGrid:


    Regards,
    Viktor Tachev
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top