2016-03-07 147 views
0

我有一个带有约会信息的datagridview。有四列NameAppointment TypeAppointment Date,Appointment Time。我需要找到任何约会的当天,并显示每天的标题和日期和日期。它看起来像这样:带日期标题的Datagridview

**Monday 07/03/2016** 
Amy  New  13:00 
**Tuesday 08/03/2016** 
Betty Second 14:30 
Sally New  16:00 
+0

好了,请告诉我问题或问题(知道某人将编辑文本并添加一些内容,以后让我看起来很蠢).. – BugFinder

+0

I需要每一行与新的日期显示标题与日期和日期,然后每个约会的日期,直到日期改变,然后新的行与标题,对不起,如果我不清楚 – PSC

+2

你还没有发布一个问题。或者一个问题。你做了一系列的陈述。我们不代码给你,我们帮你弄清楚什么是错的。 – BugFinder

回答

0

DataGridView中只能有一个标题行。 我会建议突出显示(或更好的暗光)DataGrid中的DateRows。

下面是一个简单示例如何做到这一点的样子: enter image description here

我做了什么:

首先,我添加了描述你的数据的类。

public class Entry 
{ 
    public DateTime DateTime { get; set; } //Combine the Date and the Time 
    public string AppointmentType { get; set; } 
    public string Name { get; set; } 

    public override string ToString() 
    { 
     return Name + " " + AppointmentType + " " + DateTime.ToString("dd.MM.yyyy HH:mm:ss"); 
    } 
} 

然后创建一个用于调试的列表:

 List<Entry> entries = new List<Entry>(); //This is your Input 

     entries.Add(new Entry() 
     { 
      Name = "Betty", 
      AppointmentType = "New", 
      DateTime = DateTime.Now.AddDays(-1) 
     }); 

     entries.Add(new Entry() 
     { 
      Name = "Sally", 
      AppointmentType = "New", 
      DateTime = DateTime.Now 
     }); 

     entries.Add(new Entry() 
     { 
      Name = "Amy", 
      AppointmentType = "New", 
      DateTime = DateTime.Now.AddHours(-15) 
     }); 

你可能会从其他地方得到你的数据... 然后,它解析为List<Entry>

然后我排序的数据:

按日期
entries.Sort(new Comparison<Entry>((x, y) => DateTime.Compare(x.DateTime, y.DateTime))); -- Ascending 

entries.Sort(new Comparison<Entry>((x, y) => -DateTime.Compare(x.DateTime, y.DateTime))); --Descending 

然后组“时间:

 List<Entry> newList = new List<Entry>(); 
     //Now adding the new "headers" 
     for (int i = 0; i < entries.Count; i++) 
     { 
      DateTime dateOfAppointment = (DateTime)entries[i].DateTime; 
      if (i > 0) 
      { 
       if (dateOfAppointment.Date != entries[i - 1].DateTime.Date)//Not the same Date as the previous one => Add new Header with Date 
       { 
        newList.Add(new Entry() {DateTime = dateOfAppointment}); 
       } 
      }     
      //Else: we add the entry under the current date cause its still the same... 
      newList.Add(entries[i]);//Add Entry 
     } 

最后但并非最不重要的填写您的DataGridView

//Fill the Grid 
     for (int i = 0; i < newList.Count; i++) 
     { 
      dataGridView1.Rows.Add(new DataGridViewRow()); 

      dataGridView1.Rows[i].Cells[2].Value = newList[i].DateTime.ToString("dd.MM.yyyy"); 

      if (!string.IsNullOrEmpty(newList[i].Name)) //=> Is not a Header 
      { 
       dataGridView1.Rows[i].Cells[2].Value = newList[i].DateTime.ToString("dd.MM.yyyy"); 
       dataGridView1.Rows[i].Cells[0].Value = newList[i].Name; 
       dataGridView1.Rows[i].Cells[1].Value = newList[i].AppointmentType; 
       dataGridView1.Rows[i].Cells[3].Value = newList[i].DateTime.ToString("HH:mm:ss"); 
      } 
      else 
      { 
       dataGridView1.Rows[i].Cells[2].Value = newList[i].DateTime.ToString("dddd dd.MM.yyyy", new CultureInfo("en-GB")); 
       dataGridView1.Rows[i].Cells[0].Value = newList[i].Name = null; 
       dataGridView1.Rows[i].Cells[1].Value = newList[i].AppointmentType = null; 
       dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Gray; 
      } 
     }