我正在尝试从员工出生日期的文本文件中获取数据,我已经将所有其他信息以字符串形式显示在我的表单上但不是DOB。以下是使用streamreader获取数据的代码。
public bool Load(string employeesFile)
{
List<string> lines = new List<string>();
using (StreamReader reader = new StreamReader("employees.txt"))
{
string line;
while ((line = reader.ReadLine()) != null)
{
//Splitting the data using |
string[] temp = line.Split('|');
//This is to populate an employees detials
Employee emp = new Employee()
{
firstName = temp[0],
lastName = temp[1],
address = temp[2],
postCode = temp[3],
phoneNumber = temp[4],
//dateOfBirth = temp.ToString[5]
};
接下来是表单中的代码,用于显示表单中的数据。
public partial class Salaried_Employee_Details : Form
{
public Salaried_Employee_Details(Employee emp)
{
InitializeComponent();
textBoxLastName.Text = emp.lastName;
textBoxFirstName.Text = emp.firstName;
textBoxAddress.Text = emp.address;
textBoxPostCode.Text = emp.postCode;
textBoxPhoneNumber.Text = emp.phoneNumber;
dateTimeDateOfBirth.Text = emp.dateOfBirth.ToString();
文件中的出生日期为1995 | 5 | 22格式。
如何从文本文件链接到表单中显示?
答案 0 :(得分:1)
您正在使用“|”拆分文本和日期采用“1995 | 5 | 22”格式,这意味着您的日期将分为三个部分。如果您获得最后三项(年,月,日),您可以设置这样的日期;
int year = Convert.Int32(temp[5]);
int month = Convert.Int32(temp[6]);
int day = Convert.Int32(temp[7]);
//This is to populate an employees detials
Employee emp = new Employee()
{
firstName = temp[0],
lastName = temp[1],
address = temp[2],
postCode = temp[3],
phoneNumber = temp[4],
dateOfBirth = new DateTime(year, month, day)
};