当我从数据库中检索到一组数据后,我需要在绑定到gridview之前编辑行值。例如, 例如,从数据库中检索一组数据表。如何在从数据库检索后绑定到gridview之前编辑列值?
例如:[用户ID],[USEREMAIL] - > 1,[email protected]
我想改变 “[email protected]” 到 “詹姆斯” 然后将其绑定到gridview的。 [userEmail]的每一行都将用邮件扩展名(@ hotmail.com)分隔... 我应该怎么做..?
当我从数据库中检索到一组数据后,我需要在绑定到gridview之前编辑行值。例如, 例如,从数据库中检索一组数据表。如何在从数据库检索后绑定到gridview之前编辑列值?
例如:[用户ID],[USEREMAIL] - > 1,[email protected]
我想改变 “[email protected]” 到 “詹姆斯” 然后将其绑定到gridview的。 [userEmail]的每一行都将用邮件扩展名(@ hotmail.com)分隔... 我应该怎么做..?
像这样的东西应该工作:
DataTable dt = getMyDataTable();
foreach (DataRow dr in dt.Rows)
{
string email = Convert.ToString(dr["email"]);
email = email.Substring(0, email.IndexOf('@'));
dr["email"] = email;
}
您需要进军GridView中的数据绑定事件和编辑USEREMAIL值。
事情是这样的:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
// format the email, provided cell 1 is email
e.Row.Cells[1].Text = e.Row.Cells[1].Text.Substring(0, e.Row.Cells[1].Text.IndexOf("@"));
}
}
在您的ASPX文件:
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
onrowdatabound="CustomersGridView_RowDataBound"
runat="server">
</asp:gridview>
参考: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx