string percentage = e.Row.Cells.Text;
I am trying to do some dynamic stuff with my GridView, so I have wired up some code to the RowDataBound event. I am trying to get the value from a particular cell, which is a TemplateField. But the code above always seems to be returning an empty string.
To clarify, here is a bit the offending cell:
<asp:TemplateField HeaderText="# Percentage click throughs"> <ItemTemplate> <%# AddPercentClickThroughs((int)Eval("EmailSummary.pLinksClicked"), (int)Eval("NumberOfSends")) %> </ItemTemplate> </asp:TemplateField>
On a related note, does anyone know if there is a better way of selecting the cell in the row. It sucks putting in
cell. Couldn't I do
cell["mycellname"], so if I decide to change the order of my cells, bugs wont appear?
First you need to wrap your code in a
Literal control so that you can reference it properly. What's happening is that there's no way for the system to keep track of it, because there's no control associated with the text. It's the control's responsibility to add its contents to viewstate.
You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including
You can also get at the DataItem property of the Row in question and cast it to the appropriate type and extract the information directly.
why not pull the data directly out of the data source.