DataItem is null

2 posts, 0 answers
  1. Tim
    Tim avatar
    60 posts
    Member since:
    May 2011

    Posted 26 Aug 2011 Link to this post

    DataItem is always null when I activate the SelectedIndexChanged event. What am I doing wrong? using version 2011.2.712.35

    <telerik:RadGrid ID="grdPolicyCoverage" runat="server" CellSpacing="0" GridLines="None">
        <headercontextmenu cssclass="GridContextMenu GridContextMenu_Default">
        <mastertableview tablelayout="Auto" width="95%">
            <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
                <HeaderStyle Width="20px"></HeaderStyle>
            <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
                <HeaderStyle Width="20px"></HeaderStyle>
                <EditColumn FilterControlAltText="Filter EditCommandColumn column">
        <clientsettings EnablePostBackOnRowClick="true" Selecting-AllowRowSelect="true">
        <filtermenu enableimagesprites="False">

    protected override void OnInit(EventArgs e)
        this.grdPolicyCoverage.SelectedIndexChanged += new EventHandler(grdPolicyCoverage_SelectedIndexChanged);
        this.grdPolicyCoverage.NeedDataSource += new GridNeedDataSourceEventHandler(grdPolicyCoverage_NeedDataSource);
    private void grdPolicyCoverage_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        PolicyCoverageList _policyCoverageList = GetPolicyCoverage();
        this.grdPolicyCoverage.DataSource = _policyCoverageList;
    private void grdPolicyCoverage_SelectedIndexChanged(object sender, System.EventArgs e)
        foreach (PolicyCoverage _selectedPolicy in this.grdPolicyCoverage.MasterTableView
                .Items.Cast<GridDataItem>().Where(item => item.Selected)
                .Select(item => item.DataItem as PolicyCoverage))
            Session[SelectedPolicy] = _selectedPolicy;

  2. Pavlina
    Pavlina avatar
    6070 posts

    Posted 01 Sep 2011 Link to this post

    Hi Tim,

    The DataItem object is not null only when used in data bound context (inside the ItemDataBound handler of the grid). The same is applicable for the MS GridView control.

    If you would like to get reference to the properties of the underlying field, consider extracting their values from the row cells as illustrated in this article:

    All the best,
    the Telerik team

    Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

  3. DevCraft R3 2016 release webinar banner
Back to Top