Solved

MonthCalendar and DataGridView - Very Slow Loading.

Posted on 2009-04-06
7
1,955 Views
Last Modified: 2013-12-17
I have a DataGridView with several columns.  If a user selects a cell in the date column a monthCalendar control appears at that location.  

My issue is that it is extremely slow (it appears fast, but attempting to select a date seems to take a long time).  I have used the exact same code (posted below) for a textBox click event and it works perfectly fast.  Does anybody know what may be wrong?

if (dataGridView.CurrentRow.Cells["transactionDate"].Selected)

                    {

                        monthCalendar.Visible = true;

                        monthCalendar.Top = 40;

                        monthCalendar.Left = 50;

                        monthCalendar.BringToFront();

                        monthCalendar.Capture = true;

                        while (monthCalendar.Visible == true)

                        {

                            Application.DoEvents();

                        }

                        dataGridView.CurrentRow.Cells["transactionDate"].Value = _accountDate;

                        return;

                    }
 

 private void monthCalendar_MouseUp(object sender, MouseEventArgs e)

        {

            switch (monthCalendar.HitTest(e.Location).HitArea)

            {

                case MonthCalendar.HitArea.Date:

                    monthCalendar.Visible = false;

                    break;

                case MonthCalendar.HitArea.NextMonthDate:

                    break;

                case MonthCalendar.HitArea.PrevMonthDate:

                    break;

            }

            _accountDate = monthCalendar.SelectionStart.ToShortDateString();

        }

Open in new window

0
Comment
Question by:angus_young_acdc
  • 4
  • 3
7 Comments
 
LVL 15

Expert Comment

by:oobayly
ID: 24075530
I'd suggest using a custom DataGridViewColumn that hosts a DateTimePicker control, once you've created it, it can be used all over the place with very little code.

This MSDN article explains how to do it. Once you understand how it works, it's simple to create a whole host (no pun intended) of custom columns. So far I've got a DateTimePicker column, a NumericUpDown column and a column that allows editing of binary data by displaying it as a hex string.

http://msdn.microsoft.com/en-us/library/7tas5c80.aspxhttp://msdn.microsoft.com/en-us/library/7tas5c80.aspx
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24075677
In the example provided it is adding the CalendarColumn to the grid as a new column.  How can I assign the custom class to a column that already exists?  Or how can I actually use this?  I currently have lots of code for the other columns and seems like a re-write would be required.
0
 
LVL 15

Expert Comment

by:oobayly
ID: 24076102
Well, presuming that your columns are autogenerated when binding to a datatable, you can just add the following code:
It uses the same header text and dataproperty as the original column that contains a date, inserts itself in the same position, and then hides the original.

As for your code, I'm guessing it's to do with the while loop. Why not remove the loop, and set the column's date after you hide the Calendar control:
dataGridView.CurrentRow.Cells["transactionDate"].Value = monthCalendar.SelectionStart;

Why are you converting the date selected by the user to a string, is the Date column not DateTime?
You're only testing for one hittest type, so why use switch?
// Using Calendar column

      DataGridViewColumn autoGenDateCol = dataGridView1.Columns["Date Column"];

      CalendarColumn  calendarCol = new CalendarColumn ();

      calendarCol.HeaderText = autoGenDateCol.HeaderText;

      calendarCol.DataPropertyName = autoGenDateCol.DataPropertyName;

      calendarCol.Name = "Date Column2";

      dataGridView1.Columns.Insert(autoGenDateCol.Index, calendarCol);

      autoGenDateCol.Visible = false;
 

// Your modified code

if (dataGridView.CurrentRow.Cells["transactionDate"].Selected)

                    {

                        monthCalendar.Visible = true;

                        monthCalendar.Top = 40;

                        monthCalendar.Left = 50;

                        monthCalendar.BringToFront();

                        monthCalendar.Capture = true;

                        dataGridView.Enabled = false;

                        return;

                    }

 

 private void monthCalendar_MouseUp(object sender, MouseEventArgs e)

        {

            if (monthCalendar.HitTest(e.Location).HitArea == MonthCalendar.HitArea.Date){

              dataGridView.CurrentRow.Cells["transactionDate"].Value = monthCalendar.SelectionStart;

              monthCalendar.Visible = false;

              dataGridView.Enabled = true;

            }

        }

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24077966
Hi oobayly

Unfortunately not, they have been created just via Edit Columns as there is no real binding, it's taking a value from XML to populate the table.  

Good idea about hiding after populating the date, shall try that out now.  

I'm converting the date to a string as that's just the way it is read/written to the XML, so just seemed easier (albeit uglier) rather than just converting it later.

As for the switch... I'm not sure, should I just change it to an if statement for the HitArea.Date?
0
 
LVL 15

Accepted Solution

by:
oobayly earned 500 total points
ID: 24078064
Well, when you add the CalendarColumn to your project, it should appear in your list of available column types when editing the columns for a DataGridView, so editing them should be pretty easy, and you could dump the DataGridViewTextBoxColumn that is currently displaying the date.

Fair enough about using the date as a string if the datasource is xml, though I tend to do the conversions when loading and saving my tables, so any controls have the datatype that they expect. I suppose it's personal preference.

I'd use if instead of switch as your only checking for a single case. If you were handling any other HitArea then I'd definitely use a switch statement. Again, it's personal preference as I find it easier to read.
0
 
LVL 15

Author Comment

by:angus_young_acdc
ID: 24085191
Hi again,

I have tried that code but unfortunately I can't seem to get it working correctly.  I'm either getting a null reference exception or an invalid cast exception.

They all happen here:

 public override void InitializeEditingControl(int rowIndex, object
            initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle)
        {
            // Set the value of the editing control to the current cell value.
            base.InitializeEditingControl(rowIndex, initialFormattedValue,
                dataGridViewCellStyle);
            CalendarEditingControl ctl =
                DataGridView.EditingControl as CalendarEditingControl;
            ctl.Value = (DateTime)this.Value;  // Exception thrown
        }
0
 
LVL 15

Author Closing Comment

by:angus_young_acdc
ID: 31566937
Got it working by removing code and doing a bit of a hack.  Also it is a lot faster (most likely due to your suggestion about the loop).  Thanks a lot1
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now