从细胞中获得价值从RowDataBound事件的GridView [英] Getting value from a cell from a gridview on RowDataBound event
问题描述
string percentage = e.Row.Cells[7].Text;
我试图做一些动态的东西与我的GridView的,所以我的有线了一些code到RowDataBound事件。我想从特定细胞,这是一个TemplateField值。但是,code以上似乎总是返回一个空字符串。
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>
在一个相关的说明,没有人知道是否有行中选择单元格的更好的方法。它吮吸投入细胞[1]
。我不能做细胞[mycellname]
,所以如果我决定改变我的细胞的顺序,虫子不会出现?
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?
推荐答案
首先你需要用你的code在标签
或文字
控制,这样就可以正确地引用它。发生的事情是,有没有办法让系统来跟踪它,因为没有与文字相关的控制。这是控制的责任,它的内容添加到视图状态。
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.
您需要使用gridView.FindControl(控件名称);以获得该行中的控制。从那里,你可以在它的属性,包括文本
得到。
You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including Text
.
您还可以在有关该行的DataItem属性,并将其转换为适当的类型和直接提取信息。
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.
这篇关于从细胞中获得价值从RowDataBound事件的GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!