RadGrid ItemDataBound Event Firing Twice

2 posts, 0 answers
  1. Sam
    Sam avatar
    6 posts
    Member since:
    Sep 2010

    Posted 08 Feb 2016 Link to this post

    Hello Support,

    I tried searching forums regarding the itemdatabound event being fired twice for a radgrid. But no solutions proved conclusive. So I have created an example project  with a single page.

    1 RadScriptManager
    1 SQLDataSource
    1 RadGrid (Telerik v 2015.2.826.45)

    SQLDatasource connecting to a table with 5 rows

    id desc percent
    1   A   10
    2   B   20
    3   C   30
    4   D   40
    5   E   50

    ItemDataBound was seen to fire 12 times twice for each row. (+1 for header and +1 for footer)  

    Please explain how can I restrict this to fire once for each row.

    Code for .vb , .aspx are included. The example project is also attached along with screenshots for runtime and database.


    Imports Telerik.Web.UI
    Public Class _default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Session("ctr") = 0
        End Sub
        Private Sub RadGrid1_ItemDataBound(sender As Object, e As GridItemEventArgs) Handles RadGrid1.ItemDataBound
            Session("ctr") += 1
            Debug.WriteLine("ItemDataBound: " & Session("ctr"))
        End Sub
    End Class



    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="default.aspx.vb" Inherits="RadGrid_ItemDatabound._default" %>
    <%@ Register assembly="Telerik.Web.UI" namespace="Telerik.Web.UI" tagprefix="telerik" %>
    <!DOCTYPE html>
    <head runat="server">
        <form id="form1" runat="server">
            <telerik:RadScriptManager ID="RadScriptManager1" Runat="server">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySQLConnectionString %>" ProviderName="<%$ ConnectionStrings:MySQLConnectionString.ProviderName %>" SelectCommand="Select * from temp"></asp:SqlDataSource>
            <telerik:RadGrid ID="RadGrid1" runat="server" CellSpacing="-1" DataSourceID="SqlDataSource1" GridLines="Both" GroupPanelPosition="Top">
                <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
                        <telerik:GridBoundColumn DataField="id" DataType="System.Int32" FilterControlAltText="Filter id column" HeaderText="id" SortExpression="id" UniqueName="id">
                        <telerik:GridBoundColumn DataField="desc" FilterControlAltText="Filter desc column" HeaderText="desc" SortExpression="desc" UniqueName="desc">
                        <telerik:GridBoundColumn DataField="percent" DataType="System.Int32" FilterControlAltText="Filter percent column" HeaderText="percent" SortExpression="percent" UniqueName="percent">


    Debug Output:

    ItemDataBound: 1
    ItemDataBound: 2
    ItemDataBound: 3
    ItemDataBound: 4
    ItemDataBound: 5
    ItemDataBound: 6
    ItemDataBound: 7
    ItemDataBound: 8
    ItemDataBound: 9
    ItemDataBound: 10
    ItemDataBound: 11
    ItemDataBound: 12



  2. Viktor Tachev
    Viktor Tachev avatar
    1768 posts

    Posted 10 Feb 2016 Link to this post

    Hi Sam,

    I have replied to your query in the support ticket you have submitted. I suggest we continue the conversation there.

    On a side note, please avoid submitting duplicate thread. This way we can keep better track or your support history and provide answers faster.

    I will paste my reply from the ticket in case someone has similar question.

    Note that the ItemDataBound event is fired for every item in the grid after it is data bound. Also, there is a GridEditFormItem generated automatically for every GridDataItem.

    This is why you are seeing ItemDataBound fire twice for each row. It is triggered for the GridDataItem and then for the GridEditFormItem.

    In order to distinguish between the items you can use a condition in the handler. Thus, you can execute custom logic only once depending on the type of the item.

    Copy Code
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        if (e.Item is GridDataItem)
            // data item
        if (e.Item is GridEditFormItem)
            // edit form item

    Viktor Tachev
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top