string percentage = e.Row.Cells[7].Text;

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

有什么想法吗?

澄清一下,这里有一个令人讨厌的细胞:

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

在相关的说明中,有没有人知道是否有更好的方法来选择行中的单元格。它很糟糕。cell[1]。我不能cell["mycellname"],所以如果我决定改变我的细胞的顺序,就不会出现错误?

有帮助吗?

解决方案

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

您需要使用gridView.FindControl(<!> quot; controlName <!> quot;);获得行中的控件。从那里你可以得到它的属性,包括Text

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

其他提示

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

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

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

因此,要在不更改模板的情况下访问信息以使用控件,您可以这样投射:

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

那会得到你的文字!

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

运行调试器时,您会看到文本出现在此方法中。

只需使用循环来检查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 
    }
}

使用RowDataBound函数将数据绑定到特定单元格,并使用控件 (ASP控制名称如DropDownList)GridView.FindControl("Name of Control")

我有一个类似的问题,但通过略有不同的方法找到了解决方案。我没有像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,值在绑定后不在单元格中,但你必须通过控件查找它。

Label lblSecret = ((Label)e.Row.FindControl("lblSecret"));
<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");
}

如果您将asp:BoundField上的属性可见设置为 False 。喜欢这个

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

循环行时,会在Cells [i] .Text属性中获取任何文本。所以

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
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);
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top