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


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.
Who is Participating?
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 />
andycrellinAuthor Commented:
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!
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
andycrellinAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.