我可以在数据绑定(bind)后更新 gridview 列的内容吗? SQL 列是全名,我需要将其显示为姓氏、名字,而不是现在的名字和姓氏。我知道我可以在 sql 中做到这一点,但它似乎会有点痛苦(SQL: parse the first, middle and last name from a fullname field)。我只是想知道是否有更简单的方法来做到这一点。
谢谢
好的,我想通了...抱歉。如果其他人需要这个,我添加了一个 OnRowDataBound="MailingList_RowDataBound" 事件,并且在该事件处理程序中,我将使用以下代码(修改为解析名称):
public void MailingList_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Required to ignore the header/footer.
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Get the Person's Name
string name = e.Row.Cells[0].Text;
//Add your JavaScript onClick event link.
e.Row.Attributes.Add("onClick", "LookupEmail('" + name + "')");
e.Row.Cells[0].Text = "Test";
}
}
将“测试”更改为正确的值。
请您参考如下方法:
使用项目模板
<asp:GridView ID="tset" runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="label1" runat="server" Text='<%#GetFormattedName(Eval("full_name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
并在后面的代码中编写 GetFormattedName 方法以返回您想要的字符串,
protected string GetFormattedName(string fullName)
{
// you can split full name here and swap first name, last name
// depending on how you store it in DB
}
