Use the ComboBox control to implement the datagridview drop-down menu Function

Source: Internet
Author: User
VC ++. the data browsing control datagridview is added in net2005. Although we can add a drop-down box column to the datagridview using its datagridviewcomboboxcolumn method, the problem is that the drop-down box of the entire column is not beautiful, it also needs to be programmed to bind data to it, which does not match. net. I recently implemented VC ++. net200 through simple programming of its form control events
5. Add the drop-down box function to the datagridview Control for your reference:
Create a Windows form application in C ++ language in vs2005 Program And then add a dview control to form1.
The system will prompt you to bind data to the datagridview control. After completing the above work, add a ComboBox control combox1 on form1

Double-click the form1 form with the mouse to enter the form1_load event.CodeThe compiling process is as follows:

 

  
  
  1. Private VoidForm1_load (ObjectSender, eventargs E)
  2. {
  3. This. Combobox1.visible =False;
  4. This. Combobox1.width = 0;
  5. This. Datagridview1.controls. Add (This. Combox1 );
  6. }

Then add the following code to the currentcellchanged event of datagridview1:

 

 
 
  1. Try 
  2. {
  3. This. Combobox1.visible =False;
  4. This. Combobox1.width = 0;
  5. If(This. Maid. columnindex = 1)
  6. {
  7. This. Combobox1.visible =False;
  8. This. Combobox1.width = 0;
  9. This . combobox1.left = This . datagridview1.getcelldisplayrectangle ( This . datagridview1.currentcell. columnindex, This . datagridview1.currentcell. rowindex, true ). left;
  10. This . combobox1.top = This . datagridview1.getcelldisplayrectangle ( This . datagridview1.currentcell. columnindex, This . datagridview1.currentcell. rowindex, true ). top;
  11. This. Combobox1.width =This. Datagridview1.getcelldisplayrectangle (This. Maid. columnindex,This. Maid. rowindex,True). Width;
  12. StringContent = convert. tostring (This. Datagridview1.currentcell. value );
  13. This. Combobox1.text = content;
  14. This. Combobox1.visible =True;
  15. }
  16. Else 
  17. {
  18. This. Combobox1.visible =False;
  19. This. Combobox1.width = 0;
  20. }
  21. }
  22. Catch(Exception ex)
  23. {
  24. // MessageBox. Show (ex. Message );
  25. }

Here are some notes:

First:

 
  
  
  1. This. Maid. columnindex = 1

To determine which columns we want to display the drop-down menu. Change the number 1 to the column number you need.

Second point:

 

 
 
  1. This. Combobox1.left =This. Datagridview1.getcelldisplayrectangle (This. Maid. columnindex,This. Maid. rowindex,True). Left;
  2. This. Combobox1.top =This. Datagridview1.getcelldisplayrectangle (This. Maid. columnindex,This. Maid. rowindex,True). Top;
  3. This. Combobox1.width =This. Datagridview1.getcelldisplayrectangle (This. Maid. columnindex,This. Maid. rowindex,True). Width;

This is to determine the position of the current cell, and dynamically adjust the size of the ComboBox, so that he can completely overwrite the cell on the click, perfectly covering the bottom of the cell. However, if the content of the datagridview control is too large, the length will be too long. You need to modify the method for getting the location.

 

 

  
  
  1. This. Combobox1.left = viewer. Location. x +This. Viewer. getcelldisplayrectangle (This. Viewer. currentcell. columnindex,This. Viewer. currentcell. rowindex,True). Left;
  2. This. Combobox1.top = viewer. Location. Y +This. Viewer. getcelldisplayrectangle (This. Viewer. currentcell. columnindex,This. Viewer. currentcell. rowindex,True). Top;

 

Of course, the size does not need to be modified. No matter where the location is, the size is so large.

Third point:

The currentcellchanged event is triggered when the selected cell changes. There is a very similar event cellvaluechanged, which is triggered when the cell value changes. Now, you only need to click the cell in the corresponding column to bring up the drop-down menu, so you must select the former here.

The reason for using try and catch to wrap this code is that the program encountered an exception when initializing the dview. Data must be bound during initialization. The cell value must be assigned one by one, and no value is assigned in time. Naturally, currentcell is changed, which triggers currentcellchanged. However, Initialization is not over yet. All obtained values are null. This is why I commented out the MessageBox. Show () method in exception handling. There will be no impact.

Then go to the scroll event of datagridview1 and add the following code:

 

  
  
  1. This. Combobox1.visible =False;
  2. This. Combobox1.width = 0;

Then go to the selectionchangecommitted event of combox1 and add the following code:

 

 
  
  
  1. Datagridview1.currentcell. value = (system. Windows. Forms. ComboBox) sender). selecteditem. tostring ();

Enter the combox1 keypress event and add the following code:

  
  
  1. This. Combobox1.visible =False;
  2. This. Combobox1.width = 0;

The code added to the keypress event of combox1 mainly solves the problem of editing the data of the Unit in the drop-down box.
Option.

The following is an example of how to apply this method in my project.


Note: The above blue part and part of the diagram are matched, and the code is used to modify the added content for the blogger.

Original post address: http://www.cnblogs.com/dx0588/archive/2006/05/14/399902.html

 

Http://xiangsen.blog.51cto.com/236167/627297

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.