MFC How to implement modifying tabular data when a list control is a report form

Source: Internet
Author: User

Disclaimer: The example in this article is to double-click the table data at any location to make changes to the cell data at the current position 1. First, the implementation of the table data in the list control, the specific code here does not describe the effect of the following figure:


2. Now to implement the function as stated in the declaration, double-click a cell to pop up a dialog box to enter a new value, and then replace the original value with the new value: 1> Important steps, be sure to set the list control extension style to full row selection, such as the Lvs_ex_fullrowselect style, if this style is not set, then there will be problems, M_list_tabledata is the control variable that the list control is associated with, LVS_EX _gridlines is the grid style that is set up above, and the code is as follows:

M_list_tabledata. SetExtendedStyle (M_list_tabledata. GetExtendedStyle () | lvs_ex_gridlines| Lvs_ex_fullrowselect);//Set the extended property of a list control to "full row check and grid"
2> Inserts a dialog resource, the design interface is shown in the following illustration:

and add a new class for the dialog box, Cdialogmodifydrillitem, and associate a CString type member variable for the edit box m_strnewvalue 3> Add a mouse double-click event to the list control, that is, the NM_DBLCLK event , the entire event response function code is as follows:

The iitem in the lpnmitemactivate structure here represents the line number of the double-click position of the mouse, isubitem the column number of the double-click position of the mouse, because I do is the database development, before modifying a record, you need to save this record to the history table. Then update the value of the corresponding field in the display table. So here's a question that gets the name of the class field for the cell that the mouse double-clicks on, because the name of the field needs to be used when updating the corresponding field value of the table, so there is a procedure that is detailed below:

Because of the confidentiality of the project, some code has been omitted, omitting the location of the code is explained:

//when the user double-clicks the left mouse button in a list control to trigger the event, the primary effect of the event is to double-click an item to modify its value void Cdialogquerydrillvertree::onnmdblclklisttabledata
(NMHDR *pnmhdr, Lresult *presult)
	{Lpnmitemactivate pnmitemactivate = reinterpret_cast<lpnmitemactivate> (PNMHDR); TODO: Add controls here to notify Handler code if (Pnmitemactivate->iitem = 1)//If the user mouse double-clicks without a point in the list control position, then prompts the error message {AfxMessageBox ("You did not click on any item.") 
	"); else {CString bhid=m_list_tabledata. GetItemText (pnmitemactivate->iitem,0);//pnmitemactivate->iitem is the line number that represents the double-click position of the mouse,pnmitemactivate-> Isubitem the column number for the double-click position of the mouse,//and the expanded style of the list control must be set to Lvs_ex_fullrowselect, that is, full row selection, so that you can click on any column in a row to get the correct line number, and then get bhid field//

		AfxMessageBox (Bhid); Before you modify, insert the original record value into the corresponding history table, and the corresponding code has been omitted/omitted to insert the records into the history table to code//.................//
		Cdialogmodifydrillitem m_dlgmodify;//Pop-up Cdialogmodifydrillitem dialog box to receive user modified value if (M_dlgmodify.domodal () ==IDOK)// If the M_dlgmodify dialog box clicks the OK button {M_list_tabledata. Setitemtext (Pnmitemactivate->iitem,pnmitemactivate->isubitem,m_dlgmodify.m_strnewvalue); Pnmitemactivate->isubitem is the column number for the double-click position of the mouse, which is used to replace the original value with M_strnewvalue (the new user modified value) based on the row number/********************** Gets the column field name of the mouse double-click position, starts ****************************///pcolumn, specifies the address of the information retrieval lvcolumn structure, and gets information about the column. The mask member specifies which column properties to retrieve.
			If the mask member specifies a Lvcf_text value, the Psztext member must contain the received text and Cchtextmax the member must specify the address of the buffer's size buffer.
			Lvcolumn Col; CString ziduan;//Store Gets the column field name col.mask=lvcf_text;//set Mask member specify Lvcf_text value col.cchtextmax=100;//and CCHTEXTMAX member must specify buffer
			The address of the size buffer. Col.psztext =ziduan. GetBuffer the//psztext member must contain the received text (that is, the Ziduan string) if (M_list_tabledata.
				GetColumn (Pnmitemactivate->isubitem,&col))//Get the Table header column field information for the column where the mouse double-clicks the location {ziduan=col.psztext; AfxMessageBox (Ziduan);//test using} Ziduan. ReleaseBuffer ()//free/********************** get mouseDouble-click the column field name of the position to end ****************************/ 
			This updates the new value of the corresponding field in the reality table, and the code has been omitted
			Here the code has omitted ...//
			AfxMessageBox ("modified successfully."). ");
		}
	}

	*presult = 0;
}
The 4> effect is shown below:


The modification was successful.


Note: If the previous list control's extended style is not set to full row selection, double-clicking any cell does not get the row number of the cell's row, so if you do not set it, there will be a problem.

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.