コード例 #1
0
        private void LoadData()
        {
            db = new Module2Entities();
            var query = db.Schedules.ToList();

            if (comboFrom.SelectedValue.ToString() != "0")
            {
                query = query.Where(x => x.Route.Airport.IATACode == comboFrom.Text).ToList();
            }

            if (comboTo.SelectedValue.ToString() != "0")
            {
                query = query.Where(x => x.Route.Airport1.IATACode == comboTo.Text).ToList();
            }

            if (dtpOutbound.Checked == true)
            {
                query = query.Where(x => x.Date == dtpOutbound.Value.Date).ToList();
            }

            if (txtFlightNumber.Text != "")
            {
                query = query.Where(x => x.FlightNumber == txtFlightNumber.Text).ToList();
            }

            if (comboSort.Text == "Date-Time")
            {
                query = query.OrderByDescending(x => x.Date).ToList();
            }
            else if (comboSort.Text == "Economy Price")
            {
                query = query.OrderByDescending(x => x.EconomyPrice).ToList();
            }
            else
            {
                query = query.OrderByDescending(x => x.Confirmed).ToList();
            }

            dgv.DataSource = query.ToList().Select(x => new
            {
                x.ID,
                x.Date,
                Time = x.Time.ToString(@"hh\:mm"),
                From = x.Route.Airport.IATACode,
                To   = x.Route.Airport1.IATACode,
                x.FlightNumber,
                Aircraft        = x.Aircraft.Name,
                EconomyPrice    = "$" + Math.Round(x.EconomyPrice, 0),
                BusinessPrice   = "$" + (Math.Round(x.EconomyPrice, 0) + Math.Round(x.EconomyPrice * 0.35m, 0)),
                FirstClassPrice = "$" + (Math.Round(x.EconomyPrice, 0) + Math.Round(x.EconomyPrice * 0.35m, 0) + Math.Round((x.EconomyPrice + Math.Round(x.EconomyPrice * 0.35m)) * 0.3m, 0)),
                Color           = x.Confirmed == true ? "White" : "Red"
            }).ToList();

            dgv.Columns["ID"].Visible    = false;
            dgv.Columns["Color"].Visible = false;

            for (int i = 0; i < dgv.RowCount; i++)
            {
                if (dgv["Color", i].Value.ToString() == "Red")
                {
                    dgv.Rows[i].DefaultCellStyle.BackColor = Color.Red;
                    dgv.Rows[i].DefaultCellStyle.ForeColor = Color.White;
                }
            }
        }