在DataGridView
中只能有一個標題行。 我會建議突出顯示(或更好的暗光)DataGrid中的DateRows。
下面是一個簡單示例如何做到這一點的樣子: ![enter image description here](https://i.stack.imgur.com/GoOnz.png)
我做了什麼:
首先,我添加了描述你的數據的類。
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;
}
}
好了,請告訴我問題或問題(知道某人將編輯文本並添加一些內容,以後讓我看起來很蠢).. – BugFinder
I需要每一行與新的日期顯示標題與日期和日期,然後每個約會的日期,直到日期改變,然後新的行與標題,對不起,如果我不清楚 – PSC
你還沒有發佈一個問題。或者一個問題。你做了一系列的陳述。我們不代碼給你,我們幫你弄清楚什麼是錯的。 – BugFinder