How to change column value before binding to gridview after retrieving from database?

After I got the dataset from the database, I need to change the value of the rows before binding to the gridview. for example, a dataset is retrieved from a database.

for example: [userid], [userEmail] → 1, james@hotmail.com

I would like to change " james@hotmail.com " to "james" and then bind it to a gridview. Each line of [userEmail] will be separated by a mail extension (@ hotmail.com) ... how should I do ??

+3
source share
2 answers

Something like this should work:

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

DataBound 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

+2

Source: https://habr.com/ru/post/1775117/


All Articles