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

Radgrid Detail Table dataItem.GetDataKeyValue Object reference not set to an Object

5 Answers 643 Views
Grid
This is a migrated thread and some comments may be shown as answers.
CG
Top achievements
Rank 1
CG asked on 13 Oct 2011, 07:51 PM
Hello,

I have an issue with a Heirarchy - Grid/Programmatic Binding.

I tried following this example as best as i can: http://demos.telerik.com/aspnet-ajax/grid/examples/programming/detailtabledatabind/defaultcs.aspx

I have a main table and three nested grids:
Main Table 
    -  Table 1
- Table 2
- Table 3

I can expand just find from Main -> Table 1. But when i try to expand a row from table 1 to get to table 2 i keep getting an error "Object reference not set to an instance of an object". I debugged through my code and saw that it breaks down on the line that contains:

portfolioTypeCode = dataItem.GetDataKeyValue("PortfolioTypeCode").ToString();


It's odd that it doesn't work because I can see the PortfoliotypeCode Column in table 1 with all the correct data, and I named everything correctly on the front end. Here is my grid front end (the table not working is named 'PortfolioTypeCode':

<telerik:RadGrid ID="PNLViewGrid" runat="server" Width="100%" AutoGenerateColumns="false"
    AllowSorting="true" AllowPaging="false" OnNeedDataSource="PNLView_NeedDataSource"
    OnDetailTableDataBind="PNLView_DetailTableDataBind">
    <MasterTableView Width="100%" DataKeyNames="PortfolioCode,PortfolioTypeCode,Strategy,StrategyId,SymbolCode,AsOfDate"
        AllowMultiColumnSorting="true">
        <DetailTables>
            <telerik:GridTableView DataKeyNames="PortfolioCode,AsOfDate" Name="PortfolioCode" Width="100%">
                <DetailTables>
                    <telerik:GridTableView DataKeyNames="PortfolioCode,PortfolioTypeCode,AsOfDate" Name="PortfolioTypeCode" Width="100%">
                        <DetailTables>
                            <telerik:GridTableView DataKeyNames="PortfolioCode,PortfolioTypeCode,StrategyId,AsOfDate" Name="Strategy" Width="100%">
                                <Columns>
                                    <telerik:GridBoundColumn SortExpression="SymbolCode" HeaderText="Symbol Code" HeaderButtonType="TextButton" DataField="SymbolCode" />
                                    <telerik:GridBoundColumn SortExpression="DailyPNL" HeaderText="Total Daily PNL" HeaderButtonType="TextButton" DataField="DailyPNL" />
                                    <telerik:GridBoundColumn SortExpression="MTDPNL" HeaderText="Total Montly PNL" HeaderButtonType="TextButton" DataField="MTDPNL" />
                                    <telerik:GridBoundColumn SortExpression="YTDPNL" HeaderText="Total Yearly PNL" HeaderButtonType="TextButton" DataField="YTDPNL" />
                                    <telerik:GridBoundColumn SortExpression="BaseMktValue" HeaderText="Total Base Market Value" HeaderButtonType="TextButton" DataField="BaseMktValue" />
                                    <telerik:GridDateTimeColumn SortExpression="AsOfDate" HeaderText="As Of Date" HeaderButtonType="TextButton" DataField="AsOfDate" Visible="false" />
                                </Columns>
                            </telerik:GridTableView>
                        </DetailTables>
                        <Columns>
                            <telerik:GridNumericColumn SortExpression="StrategyId" HeaderText="StrategyId" HeaderButtonType="TextButton" DataField="StrategyId" Visible="false" />
                            <telerik:GridBoundColumn SortExpression="Strategy" HeaderText="Strategy" HeaderButtonType="TextButton" DataField="Strategy" />
                            <telerik:GridBoundColumn SortExpression="DailyPNL" HeaderText="Total Daily PNL" HeaderButtonType="TextButton" DataField="DailyPNL" />
                            <telerik:GridBoundColumn SortExpression="MTDPNL" HeaderText="Total Montly PNL" HeaderButtonType="TextButton" DataField="MTDPNL" />
                            <telerik:GridBoundColumn SortExpression="YTDPNL" HeaderText="Total Yearly PNL" HeaderButtonType="TextButton" DataField="YTDPNL" />
                            <telerik:GridBoundColumn SortExpression="BaseMktValue" HeaderText="Total Base Market Value" HeaderButtonType="TextButton" DataField="BaseMktValue" />
                            <telerik:GridDateTimeColumn SortExpression="AsOfDate" HeaderText="As Of Date" HeaderButtonType="TextButton" DataField="AsOfDate" Visible="false" />
                        </Columns>
                    </telerik:GridTableView>
                </DetailTables>
                <Columns>
                    <telerik:GridBoundColumn SortExpression="PortfolioTypeCode" HeaderText="Portfolio Type Code" HeaderButtonType="TextButton" DataField="PortfolioTypeCode" />
                    <telerik:GridBoundColumn SortExpression="DailyPNL" HeaderText="Total Daily PNL" HeaderButtonType="TextButton" DataField="DailyPNL" />
                    <telerik:GridBoundColumn SortExpression="MTDPNL" HeaderText="Total Montly PNL" HeaderButtonType="TextButton" DataField="MTDPNL" />
                    <telerik:GridBoundColumn SortExpression="YTDPNL" HeaderText="Total Yearly PNL" HeaderButtonType="TextButton" DataField="YTDPNL" />
                    <telerik:GridBoundColumn SortExpression="BaseMktValue" HeaderText="Total Base Market Value" HeaderButtonType="TextButton" DataField="BaseMktValue" />
                    <telerik:GridDateTimeColumn SortExpression="AsOfDate" HeaderText="As Of Date" HeaderButtonType="TextButton" DataField="AsOfDate" Visible="false" />
                </Columns>
            </telerik:GridTableView>
        </DetailTables>   
        <Columns>
            <telerik:GridBoundColumn SortExpression="PortfolioCode" HeaderText="Portfolio Code" HeaderButtonType="TextButton" DataField="PortfolioCode" />
            <telerik:GridBoundColumn SortExpression="DailyPNL" HeaderText="Total Daily PNL" HeaderButtonType="TextButton" DataField="DailyPNL" />
            <telerik:GridBoundColumn SortExpression="MTDPNL" HeaderText="Total Montly PNL" HeaderButtonType="TextButton" DataField="MTDPNL" />
            <telerik:GridBoundColumn SortExpression="YTDPNL" HeaderText="Total Yearly PNL" HeaderButtonType="TextButton" DataField="YTDPNL" />
            <telerik:GridBoundColumn SortExpression="BaseMktValue" HeaderText="Total Base Market Value" HeaderButtonType="TextButton" DataField="BaseMktValue" />
            <telerik:GridDateTimeColumn SortExpression="AsOfDate" HeaderText="As Of Date" HeaderButtonType="TextButton" DataField="AsOfDate" Visible="false" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>


Here is my codebehind:

protected void PNLView_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
       {
           GridDataItem dataItem = (GridDataItem) e.DetailTableView.ParentItem;
           pnlVO item = (pnlVO) dataItem.DataItem;
           string portfolioCode = "";
           string portfolioTypeCode = "";
           string strategy = "";
           int strategyId = 0;
           string date = "";
           switch (e.DetailTableView.Name)
           {
               case "PortfolioCode":
                   portfolioCode = dataItem.GetDataKeyValue("PortfolioCode").ToString();
                   date = dataItem.GetDataKeyValue("AsOfDate").ToString();
                   e.DetailTableView.DataSource = ViewUtil.Instance.GetByPortfolioTypeCode(portfolioCode, date);
                   break;
               case "PortfolioTypeCode":
   ERROR HERE ===> portfolioTypeCode = dataItem.GetDataKeyValue("PortfolioTypeCode").ToString();
                   portfolioCode = dataItem.GetDataKeyValue("PortfolioCode").ToString();
                   date = dataItem.GetDataKeyValue("AsOfDate").ToString();
                   e.DetailTableView.DataSource = ViewUtil.Instance.GetByStrategy(portfolioTypeCode, portfolioCode, date);
                   break;
               case "Strategy":
                   portfolioTypeCode = dataItem.GetDataKeyValue("PortfolioTypeCode").ToString();
                   portfolioCode = dataItem.GetDataKeyValue("PortfolioCode").ToString();
      strategyId = Convert.ToInt32(dataItem.GetDataKeyValue("StrategyId").ToString());
                   date = dataItem.GetDataKeyValue("AsOfDate").ToString();
                   e.DetailTableView.DataSource = ViewUtil.Instance.GetBySymbolCode(portfolioTypeCode, portfolioCode, strategyId, date);
                   break;
           }
       }


Can someone please help me? I know for a fact that the PortfolioTypeCode column is being populated because i can see it in Table 1. Why is it not able to get the datakeyvalue? It looks to me like it can't get the datakeyvalue from a detail nested table but can only read the datakeyvalues whos columns are found within the main table. Anyway around this?

Thanks!

5 Answers, 1 is accepted

Sort by
0
CG
Top achievements
Rank 1
answered on 13 Oct 2011, 08:36 PM
How would i get the DataKeyName from the previous nested table if it exists only in the nested table and not in the main table?
0
CG
Top achievements
Rank 1
answered on 14 Oct 2011, 02:30 PM
bump! Anyone?
0
Jayesh Goyani
Top achievements
Rank 2
answered on 14 Oct 2011, 03:08 PM
Hello,

protected void RadGrid1_DetailTableDataBind(object sender, GridDetailTableDataBindEventArgs e)
    {
        dynamic data = new[] {
                new { ID = 1, Name ="Name1",mydate=DateTime.Now.AddHours(1)},
                new { ID = 2, Name = "Name2",mydate=DateTime.Now.AddMinutes(10)},
                new { ID = 3, Name = "Name3",mydate=DateTime.Now.AddSeconds(500)},
                new { ID = 4, Name = "Name4",mydate=DateTime.Now},
                new { ID = 5, Name = "Name5",mydate=DateTime.Now}
            };
 
        GridDataItem item = e.DetailTableView.ParentItem as GridDataItem;
        if (e.DetailTableView.Name == "Child1")
        {
            string ID = item.GetDataKeyValue("ID").ToString();
            e.DetailTableView.DataSource = data;
        }
        if (e.DetailTableView.Name == "Child2")
        {
            string ID = item.OwnerTableView.ParentItem.GetDataKeyValue("ID").ToString();
            string Name = item.GetDataKeyValue("Name").ToString();
        }
    }
.....................
........................
 <MasterTableView DataKeyNames="ID" Name="Parent1">
            <DetailTables>
                <telerik:GridTableView Name="Child1" DataKeyNames="Name">
                <DetailTables>
                     <telerik:GridTableView Name="Child2">
                        <Columns>
......................
.....................

Let me know if any concern.


Thanks,
Jayesh Goyani
0
CG
Top achievements
Rank 1
answered on 14 Oct 2011, 03:20 PM
Hi Jayesh,

Thank you for the reply. However I don't think this addresses the issue. I forgot to mention one thing:

For the main table I call a query that brings back the following columns:

PortfolioCode

,

 

SUM(DailyPNL) AS 'TotalDailyPNL',

 

SUM(MTDPNL) AS 'Total MTDPNL',

SUM(YTDPNL) AS 'Total YTDPNL',

SUM(BaseMktValue) AS 'TotalBaseMktValue',

AsOfDate

I take the expanded/selected row's PortfolioCode column value and run another query that returns the following to be populated in sub Table 1:

 

PortfolioCode,  

PortfolioTypeCode,  

SUM(DailyPNL) AS 'TotalDailyPNL',
SUM(MTDPNL) AS 'Total MTDPNL',
SUM
(YTDPNL) AS 'Total YTDPNL',
SUM(BaseMktValue) AS 'TotalBaseMktValue',

AsOfDate

 


THEN, when a user selects/expands another row from the sub Table 1, and i attempt to take the PortfolioTypeCode from this subtable and query to get the following columns:

 

PortfolioCode,  

PortfolioTypeCode,  

Strategy,  

StrategyId, 

SUM(DailyPNL) AS 'TotalDailyPNL',
SUM
(MTDPNL) AS 'Total MTDPNL',
SUM
(YTDPNL) AS 'Total YTDPNL',
SUM(BaseMktValue) AS 'TotalBaseMktValue',  

AsOfDate


The error happens, i think, because the PortfolioTypeCode icolumn s not found in the Main table. I think that the only datakeynames that really matter are the ones that are linked to the Master Table. So my question is how can i get what PortfolioTypeCode the user selected from the detail table IF that PortfolioTypeCode Column is not found in the master table?

 

0
CG
Top achievements
Rank 1
answered on 14 Oct 2011, 03:51 PM
Hello all,

I actually figured this one out and thought i'd post the answer here incase anyone in the future has this same problem.

If you have a column that you need to get the value of that is not found as a column in your master table, use this code:

GridDataItem

 

 

dataItem = (GridDataItem) e.DetailTableView.ParentItem;

 

XXXXXXXXX= dataItem[

 

"XXXXXXXXX"].Text;

 

where XXXXX is the name of your column.

Hope this helps!

Tags
Grid
Asked by
CG
Top achievements
Rank 1
Answers by
CG
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Share this question
or