Binding to generic list of objects

Thread is closed for posting
2 posts, 0 answers
  1. Jonathan
    Jonathan avatar
    134 posts
    Member since:
    Sep 2007

    Posted 02 Oct 2012 Link to this post

    Hi I have a generic list of objects that I would like to bind to a bar graph that have the following properties:

    public decimal TotalValue
    {
        get
        {
            return _totalValue;
        }
        set
        {
            _totalValue = value;
        }
    }
     
    public decimal TotalHours
    {
        get
        {
            return _totalHours;
        }
        set
        {
            _totalHours = value;
        }
    }
     
    public string FieldActivityNameWUnit
    {
        get
        {
            return _fieldActivityNameWUnit;
        }
        set
        {
            _fieldActivityNameWUnit = value;
        }
    }
     
    public int ActivityMonth
    {
        get
        {
            return _activityMonth;
        }
        set
        {
            _activityMonth = value;
        }
    }
     
    public string ActivityMonthName
    {
        get
        {
            return _activityMonthName;
        }
        set
        {
            _activityMonthName = value;
        }
    }
     
    public string FieldActivity
    {
        get
        {
            return _fieldActivity;
        }
        set
        {
            _fieldActivity = value;
        }
    }

    If you look at the attached graphic, really all I want is the totalvalue displayed.not the month number or the totalhours.  how do I get rid of these other two series?  here is my code so far:
        protected void CreateYearChart()
        {
     
            RadChartProjectActivityYearly.Legend.Clear();
            RadChartProjectActivityYearly.Series.Clear();
     
            FieldActivity _activity = FieldActivityService.GetByID(Int32.Parse(ddlFieldActivity.SelectedValue.ToString()));
            RadChartProjectActivityYearly.ChartTitle.TextBlock.Text = _activity.Unit + " in " + ddlDetailYear.SelectedItem.Text;
     
            List<FactProjectActivityTotalsByMonth> _factsBar1 = FactProjectActivityTotalsByMonthService.GetForYear(Int32.Parse(ddlDetailYear.SelectedItem.Text), Int32.Parse(ddlFieldActivity.SelectedValue.ToString()));
     
            RadChartProjectActivityYearly.DataSource = _factsBar1;
     
            RadChartProjectActivityYearly.Legend.Visible = true;
     
    //        RadChartProjectActivityYearly.Series[0].DataYColumn = "TotalValue";
            RadChartProjectActivityYearly.PlotArea.XAxis.DataLabelsColumn = "ActivityMonthName";
             
            RadChartProjectActivityYearly.DataBind();
            RadChartProjectActivityYearly.AutoLayout = true;
     
            foreach (Telerik.Charting.ChartSeries series in RadChartProjectActivityYearly.Series)
            {
                Debug.WriteLine(series.Name);
                //if (series.Name != "TotalValue")
                //{
                //    RadChartProjectActivityYearly.Series.Remove(series);
                //}
            }
     
            //RadChartProjectActivityYearly.RemoveSeriesAt(2);
            //RadChartProjectActivityYearly.RemoveSeriesAt(0);

    I thought about creating the series manually but then I lose the X-axis labels so ideally I'd like to be able to databind to only one property of the object in the underlying list.  Her is the declaration:
    <telerik:RadChart ID="RadChartProjectActivityYearly" runat="server" skin="Forest" Width="1000px" >
    <Legend>
     <Appearance Position-AlignedPosition="TopRight"></Appearance>
     </Legend>
        <PlotArea>
            <XAxis AutoScale="False" DataLabelsColumn="ActivityMonthName" ></XAxis>
        </PlotArea>
    </telerik:RadChart>
    thanks for the help
  2. Petar Kirov
    Admin
    Petar Kirov avatar
    425 posts

    Posted 04 Oct 2012 Link to this post

    Hello Jonathan,

    I believe your problem is that you have commented out this line:
    RadChartProjectActivityYearly.Series[0].DataYColumn = "TotalValue";
    Without it the chart can not know which property to treat as YValue.

    You can check this example of data binding to generic list of objects for reference.

    Let us know how it works for you.

    Regards,
    Petar Kirov
    the Telerik team
    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 their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top