2008-09-23 68 views
28
string percentage = e.Row.Cells[7].Text; 

我想用GridView做一些动态的东西,所以我已经将一些代码连接到RowDataBound事件。我试图从一个特定的单元格获取值,这是一个TemplateField。但上面的代码似乎总是返回一个空字符串。在RowDataBound事件中从GridView获取单元格的值

任何想法?

澄清,这里是一个有点违规细胞:

<asp:TemplateField HeaderText="# Percentage click throughs"> 
<ItemTemplate> 
    <%# AddPercentClickThroughs((int)Eval("EmailSummary.pLinksClicked"), (int)Eval("NumberOfSends")) %> 
</ItemTemplate> 
</asp:TemplateField> 

与此相关的,没有人知道是否有行中选择小区的更好的方法。它吸入放入cell[1]。难道我不能做cell["mycellname"],所以如果我决定改变我的细胞的顺序,错误不会出现?

+0

你在每一个回传数据绑定?你甚至有回传? – 2008-09-23 15:38:04

回答

11

首先,您需要将代码包装在LabelLiteral控件中,以便您可以正确引用它。发生的事情是,系统无法跟踪它,因为没有与文本相关的控制。控件的责任是将其内容添加到视图状态。

您需要使用gridView.FindControl(“controlName”);在行中获得控制权。从那里你可以得到其物业包括Text

您还可以获取相关行的DataItem属性,并将其转换为适当的类型并直接提取信息。

+0

但是我实际上并没有在单元格中有一个控件(我知道!),我只是使用一种方法将字符串放在那里 – qui 2008-09-23 15:31:43

+2

您不需要将代码封装在标签或文字中。请参阅下面的答案。 – Chris 2008-11-03 00:57:57

+0

周解决方案,有更好的方法来做到这一点。 – 2016-12-28 06:45:53

45

为什么不直接从数据源中提取数据。

DataBinder.Eval(e.Row.DataItem, "ColumnName") 
+0

它不在数据源中,它是动态生成的,我编辑了我的问题来澄清 – qui 2008-09-23 15:32:20

21

当您使用TemplateField并将文本文本像绑定一样绑定到它时,asp.net实际上会为您插入一个控件!它被放入DataBoundLiteralControl中。你可以看到这个,如果你看到调试器附近的代码行正在获取空文本。

因此,在不改变你的模板使用控制访问这些信息,你会投这样的:

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

,将让你的文字!

11

以上是很好的建议,但是您可以在网格视图中获取单元格的文本值,而无需将其包装在文字或标签控件中。你只需要知道要连接什么事件。在这种情况下,请使用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; 
     } 
    } 
} 

运行调试程序时,您将看到文本显示在此方法中。

6

只需使用一个循环来检查你的电池在例如一个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 
    } 
} 
1

使用RowDataBound功能与perticular结合细胞的数据,并获得控制使用 (ASP的控制名称类似的DropDownList)GridView.FindControl("Name of Control")

0
Label lblSecret = ((Label)e.Row.FindControl("lblSecret")); 
1

我有一个类似的问题,但通过稍微不同的方法找到解决方案。我没有按照Chris的建议查找控件,而是首先改变了在.aspx页面中指定字段的方式。我没有使用<asp:TemplateField ...>标签,而是将相关字段更改为使用<asp:BoundField ...>。然后,当我到达RowDataBound事件时,可以直接在单元格中访问数据。

相关片段:一,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> 

然后在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"; 
    } 
} 

如果有人能在为什么这个作品发表评论,我会很感激。我不完全理解为什么没有BoundField的值不在绑定后的单元格中,但是您必须通过控件查找它。

0
<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"); 
} 
-1
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); 
    } 
} 
0

如果设置上的ASP属性可见:绑定列于。像这样

<asp:BoundField DataField="F1" HeaderText="F1" Visible="False"/> 

得到的细胞任何文本[我]。文本属性,当你循环的行。所以

foreach (GridViewRow row in myGrid.Rows) 
{ 
    userList.Add(row.Cells[0].Text); //this will be empty "" 
} 

但是你可以设置为不通过电网连接到事件可见一列OnRowDataBound然后从这里做这个

e.Row.Cells[0].Visible = false //now the cell has Text but it's hidden 
相关问题