This is a migrated thread and some comments may be shown as answers.

DataItem is null

1 Answer 59 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Tim
Top achievements
Rank 2
Tim asked on 26 Aug 2011, 05:19 PM
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">
    </headercontextmenu>
    <mastertableview tablelayout="Auto" width="95%">
        <RowIndicatorColumn FilterControlAltText="Filter RowIndicator column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn FilterControlAltText="Filter ExpandColumn column">
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
        <EditFormSettings>
            <EditColumn FilterControlAltText="Filter EditCommandColumn column">
            </EditColumn>
        </EditFormSettings>
    </mastertableview>
    <clientsettings EnablePostBackOnRowClick="true" Selecting-AllowRowSelect="true">
     </clientsettings>
    <filtermenu enableimagesprites="False">
    </filtermenu>
</telerik:RadGrid>

protected override void OnInit(EventArgs e)
{
    base.OnInit(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;
    }
}



























1 Answer, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 01 Sep 2011, 12:02 PM
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:
http://www.telerik.com/help/aspnet-ajax/grid-accessing-cells-and-rows.html

All the best,
Pavlina
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 >>

Tags
Grid
Asked by
Tim
Top achievements
Rank 2
Answers by
Pavlina
Telerik team
Share this question
or