Quick processing on the Master/Slave table editing interface on the Winform interface, winform Master/Slave

Source: Internet
Author: User

Quick processing on the Master/Slave table editing interface on the Winform interface, winform Master/Slave

In Winform development, apart from regular Single-table information input, we sometimes design interfaces such as data display and editing for multiple master-slave tables, the information of a single table is generally the one-to-one correspondence between the control and the object, and then you can call the API to save the information. The master and slave tables require additional special processing. This article describes how to quickly implement the master-slave table editing interface, it is very convenient to implement real-time data editing in the list by combining the GridControl's GridView control object.

1. Design and display the Master/Slave Table Interface

A master-slave Table generally involves more than two tables. One is a master table and the other is a slave table. In actual situations, there are usually many tables, here we analyze and process the relationship between the two tables.

For example, the relationship between the two reimbursement application forms is as follows.

For the reimbursement Master/Slave table information, we can display it in the list, as shown in the following interface, divided into two parts: one is the master table information, and the other is the slave table information, click the master table information to display the list of corresponding slave tables.

When a new primary table record is added, a new interface is displayed for data maintenance and processing to facilitate the entry of the Master/Slave table information, as shown in the following figure.

The above interface includes the information of the master table and the information of the slave table (Real-time input in the GridView). After entering the information, submit the information in a unified manner.

 

2. process the Master/Slave table editing interface

This section describes how to edit a Master/Slave table, that is, how to implement the preceding interface.

The code for initializing the GridView is as follows.

/// <Summary> /// display the GridView data of the initialization schedule /// </summary> private void InitDetailGrid () {// The initial clearing column this. gridView1.Columns. clear (); // set some columns to hide this. gridView1.CreateColumn ("ID", "No "). visible = false; this. gridView1.CreateColumn ("Header_ID", "primary table number "). visible = false; this. gridView1.CreateColumn ("Apply_ID", "Application No "). visible = false; // Add a drop-down list and bind the data source this. gridView1.CreateColumn ("FeeType", "fee type", 100 ). createCo MboBox (). bindDictItems ("billing type"); // create a date column and specify the format var OccurTime = this. gridView1.CreateColumn ("OccurTime", "occurrence time", 120 ). createDateEdit (); OccurTime. editMask = "yyyy-MM-dd HH: mm"; OccurTime. displayFormat. formatString = "yyyy-MM-dd HH: mm"; // create the value column this. gridView1.CreateColumn ("FeeAmount", "fee amount "). createSpinEdit (); // create the remarks column this. gridView1.CreateColumn ("FeeDescription", "fee description", 200 ). createMemoEdit (); // initialize Gr IdView, you can add a column this. gridView1.InitGridView (GridType. newItem, false, EditorShowMode. mouseDownFocused, ""); // the content of the escape column shows this. gridView1.CustomColumnDisplayText + = new CustomColumnDisplayTextEventHandler (gridView1_CustomColumnDisplayText); // process the cell style this. gridView1.RowCellStyle + = new RowCellStyleEventHandler (gridView1_RowCellStyle); // the header sorting is not allowed this. gridView1.OptionsCustomization. allowSort = false; // draw sequence number This. gridView1.CustomDrawRowIndicator + = (s, e) => {if (e. info. isRowIndicator & e. rowHandle> = 0) {e. info. displayText = (e. rowHandle + 1 ). toString () ;}}; // perform non-null verification on the input cell. this. gridView1.ValidateRow + = delegate (object sender, ValidateRowEventArgs e) {var result = gridControl1.ValidateRowNull (e, new string [] {"FeeType "});}; // initialize this. gridView1.InitNewRow + = (s, e) => {gridView1.SetRowC EllValue (e. rowHandle, "ID", Guid. newGuid (). toString (); gridView1.SetRowCellValue (e. rowHandle, "Header_ID", tempInfo. ID); gridView1.SetRowCellValue (e. rowHandle, "Apply_ID", tempInfo. apply_ID); gridView1.SetRowCellValue (e. rowHandle, "OccurTime", DateTime. now) ;};} void gridView1_RowCellStyle (object sender, DevExpress. xtraGrid. views. grid. rowCellStyleEventArgs e) {GridView = this. gridView1; If (e. column. fieldName = "FeeAmount") {e. appearance. backColor = Color. green; e. appearance. backColor2 = Color. lightCyan ;}} void gridview1_mcustomcolumndisplaytext (object sender, DevExpress. xtraGrid. views. base. customColumnDisplayTextEventArgs e) {string columnName = e. column. fieldName; if (e. column. columnType = typeof (DateTime) {if (e. value! = Null) {if (e. value = DBNull. value | Convert. toDateTime (e. value) <= Convert. toDateTime ("1900-1-1") {e. displayText = "";} else {e. displayText = Convert. toDateTime (e. value ). toString ("yyyy-MM-dd HH: mm"); // yyyy-MM-dd }}}}

The above code has detailed remarks, mainly because we can create the corresponding displayed fields based on the relationship between the database tables. If the fields need to be hidden, do not display them (to obtain the corresponding values)

// Set some columns to hide this. gridView1.CreateColumn ("ID", "No "). visible = false; this. gridView1.CreateColumn ("Header_ID", "primary table number "). visible = false; this. gridView1.CreateColumn ("Apply_ID", "Application No "). visible = false;

To bind a field similar to the drop-down list, create the corresponding data type and call the BIND function to bind it. See the following code.

// Add a drop-down list and bind the data source this. gridView1.CreateColumn ("FeeType", "billing type", 100). CreateComboBox (). BindDictItems ("billing type ");

If you need to set the format display or mask for some special input, as shown below:

// Create a date column and specify the format var OccurTime = this. gridView1.CreateColumn ("OccurTime", "occurrence time", 120 ). createDateEdit (); OccurTime. editMask = "yyyy-MM-dd HH: mm"; OccurTime. displayFormat. formatString = "yyyy-MM-dd HH: mm ";

It is worth noting that when we add a row from the table record, we need to record some attributes of the master table. In this way, we are initializing the row, assign a value to the hidden column of the slave table.

// Initialize this. gridView1.InitNewRow + = (s, e) => {gridView1.SetRowCellValue (e. rowHandle, "ID", Guid. newGuid (). toString (); gridView1.SetRowCellValue (e. rowHandle, "Header_ID", tempInfo. ID); gridView1.SetRowCellValue (e. rowHandle, "Apply_ID", tempInfo. apply_ID); gridView1.SetRowCellValue (e. rowHandle, "OccurTime", DateTime. now );};

If you need to display the information of the primary table on the page, you can obtain the corresponding primary Table Record object based on the conditions and display it to the interface control.

/// <Summary> /// display the general object content /// </summary> /// <param name = "info"> </param> private void DisplayInfo (reimbursementInfo info) {tempInfo = info; // assign a value to the temporary object to point to the existing record object txtCategory. text = info. category; txtReason. text = info. reason; txtTotalAmount. value = info. totalAmount; txtNote. text = info. note ;}

While saving, we re-assign the interface content to the corresponding primary table object.

/// <Summary> /// edit or save the value function. /// </summary> /// <param name = "info"> </param> private void setInfo (ReimbursementInfo info) {info. category = txtCategory. text; info. reason = txtReason. text; info. totalAmount = txtTotalAmount. value; info. note = txtNote. text; info. applyDate = DateTime. now; info. applyDept = base. loginUserInfo. deptId; info. currentLoginUserId = base. loginUserInfo. ID ;}

When we need to obtain the detailed input of the GridView, we can retrieve the row record of the GridView through a function traversal and convert it to the corresponding object, as shown below.

/// <Summary> /// obtain the details List /// </summary> /// <returns> </returns> private List <ReimbursementDetailInfo> GetDetailList () {var list = new List <ReimbursementDetailInfo> (); for (int I = 0; I <this. gridView1.RowCount; I ++) {var detailInfo = gridView1.GetRow (I) as ReimbursementDetailInfo; if (detailInfo! = Null) {list. Add (detailInfo) ;}} return list ;}

After this information is processed, you can save the detailed table information while saving the master table.

/// <Summary> /// save data in the new State /// </summary> /// <returns> </returns> public override bool SaveAddNew () {ReimbursementInfo info = tempInfo; // existing local variables must be used, because some information may be appended with SetInfo (info); info. creator = base. loginUserInfo. ID; info. createTime = DateTime. now; try {# region add data bool succeed = BLLFactory <Reimbursement>. instance. insert (info); if (succeed) {// you can add other associated operations var list = GetDetailList (); foreach (var detailInfo in list) {BLLFactory <ReimbursementDetail>. instance. insertUpdate (detailInfo, detailInfo. ID);} return true;} # endregion} catch (Exception ex) {LogTextHelper. error (ex); MessageDxUtil. showError (ex. message);} return false ;}

Code

BLLFactory<ReimbursementDetail>.Instance.InsertUpdate(detailInfo, detailInfo.ID);

You can save new records or update existing records.

Through the above introduction, we can see that the logic of different Master/Slave tables is still very common. We can extract their logic and quickly generate it using the code generation tool.

 

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.