Adding User Control to each cell in a DataGrid column using attributes

Posted on 2005-05-05
Last Modified: 2010-04-07

I have a User Control that I can happily add to a page using, for example:

<myControl:Chart id="chart1" probL="0.15" prob="0.2" probU="0.35" runat="Server />

This works great and displays the image that the control draws on the fly. The image is a small graph based on the three decimal attributes.
However, I have a DataGrid control on the page that has three columns that contain the probL, prob and ProbU values for a number of rows of data (these are in a DataSet and the DataGrid is then bound to the DataSet) and I want to have a 4th column that has the graph produced by my MyControl:Chart in each cell using the three attributes on that row. I'm sure this is quite straight forward, but all of the examples I've found so far fall short of how to do this with a User Control. The datagrid should end up something like:

probL     Prob     probU                                                Graph
0.123     0.222    0.345     [image: graph based on values in row 1 using <MyControl:Chart>]
0.011     0.079    0.101     [image: graph based on values in row 2 using <MyControl:Chart>]
.. and so on

Cheers for any help.
Question by:andycrellin
    LVL 12

    Accepted Solution

    Try add this to the datagrid, you should do the same thing to prob and probU as to probL
                                  <asp:TemplateColumn HeaderText="Graph">
    <myControl:Chart id="chart1" probL=<%# DataBinder.Eval(Container.DataItem, "probL") %> prob="0.2" probU="0.35" runat="Server />

    Author Comment

    Sorry - should have clarified: How would I do this in code? The DataGrid is empty at dev. time and the columns are built (when the page is called) in the code-behind module. Or would this method still work?
    Thanks for the quick response by the way!
    LVL 12

    Expert Comment

    I think this should still work with other columns built in code

    And you can do it in code too,
    You can create a user control:
    Dim chart1 as New Chart

    Author Comment

    Just tried it and it's perfect. Was stumped for a little while with a String to Decimal conversion error before I realised that by the time probL gets into the datagrid it's in "0.00%" format! I used the method set out first - just means I need to sort out the column ordering but that should be simple enough.
    You managed to answer in 10 lines what I must have trawled through 20-odd tutorials to try to find out. Cheers.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
    User art_snob ( encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
    This video discusses moving either the default database or any database to a new volume.
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now