Torna al Thread

private void InitializeGrid() { dataGridView1 = new DataGridView(); dataGridView1.Dock = DockStyle.Fill; dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; dataGridView1.Parent = this; } private void LoadFirstMonth(int year) { DateTime dt = new DateTime(year, 1, 1); for (int i = 0; i < DateTime.DaysInMonth(year, 1); i++) { int dayNumber = i + 1; dt = new DateTime(year, 1, dayNumber); CreateColumn(dt.DayOfWeek); } int row = dataGridView1.Rows.Add(); dataGridView1.Rows[row].HeaderCell.Value = dt.ToString("MMMM"); } private void LoadGridData(int year) { for (int i = 0; i < 12; i++) { DateTime dt = new DateTime(year, i + 1, 1); int row = 0; if (i > 0) { row = dataGridView1.Rows.Add(); dataGridView1.Rows[row].HeaderCell.Value = dt.ToString("MMMM"); } for (int j = 0; j < this.dataGridView1.ColumnCount; j++) { this.dataGridView1.Columns[j].Width = 25; } int startFrom = 0; for (int j = 0; j < DateTime.DaysInMonth(year, i + 1); j++) { DateTime currentDay = new DateTime(year, i + 1, j + 1); //calculate start position if (j == 0) { int month = i + 1; int day = j + 1; DateTime x = new DateTime(year, month, day); startFrom = First(x.DayOfWeek.ToString()); } // Create extra columns if is neded. if (dataGridView1.Columns.Count <= j + startFrom) { CreateColumn(currentDay.DayOfWeek); } // Set cell value. dataGridView1[j + startFrom, row].Value = currentDay.Day.ToString(); } } } private int First(string n) { for (int i = 0; i < dataGridView1.Columns.Count; i++) { if (dataGridView1.Columns[i].Name == n) { return i; } } return 0; } private void CreateColumn(DayOfWeek dayOfWeek) { string day1 = dayOfWeek.ToString(); int columnIndex = dataGridView1.Columns.Add(dayOfWeek.ToString(), day1.Remove(3, day1.Length - 3)); if (dayOfWeek == DayOfWeek.Saturday || dayOfWeek == DayOfWeek.Sunday) { dataGridView1.Columns[columnIndex].DefaultCellStyle.BackColor = ControlPaint.Light(Color.LightGray); } }
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5