在创建的列表中选择行中的项目

时间:2015-02-10 15:46:59

标签: c#

我目前正在使用 JobComponent 类中的方法在类 ExpandWindow 中创建一个列表。如何从 jobDetail 访问 JobList 中的 JobDateTime ,并将值分配给 DateTimeTextBlock

以下代码:

ExpandWindow.cs

    public ExpandWindow(int jobId)
    {
        InitializeComponent();
        List<JobComponent.JobList> jobDetail = JobComponent.SelectJobBooking(jobId);
    }

JobComponent.cs

    public static List<JobList> SelectJobBooking(int jobId)
    {
        const string query = "SELECT t1.datetime FROM booking t1 " +
                             "WHERE t1.id=@id";
        var jobList = new List<JobList>();
        using (var cmd = new MySqlCommand(query, DbObject.Connection))
        {
            if (DbObject.Connection.State != ConnectionState.Open)
            {
                DbObject.OpenConnection();
            }
            cmd.Parameters.AddWithValue(("@id"), jobId);
            try
            {
                using (MySqlDataReader dataReader = cmd.ExecuteReader())
                {
                    while (dataReader.Read())
                    {
                        var item = new JobList
                        {
                            JobDateTime = dataReader["datetime"] + ""
                        };
                        jobList.Add(item);
                    }
                    dataReader.Close();
                    DbObject.CloseConnection();
                    return jobList;
                }
            }
            catch (Exception ex)
            {
                ErrorHandlingComponent.LogError(ex.ToString());
                throw;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

不完全确定你在这里尝试了什么,但是你会想到如何获得JobDateTime。

 public ExpandWindow(int jobId)
    {
        InitializeComponent();
        List<JobComponent.JobList> jobDetail = JobComponent.SelectJobBooking(jobId);

        if(jobDetail == null) return;

        var item = jobDetail.FirstOrDefault();
        if(item == null) return;

        yourDatetimeControl.Value = item.JobDateTime;
    }