views:

23151

answers:

6
string percentage = e.Row.Cells[7].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.

Any ideas?

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[1]. Couldn't I do cell["mycellname"], so if I decide to change the order of my cells, bugs wont appear.

+3  A: 

First you need to wrap your code in a Label or 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 Text.

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.

Orion Adrian
But i dont actually have a control (that i know of!) in the cell, I am just using a method to put a string in there
qui
You do NOT need to wrap your code in a Label or Literal. See my answer below.
Chris
A: 

why not pull the data directly out of the data source.

DataBinder.Eval(e.Row.DataItem, "ColumnName")
Stephen Wrighton
It's not in the datasource, its generated dynamically, i have edited my question to clarify
qui
+2  A: 

When you use a TemplateField and bind literal text to it like you are doing, asp.net will actually insert a control FOR YOU! It gets put into a DataBoundLiteralControl. You can see this if you look in the debugger near your line of code that is getting the empty text.

So, to access the information without changing your template to use a control, you would cast like this:

string percentage = ((DataBoundLiteralControl)e.Row.Cells[7].Controls[0]).Text;

That will get you your text!

Chris
+2  A: 

The above are good suggestions, but you can get at the text value of a cell in a grid view without wrapping it in a literal or label control. You just have to know what event to wire up. In this case, use the DataBound event instead, like so:

protected void GridView1_DataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[0].Text.Contains("sometext"))
            {
                e.Row.Cells[0].Font.Bold = true;
            }
        }
    }

When running debugger, you will see the text appear in this method.

--Harry

This worked for me perfectly - Thanks
leen3o
A: 

??? some help with this please. The ByVal E As System.EventArgs is being used by another portion of the code. ??? And suggestions or help would be appreciated.

Protected Sub SubmitBtn6_Click(ByVal Sender As Object, ByVal E As System.EventArgs, ByVal S As GridViewRowEventArgs) Handles SubmitBtn6.Click

Dim percentage As String = DirectCast(S.Row.Cells(5).Controls(0), DataBoundLiteralControl).Text Label1.Text = percentage

Compiler Error Message: BC30408: Method 'Protected Sub SubmitBtn6_Click(Sender As Object, E As System.EventArgs, S As System.Web.UI.WebControls.GridViewRowEventArgs)' does not have the same signature as delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.

Stephen
A: 

Someone please help me... I have a gridview and it contains a linkbutton in its templatefield. What I want to know is, when I click on the linkbutton,I need he text of the linkbutton on a string 'name'???

pRAVeEN