带有日期标题的Datagridview

时间:2016-03-07 09:46:13

标签: c# winforms datagridview

我有一个带有约会信息的数据网格视图。有四列NameAppointment TypeAppointment DateAppointment Time。我需要找到任何约会的日期,并显示每天的标题与日期和日期。它看起来像这样:

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

1 个答案:

答案 0 :(得分: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:

        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;
            }
        }