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

How to use RadListViewDataItem.GetDataKeyValue when RadListView with Grouping

1 Answer 459 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Robin
Top achievements
Rank 2
Robin asked on 22 Oct 2013, 10:26 AM
Hi,

I've a radlistview and I used its itemDataBound event to attach datakey value to another control to do some staffs.
Previously, my radlistview don't have grouping. But with new requirement, I added grouping to my listview.
Then, I'm getting "Argument out of range exception" when accessing data key value inside ItemDataBound event.
I've reproduced the error and you can see in my sample project below.

This is my Markup File of RadListView.
<telerik:RadListView ID="lvTest" runat="server" attrib="Temp"
    ItemPlaceholderID="ItemPlaceHolder1" AllowPaging="true"
    OnNeedDataSource="lvTest_NeedDataSource"
    OnItemDataBound="lvTest_ItemDataBound"
    GroupAggregatesScope="AllItems"
    DataKeyNames="product_id">
    <ItemTemplate>
        <table style="width:100%;background-color:whitesmoke;border-bottom:1px solid silver;">
            <tr>
                <td style="width:100px">Code:
                </td>
                <td>
                    <%#Eval("Code") %>
                    <asp:Button ID="btnLookup" runat="server" Text="View Details" />
                </td>
            </tr>
            <tr>
                <td>Name:
                </td>
                <td>
                    <%#Eval("Name") %>
                </td>
            </tr>
            <tr>
                <td>Description:
                </td>
                <td>
                    <%#Eval("Desc") %>
                </td>
            </tr>
        </table>
    </ItemTemplate>
    <LayoutTemplate>
        <asp:Panel ID="DataGroupPlaceHolder1" runat="server">
        </asp:Panel>
    </LayoutTemplate>
    <DataGroups>
        <telerik:ListViewDataGroup GroupField="company_name" DataGroupPlaceholderID="DataGroupPlaceHolder1">
            <DataGroupTemplate>
                <table style="border: 1px solid lightgrey; background-color: silver; width:100%;">
                    <tr>
                        <td style="background-color: silver; color: white; font-weight: bold;">
                            <%# (Container as RadListViewDataGroupItem).DataGroupKey %>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Panel runat="server" ID="ItemPlaceHolder1"></asp:Panel>
                        </td>
                    </tr>
                </table>
            </DataGroupTemplate>
            <GroupAggregates>
                <telerik:ListViewDataGroupAggregate Aggregate="Min" DataField="company_id"/>
            </GroupAggregates>
        </telerik:ListViewDataGroup>
    </DataGroups>
    <GroupSeparatorTemplate>
        <div style="background-color: lightgray;height:10px">
        </div>
    </GroupSeparatorTemplate>
</telerik:RadListView>

This is my code behind.
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        lvTest.DataBind();
    }
}
 
protected void lvTest_NeedDataSource(object sender, Telerik.Web.UI.RadListViewNeedDataSourceEventArgs e)
{
    lvTest.DataSource = GetDataTable();
}
 
private object GetDataTable()
{
    DataTable l_Table = new DataTable();
    l_Table.Columns.Add("product_id");
    l_Table.Columns.Add("Code");
    l_Table.Columns.Add("Name");
    l_Table.Columns.Add("Desc");
    l_Table.Columns.Add("company_id");
    l_Table.Columns.Add("company_name");
 
    l_Table.Rows.Add(new string[] { "1", "C001", "Code 001", "Description 001", "1", "Company 1" });
    l_Table.Rows.Add(new string[] { "2", "C002", "Code 002", "Description 002", "1", "Company 1" });
    l_Table.Rows.Add(new string[] { "3", "C003", "Code 003", "Description 003", "1", "Company 1" });
 
    l_Table.Rows.Add(new string[] { "4", "C004", "Code 004", "Description 004", "2", "Company 2" });
    l_Table.Rows.Add(new string[] { "5", "C005", "Code 005", "Description 005", "2", "Company 2" });
    l_Table.Rows.Add(new string[] { "6", "C006", "Code 006", "Description 006", "2", "Company 2" });
    l_Table.Rows.Add(new string[] { "7", "C007", "Code 007", "Description 007", "2", "Company 2" });
    l_Table.Rows.Add(new string[] { "8", "C008", "Code 008", "Description 008", "2", "Company 2" });
 
    l_Table.Rows.Add(new string[] { "9", "C009", "Code 009", "Description 009", "3", "Company 3" });
    l_Table.Rows.Add(new string[] { "10", "C010", "Code 010", "Description 010", "3", "Company 3" });
    l_Table.Rows.Add(new string[] { "11", "C011", "Code 011", "Description 011", "3", "Company 3" });
    l_Table.Rows.Add(new string[] { "12", "C012", "Code 012", "Description 012", "3", "Company 3" });
 
    l_Table.Rows.Add(new string[] { "13", "C013", "Code 013", "Description 013", "4", "Company 4" });
    l_Table.Rows.Add(new string[] { "14", "C014", "Code 014", "Description 014", "4", "Company 4" });
    l_Table.Rows.Add(new string[] { "15", "C015", "Code 015", "Description 015", "4", "Company 4" });
    l_Table.Rows.Add(new string[] { "16", "C016", "Code 016", "Description 016", "4", "Company 4" });
 
    l_Table.Rows.Add(new string[] { "17", "C017", "Code 017", "Description 017", "5", "Company 5" });
 
    l_Table.Rows.Add(new string[] { "18", "C018", "Code 018", "Description 018", "6", "Company 6" });
    l_Table.Rows.Add(new string[] { "19", "C019", "Code 019", "Description 019", "6", "Company 6" });
 
    l_Table.Rows.Add(new string[] { "20", "C020", "Code 020", "Description 020", "7", "Company 7" });
 
    l_Table.Rows.Add(new string[] { "21", "C021", "Code 021", "Description 021", "8", "Company 8" });
    l_Table.Rows.Add(new string[] { "22", "C022", "Code 022", "Description 022", "8", "Company 8" });
    l_Table.Rows.Add(new string[] { "23", "C023", "Code 023", "Description 023", "8", "Company 8" });
    l_Table.Rows.Add(new string[] { "24", "C024", "Code 024", "Description 024", "8", "Company 8" });
 
    return l_Table;
}
 
protected void lvTest_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
{
    try
    {
        if (e.Item is RadListViewDataItem)
        {
            if (e.Item.FindControl("btnLookup") != null)
            {
                try
                {
                    string l_product_ID = (e.Item as RadListViewDataItem).GetDataKeyValue("product_id").ToString();
                    (e.Item.FindControl("btnLookup") as Button).Text = "View: " + l_product_ID;
                }
                catch (Exception)
                {

                }
            }
        }
    }
    catch (Exception ex)
    {
 
    }
}

In this sample project, I change button text with DataKeyValue.
It works only for first two items. Then it throw "argument out of range exception".
I have no idea what's wrong with my code.
You can see it by throwing exception from inner try-catch block.

This is my first time using radlistview with grouping.
So, if there is anything I configured incorrectly, please point me out.
Is data structure OK? (see GetDataTable())

Thanks in advanced, 
Robin

1 Answer, 1 is accepted

Sort by
0
Marin
Telerik team
answered on 25 Oct 2013, 11:21 AM
Hello,

 Thank you for reporting the problem. We have logged it and will try to resolve it for the next release of the controls.
Meanwhile you can use the following workaround to manually calculate the correct index of the data item:

int itemIndex = 0;
 
    protected void lvTest_ItemDataBound(object sender, Telerik.Web.UI.RadListViewItemEventArgs e)
    {
        var dataItem = e.Item as RadListViewDataItem;
        if (dataItem != null)
        {
            var btnLookUp = dataItem.FindControl("btnLookup") as Button;
            if (btnLookUp != null)
            {
                string l_product_ID = lvTest.DataKeyValues[itemIndex]["product_id"].ToString();
                btnLookUp.Text = "View: " + l_product_ID;
                itemIndex++;
            }
        }
    }


I hope this helps.

Regards,
Marin
Telerik
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to the blog feed now.
Tags
ListView
Asked by
Robin
Top achievements
Rank 2
Answers by
Marin
Telerik team
Share this question
or