C# 页面加载时更改asp:BoundField的值

C# 页面加载时更改asp:BoundField的值,c#,asp.net,C#,Asp.net,我有以下资料: <Columns> ... <asp:BoundField DataField="datetime_added" HeaderText="When Added" ReadOnly="True" SortExpression="datetime_added" /> ... </Columns> 有办法做到这一点吗?使用templatefield而不是boundfield -带标签 -标签文本由函数通过如下方式提供: &l

我有以下资料:

<Columns>
    ...
    <asp:BoundField DataField="datetime_added" HeaderText="When Added" ReadOnly="True" SortExpression="datetime_added" />
    ...
</Columns>

有办法做到这一点吗?

使用templatefield而不是boundfield -带标签 -标签文本由函数通过如下方式提供:

<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label runat="server" Text='<%= SomeFunction(Eval("db_id"),Eval("description")) %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
调用函数时,SQL中的字段将作为对象传递 这样你就可以在你想要的时候使用它,或者根据你想要的返回其他东西

当值从每一行的数据源绑定时调用该函数,您可能还需要传递一个id来标识该行。

您是否尝试过“OnRowCreated”事件?我在这里假设您使用的是GridView,如果不是,那么在您将数据绑定到的任何组件中都可能有类似的东西

对于GridView,以下是一个示例:

protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // do what you need in here
        // e.Row.Cells[x] where x = the column number
    }
}

此外,此方法的速度可能会非常慢,具体取决于正在处理的行数。

如果在将值绑定到网格之前使用
RowDataBound
事件操纵值(在将每一行绑定到gridview时被调用)


你想在绑定到gridview之前还是在点击按钮之后编辑这些值?这就是我想要的。非常感谢。
protected string SomeFunction(object id, object description)
{
    //your code
    return "";
}
protected void gridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        // do what you need in here
        // e.Row.Cells[x] where x = the column number
    }
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
       string datetimeadded = DataBinder.Eval(e.Row.DataItem, "datetime_added").ToString();

       // or you can use the cell index to reference the boundfield like below
       string datetimeadded = e.Row.Cells[1].Text;
    }
}