C# 在 RowDataBound 事件上从网格视图中获取单元格的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/121722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting value from a cell from a gridview on RowDataBound event
提问by qui
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.
我试图用我的 GridView 做一些动态的事情,所以我已经将一些代码连接到 RowDataBound 事件。我正在尝试从特定单元格获取值,该单元格是 TemplateField。但是上面的代码似乎总是返回一个空字符串。
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?
在相关说明中,有没有人知道是否有更好的方法来选择行中的单元格。插入很糟糕cell[1]
。我不能这样做cell["mycellname"]
,所以如果我决定改变我的单元格的顺序,错误就不会出现?
采纳答案by Orion Adrian
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.
首先,您需要将代码包装在Label
或Literal
控件中,以便您可以正确引用它。发生的事情是系统无法跟踪它,因为没有与文本相关的控件。将其内容添加到视图状态是控件的责任。
You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including Text
.
您需要使用 gridView.FindControl("controlName"); 获得该行的控制权。从那里你可以得到它的属性,包括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.
您还可以获取相关行的 DataItem 属性并将其转换为适当的类型并直接提取信息。
回答by Stephen Wrighton
why not pull the data directly out of the data source.
为什么不直接从数据源中拉出数据。
DataBinder.Eval(e.Row.DataItem, "ColumnName")
回答by Chris
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.
当您使用 TemplateField 并将文字文本绑定到它时,asp.net 实际上会为您插入一个控件!它被放入一个 DataBoundLiteralControl。如果您查看获得空文本的代码行附近的调试器,您就会看到这一点。
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!
这会让你得到你的文字!
回答by Chris
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:
以上是很好的建议,但您可以在网格视图中获取单元格的文本值,而无需将其包装在文字或标签控件中。您只需要知道要连接什么事件。在这种情况下,请改用 DataBound 事件,如下所示:
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 a debugger, you will see the text appear in this method.
运行调试器时,您将看到此方法中出现的文本。
回答by Ahmad
Just use a loop to check your cell in a gridview for example:
只需使用循环来检查 gridview 中的单元格,例如:
for (int i = 0; i < GridView2.Rows.Count; i++)
{
string vr;
vr = GridView2.Rows[i].Cells[4].Text; // here you go vr = the value of the cel
if (vr == "0") // you can check for anything
{
GridView2.Rows[i].Cells[4].Text = "Done";
// you can format this cell
}
}
回答by SEO Service
use RowDataBound
function to bind data with a perticular cell, and to get control use
(ASP Control Name like DropDownList) GridView.FindControl("Name of Control")
使用RowDataBound
函数将数据与特定单元格绑定,并获得控件使用(ASP Control Name like DropDownList)GridView.FindControl("Name of Control")
回答by Vaibhav Saran
Label lblSecret = ((Label)e.Row.FindControl("lblSecret"));
回答by Mark Meuer
I had a similar question, but found the solution through a slightly different approach. Instead of looking up the control as Chris suggested, I first changed the way the field was specified in the .aspx page. Instead of using a <asp:TemplateField ...>
tag, I changed the field in question to use <asp:BoundField ...>
. Then, when I got to the RowDataBound event, the data could be accessed in the cell directly.
我有一个类似的问题,但通过稍微不同的方法找到了解决方案。我没有像 Chris 建议的那样查找控件,而是首先更改了在 .aspx 页面中指定字段的方式。<asp:TemplateField ...>
我没有使用标签,而是将相关字段更改为使用<asp:BoundField ...>
. 然后,当我到达 RowDataBound 事件时,可以直接在单元格中访问数据。
The relevant fragments: First, the aspx page:
相关片段: 一、aspx页面:
<asp:GridView ID="gvVarianceReport" runat="server" ... >
...Other fields...
<asp:BoundField DataField="TotalExpected"
HeaderText="Total Expected <br />Filtration Events"
HtmlEncode="False" ItemStyle-HorizontalAlign="Left"
SortExpression="TotalExpected" />
...
</asp:Gridview>
Then in the RowDataBound event I can access the values directly:
然后在 RowDataBound 事件中,我可以直接访问这些值:
protected void gvVarianceReport_Sorting(object sender, GridViewSortEventArgs e)
{
if (e.Row.Cells[2].Text == "0")
{
e.Row.Cells[2].Text = "N/A";
e.Row.Cells[3].Text = "N/A";
e.Row.Cells[4].Text = "N/A";
}
}
If someone could comment on whythis works, I'd appreciate it. I don't fully understand why without the BoundField the value is not in the cell after the bind, but you have to look it up via the control.
如果有人可以评论为什么这有效,我将不胜感激。我不完全理解为什么没有 BoundField 绑定后值不在单元格中,但您必须通过控件查找它。
回答by user3679550
<asp:TemplateField HeaderText="# Percentage click throughs">
<ItemTemplate>
<%# AddPercentClickThroughs(Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "EmailSummary.pLinksClicked")), Convert.ToDecimal(DataBinder.Eval(Container.DataItem, "NumberOfSends")))%>
</ItemTemplate>
</asp:TemplateField>
public string AddPercentClickThroughs(decimal NumberOfSends, decimal EmailSummary.pLinksClicked)
{
decimal OccupancyPercentage = 0;
if (TotalNoOfRooms != 0 && RoomsOccupied != 0)
{
OccupancyPercentage = (Convert.ToDecimal(NumberOfSends) / Convert.ToDecimal(EmailSummary.pLinksClicked) * 100);
}
return OccupancyPercentage.ToString("F");
}
回答by ashiq
protected void gvbind_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvbind, "Select$" + e.Row.RowIndex);
}
}