2010-11-16 83 views
3

當我從數據庫中檢索到一組數據後,我需要在綁定到gridview之前編輯行值。例如, 例如,從數據庫中檢索一組數據表。如何在從數據庫檢索後綁定到gridview之前編輯列值?

例如:[用戶ID],[USEREMAIL] - > 1,[email protected]

我想改變 「[email protected]」 到 「詹姆斯」 然後將其綁定到gridview的。 [userEmail]的每一行都將用郵件擴展名(@ hotmail.com)分隔... 我應該怎麼做..?

回答

6

像這樣的東西應該工作:

DataTable dt = getMyDataTable(); 
foreach (DataRow dr in dt.Rows) 
{ 
    string email = Convert.ToString(dr["email"]); 
    email = email.Substring(0, email.IndexOf('@')); 
    dr["email"] = email; 
} 
2

您需要進軍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

相關問題