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

acessing Table's DataObject

3 Answers 342 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gustavo
Top achievements
Rank 1
Gustavo asked on 22 Mar 2012, 10:39 PM
Hello,
I have a problem acessing a Table's DataObject that lies in the Detail Section.
The Detail Section's Datasource is set to a list of Questions and I want to display
a List with the possible answers of each Question. So I use the ItemDataBind of
the Detail Section to set each Processing.Table Datasource:

carta.Items["detail"].ItemDataBinding += detail_ItemDataBind;
((List)carta.Items["detail"].Items["ListAnswers"]).ItemDataBinding += ListAnswers_ItemDataBind;

private void detail_ItemDataBind(object sender, EventArgs e)
{
            var detail = sender as Telerik.Reporting.Processing.DetailSection;
            var processingTable = detail.ChildElements.Find("ListAnswers", true)[0] as Telerik.Reporting.Processing.Table;

            Question question = detail.DataObject.RawData as Question;
            processingTable.DataSource = question.AnswersList;
}

this works fine, but if I want to access each Answer object within the ListAnswers ReportItem I get a ClassCast exception, because
the List.DataObject is set to the same object as Detail.DataObject, even though I set a different Datasource to them:

private void ListAnswers_ItemDataBind(object sender, EventArgs e)
{
            Telerik.Reporting.Processing.Table tableProc = (Telerik.Reporting.Processing.Table)sender;

            // class cast error here, RawData is a Question instead of Answer, but tableProc.Datasource is a list of Answer
            Answer answer = (Answer) tableProc.DataObject.RawData;
}

What am I missing ?

3 Answers, 1 is accepted

Sort by
0
Accepted
Elian
Telerik team
answered on 26 Mar 2012, 12:40 PM
Hello Gustavo,

The event List_ItemDataBinding appears only once, before the whole data for the List Item is bound. In the current context the DataObject will be the DetailSection's DataObject (a.k.a. the Question). If your list contains, for example, a TextBox (let's say it's name is TextBoxAnswer), then if you want an event called for each answer in the List<Answer>, you should hook up to TextBoxAnswer_ItemDataBinding. This event will appear on each answer and each binding of the TextBoxAnswer. 

All the best,
Elian
the Telerik team
NEW in Q1'12: Telerik Report Designer (Beta) for ad-hoc report creation. Download as part of Telerik Reporting Q1 2012. For questions and feedback, use the new Telerik Report Designer Forum.
0
Gustavo
Top achievements
Rank 1
answered on 26 Mar 2012, 02:36 PM
Thanks for your attention, Elian. What you say makes sense, altough I had no luck making it this way, still getting the same ClassCast error:


report.DataSource = getQuestions();

report.Items["detail"].ItemDataBinding += detail_ItemDataBound;

//hook up to the ItemDataBinding for the textBox inside the list 
report.Items["detail"].Items["listAnswers"].Items["panelAnswers"].Items["txtAnswer"].ItemDataBinding += txtAnswer_ItemDataBind;

private void detail_ItemDataBound(object sender, EventArgs e)
{
    var detail = sender as Telerik.Reporting.Processing.DetailSection;
    var processingTable = detail.ChildElements.Find("listAnswers", true)[0] as Telerik.Reporting.Processing.Table;

    Question question = detail.DataObject.RawData as Question;
    processingTable.DataSource = question.listAnswers;
}

private void txtAnswer_ItemDataBind(object sender, EventArgs e)
{
    //get the textbox from the sender object            
    Telerik.Reporting.Processing.TextBox textBox = (Telerik.Reporting.Processing.TextBox)sender;
    //get the table object
    Telerik.Reporting.Processing.Table table = (Telerik.Reporting.Processing.Table)textBox.Parent.Parent;
    
    //class cast error here, table.DataObject.RawData is a Question instead of an Answer,
    //but table.Datasource is a list of Answers.
    
    Answer answer = (Answer) table.DataObject.RawData;
}


Maybe the problem is setting the processingTable Datasource dinamically inside detail_ItemDataBound ?
0
Gustavo
Top achievements
Rank 1
answered on 26 Mar 2012, 09:01 PM
Uh, never mind, didn't realize that every item has a DataObject property, I should access (Answer) textBoxAnswer.DataObject instead of listAnswers.DataObject. Thanks for your help.
Tags
General Discussions
Asked by
Gustavo
Top achievements
Rank 1
Answers by
Elian
Telerik team
Gustavo
Top achievements
Rank 1
Share this question
or