-4

Here I need to create one gridview and add values from the text file . Text files contains list of email ids. After displaying those email ids in row by row , i need to fetch those one by one and split it by "@" character . In next columns i need to show those split values. Like as follows

Email             username   domain

abcd@gmail.com     abcd      gmail
catdog@gmail.com   catdog    gmail
4

1 回答 1

2

Create class Email:

public class Email
{
    public Email(string email)
    {
        Address = email;
        int index = email.IndexOf('@');
        UserName = email.Substring(0, index);
        Domain = email.Substring(index + 1);
    }

    public string Address { get; private set; }    
    public string UserName { get; private set; }    
    public string Domain { get; private set; }
}

And data-bind your grid to instances of this class:

var emails = from line in File.ReadLines(fileName)
             select new Email(line);

grid.DataSource = emails.ToList();

If you need first column to be named Email, then add columns manually and provide names of email class properties as DataPropertyName for each column.

UPDATE: There is already existing class MailAddress in System.Net.Mail namespace which you can use for same purpose:

var emails = from line in File.ReadLines(fileName)
             select new MailAddress(line);

It has properties Address, User and Host which serve your needs.

于 2013-07-30T07:26:23.563 回答