Datagaridview as Calendar
-
2012年3月7日 7:54
Hi,
I need my datagridview to act as a calendar for me to use it further to schedule employees weekly working hours.
How can I make datagridview to look as calendar for a month. I have given a try to browse but nothing went in favor of me.
Please suggest an example if it can be done.
Thanks,
Regards,
Bobby
Ayyagari
- 移動 CoolDadTxMVP 2012年3月7日 16:15 Winforms related (From:Visual C# General)
すべての返信
-
2012年3月7日 8:43
Do you need the MonthCalendar control: http://msdn.microsoft.com/en-us/library/system.windows.forms.monthcalendar.aspx.
Marco Minerva [MCPD]
Blog: http://blogs.ugidotnet.org/marcom
Twitter: @marcominerva -
2012年3月7日 8:45
Why don't use a DateTimePicker control? It serves your purpose far better than making a DataGridView a calendar.
Anyways, if you want to go with dataGridView only, I have create below function which accepts a DataGridView, year and month and makes the DataGridView a Month calander.
private void FillDataGridView(DataGridView dgv, int year, int month) { //Setup DataGridView Columns and Rows dgv.Rows.Clear(); dgv.Columns.Clear(); dgv.Columns.Add("colSunday", "Sun"); dgv.Columns.Add("colSunday", "Mon"); dgv.Columns.Add("colSunday", "Tue"); dgv.Columns.Add("colSunday", "Wed"); dgv.Columns.Add("colSunday", "Thu"); dgv.Columns.Add("colSunday", "Fri"); dgv.Columns.Add("colSunday", "Sat"); dgv.Rows.Add(5); dgv.RowHeadersVisible = false; //Set the width of columns as well as DataGridView int totalWidth = 0; foreach (DataGridViewColumn col in dgv.Columns) { col.Width = (int)(TextRenderer.MeasureText(col.HeaderText, dgv.Font).Width * 1.5); totalWidth += col.Width; } dgv.Width = totalWidth + 3; dgv.Height = 155; //Fill the Dates into DataGridView int days = DateTime.DaysInMonth(year, month); int currentRow = 0; for (int i = 1; i <= days; i++) { DateTime tempDate = new DateTime(year, month, i); switch (tempDate.DayOfWeek) { case DayOfWeek.Sunday: dgv[0, currentRow].Value = i; break; case DayOfWeek.Monday: dgv[1, currentRow].Value = i; break; case DayOfWeek.Tuesday: dgv[2, currentRow].Value = i; break; case DayOfWeek.Wednesday: dgv[3, currentRow].Value = i; break; case DayOfWeek.Thursday: dgv[4, currentRow].Value = i; break; case DayOfWeek.Friday: dgv[5, currentRow].Value = i; break; case DayOfWeek.Saturday: dgv[6, currentRow].Value = i; currentRow++; break; } }
I hope this helps.Please mark this post as answer if it solved your problem. Happy Programming!
-
2012年3月7日 9:12
Hi bbby A programmer,
You can achieve this with the DataGridView.Add seven columns to your dgv (eg. DataGridView), which will represent each day of the week. I added mine starting with Sunday, ending with Saturday. Then set the following properties in the designer.
[Appearance]
Row Template -> DividerHeight = 2
[Behavior]
AllowUserToAddRows = False
AllowUserToDeleteRows = FalseNow add an event handler for RowsAdded and the following code
void dataGridView1_RowsAdded(objectsender, DataGridViewRowsAddedEventArgse) {
for(inti = e.RowIndex; i < e.RowIndex + e.RowCount; i++) {
dataGridView1.Rows[i].Height = 92;
}
}Where 92 is your desired default height for each row. How it looks is mainly personal preference but this is a good start. This is the achieved result with what I provided you.
Currently developing FaultTrack. I occassionally blog about C# and .NET.
Hoping to become a MVP by 2013. Email: danderson [at] dcomproductions [dot] com -
2012年3月7日 15:11
Hi Dave,
I tried to follow your instructions but the row height is not getting changed. Here is the code I have used.
this.dataGridView1.Rows.Add("1", "2", "3", "4", "5", "6", "7"); this.dataGridView1.Rows.Add("8", "9", "10", "11", "12", "13", "14"); this.dataGridView1.Rows.Add("15", "16", "17", "18", "19", "20", "21"); this.dataGridView1.Rows.Add("22", "23", "24", "25", "26", "27", "27"); this.dataGridView1.Rows.Add("28", "29", "30", "31");
and in RowsAdded evemt i put the following code
for (int i = e.RowIndex; i < e.RowIndex + e.RowCount; i++) { dataGridView1.Rows[i].Height = 500; }Please advice..
Thanks,
Regards,
Bobby
Ayyagari
-
2012年3月7日 15:48dude.. try my solution, instead of hardcoding the values.
Please mark this post as answer if it solved your problem. Happy Programming!
-
2012年3月7日 15:59
Hi Adavesh,
Thanks for your reply. Yes, your code works fine but, how can I change grid's row height?
I want my rows in the grid to be filled as of the size of my grid. Now my grid is getting re sized.
Thanks,
Regards,
BobbyAyyagari
-
2012年3月7日 16:27
It's very simple. Comment the line dgv.Height = 155; from my code. Then, you can calculate rows height as, total dataGridView height devided by number of rows plus header. Just like below.
int totalHeight = (dgv.Height - dgv.Columns[0].HeaderCell.Size.Height) / dgv.RowCount; foreach (DataGridViewRow item in dgv.Rows) { item.Height = totalHeight; }
I hope this helps.Please mark this post as answer if it solved your problem. Happy Programming!
- 回答としてマーク bbby A programmer 2012年3月8日 2:52
-
2012年3月7日 16:39
Hi Adavesh,
I have tried as you mentioned. But seems to be not working.
The following image is the output for the code.
My grid is getting re sized as in the above image.
Thanks,
Regards,
Bobby
Ayyagari
-
2012年3月7日 16:47
Do on control load:
DataGridView.AutoSize == true; DataGridView.AutoSizeRowsMode = DataGridView.AutoSizeRowsMode.AllCells; DataGridView.RowTemplate.Height = 100; //set here the height //maybe even: DataGridView1.AutoSizeRowsMode = None;
Mitja
-
2012年3月8日 2:52
Hi,
Thanks for the help. It worked fine.
Regads,
Bobby
Ayyagari

