Use the WinForm paging control in the DevExpress program to enter data directly and save

Source: Internet
Author: User

In general, we tend to use a relatively good independent interface to input or display the relevant data, so that the comparison specifications, but also easy to display more complex data. However, in some cases, we may also need to directly input or modify the data directly on the GridView table, this is less for the field, and relatively simple content, the efficiency is a relatively high input mode. This essay mainly introduces the implementation of using GridView to input data and saving directly in DevExpress program, and realizes operation of data direct input using WinForm paging control.

1. Display data on the GridView

In the GridView display data, only need to bind the data source to the corresponding Gridcontrol control can, relatively simple, can bind the data format is also many, can be a DataTable, can also support the IEnumerable format ilist<t> Collection objects and so on, in my framework, I tend to use the ilist<t> of this entity class collection.

Most examples on the web, the data source of the binding is mostly based on the DataTable, the data displayed in this way is subject to the characteristics of different databases, the fields may be case-insensitive, and the fields of devexpress operations are case-sensitive, so if you want the field name to get the value of the time , the incoming field names must be case-consistent, so when the database is different, such as the fields that Oracle queries to are all uppercase, SQL Server is in mixed mode (either Pascal or uppercase or lowercase).

The use of entity class collections has many advantages over the operation of the framework hierarchy, can be strongly typed, and is not affected by specific databases, and all interactions are handled through entity classes or their collections, with relatively great advantages.

In general, for the data source of an entity class, we can do data binding in the following way (using the data binding method of the paging control).

 This . Wingridviewpager1.pagerinfo); this. Wingridviewpager1.datasource = list;

If you need to add new modifications to the records on the GridView, that is, the state of the bound data needs to be stored, then you cannot use the List<t> collection directly, and you need to use the collection of its bindinglist<t>, as well BindingList <T> implements the Ilist<t> interface, which we can do type conversion through the constructor new bindinglist<t> (list), and then rebind to the data source.

Modify the above processing, we can get the following data binding method:

 /// <SUMMARY>  ///  binding Gridcontrol control's data source  /// Span style= "color: #808080;" ></summary>  private  void  Span style= "color: #000000;" > Binddata () { //  use BindingList to edit records, Otherwise, the new record content cannot be stored  var  list = Bllfactory<dictdata>.    Instance.find (Treeconditionsql);  this . Gridcontrol1.datasource = new  bindinglist<dictdatainfo> 

By default, if this is bound, then the field column name will be the attribute name of the entity class, English, obviously this does not conform to our display processing, we need to limit a few fields, and need to display Chinese content, then we should build several bound column fields for the GridView.

For example, we can construct a column of fields through a function, as shown below.

The createcolumn is to simplify the introduction of the extension function of the processing mode, mainly to realize the increase of the bound field, can be understood as the following code processing

This way, after we bind the data source, we can display some of the specified field contents, and the Chinese column name, as shown in the following effect.

2. Data entry and data storage on the GridView

However, the above section just shows how the data is bound to display, if you need to input data directly on the GridView, then you need to do some special code processing.

In the above we introduce the method of using bindinglist<t> data source to ensure that the entity class collection data can be displayed and entered, as shown in the following code binding data source.

If we need to set the GridView to be able to input data directly, then we need to set the following properties, especially the red line.

Falsetrue;   = Newitemrowposition.bottom;

For some general GridView initialization, we can implement its property setting through an extension function.

false, editorshowmode.mousedownfocused);

When we enter new data lines, we often need to set up some related attributes, such as the parent ID and so on, then we need to deal with the Initnewrow event, as shown below, we would like to keep a record of the record when the parent ID (source from the left tree Node ID information)

where the Code

" dicttype_id ", typeId); // The parent ID of the store record

We need to have a hidden field in this GridView (as in the dicttype_id field of this example), otherwise it cannot store the contents of this record.

If we need to complete a field after the entry, the other fields also make corresponding changes, such as trigger correlation changes, then can be processed in the Cellvaluechanged event, as in this example we want to increase the name, the value and name consistent change, then the code is as follows.

So that when we edit the interface, we can get the following effect.

Complete the above steps, if we need to check the input data after inputting the information, and submit the database processing, if the success of the prompt, then you need to deal with the event validaterow inside.

To verify the contents of the record, we can process it through the following code.

In general, however, we can use extension functions to encapsulate the processing of these checks, which has been achieved to simplify the code.

After the validation passes the database, the first thing we need to do is locate the current record in the record collection, convert it to a specific entity class object, and then write the new record or update the processing, as shown below.

Run the program, and in the GridView record entry, and one by one, jump to a new line, trigger the data check and save operation, save the successful prompt user, and can continue to enter new records, very convenient, this is the use of the GridView directly input data and save the convenient place.

Combined with Gridcontrol's right-click menu, we can realize the general function integration of data deletion, printing and export.

3. Data display and input based on WinForm pagination control

There is nothing wrong with using the GridView to show all the records directly in the case of less data records. However, when the data record is more, if all the records are expanded with the GridView, it will affect the performance, the better way is to make the appropriate paging to the data.

In WinForm development, we use the encapsulated paging control to display the data, the feature of this page has been integrated in the framework of WinForm development Framework, hybrid development framework, such as the underlying, directly invoke the corresponding framework business class method, can obtain the corresponding business table paging records, very convenient.

The first two subsections are based on the original GridView control object for data presentation and input, the paging control itself is also encapsulated GridView control, so it can be used directly to the data input processing. The use of pagination control, you can efficiently improve the query speed of data, in addition to the use of code generation tool DATABASE2SHARP tools to quickly develop a good WinForm interface, and then make certain changes, very convenient.

Let's take a look at the data presentation and entry interface using pagination controls.

The way to make use of the pagination control is similar to the previous way of using the GridView directly, only some details are modified.

Pagination control, when generated with the WinForm interface of the Code generation tool Database2sharp, automatically with the following pagination control initialization code, we block the new and edit processing events in it, because we use the direct input method, do not need to pop up a dialog box to deal with.

The code is shown below.

Since we use the paging control, which is automatically created when data is bound, we do not need to specify a data column for the GridView, and the code for the Initgridview after we use the paging control is shown below.

/// <summary>///Initialize GridView/// </summary>Private voidInitgridview () {varGRD = This. Wingridviewpager1.gridcontrol1; varGRV = This. Wingridviewpager1.gridview1; #regionGridView Control InitializationGRV. Initgridview (Gridtype.newitem,false, editorshowmode.mousedownfocused,""); Grv. Initnewrow+=Delegate(Objectsender, Initnewroweventargs e) {        //when the data record is initialized, set the ID and ID of the parent node        if( This. Tree. Focusednode! =NULL)        {            vari =E.rowhandle; varTypeId = This. Tree. Focusednode.getvalue ("ID"); Grv. Setrowcellvalue (i,"ID", Guid.NewGuid ().            ToString ()); Grv. Setrowcellvalue (i,"dicttype_id", typeId);//The parent ID of the store record        }    }; Grv. Cellvaluechanged+=Delegate(Objectsender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e) {        //if the value of the field changes, triggering another value change is handled here        if(E.column.fieldname = ="Name")        {            stringValue =string. Concat (GRV. Getrowcellvalue (E.rowhandle,"Value")); if(string. IsNullOrEmpty (value)) {GRV. Setrowcellvalue (E.rowhandle,"Value", E.value);    }        }    }; Grv. Validaterow+=Delegate(Objectsender, Validateroweventargs e) {             varresult = Grd. Validaterownull (E,New string[]        {            "Name"        }); //submit database after validation pass        if(Result) {//gets the Record object for the current operation            varinfo = GRV. Getfocusedrow () asDictdatainfo; //If the Record object is not empty, you can write or update            if(Info! =NULL) {result= bllfactory<dictdata>.                Instance.insertupdate (info, info.id); //If you cannot write, prompt the user                if(!result) {E.valid=false; E.errortext=string. Format ("Error writing data"); }                Else                {                    Base.                Showmessageautohide ();    }            }        }    }; #endregion}

The method of binding the data source is not much different from the directly generated code, just set the GridView to editable, as shown in the code below. Binddata

This allows us to implement a direct data entry operation based on the WinForm paging control, which is first generated using the Code generation tool Database2sharp WinForm interface, then fine-tuning the code, It is very convenient to realize this kind of fast data entry.

4, based on the master-slave table data entry processing

In the previous record preservation and display inside, are using a view of the way data display and input, if the master-slave table records at the same time input, then need to master and slave table two gridview to display and data entry, for master and slave table entry relatively complex, how to operate it?

Here we still use paging control for data paging and direct data entry operation, and the addition of master-slave table data at the same time on a Gridcontrol interface processing.

So the main table record is the dictionary type, from the table as a dictionary line item, the resulting data display interface effect is as follows.

Of course, we can directly at the bottom of the data input, including the main table record and from the table of detail records, can be one go input and save processing, the interface effect is as follows.

The master-slave relationship of GridView needs to set up the mapping relation of sets, we need to set up Gridlevelnode collection to realize the processing of master-slave table relationship.

The code that initializes the GRIDVIEW2 and master table relationships from the table is shown below

With the above initialization code, after specifying the relationship between master and slave tables, we also need to do some processing of the bound data source to display the records of master-slave table relationships on the Gridcontrol control.

First, you need to define a business object that is used to store the master-slave relationship's record object.

Then, when Binddata binds the data, the code is handled as follows.

This allows you to get started with the Master-slave table interface effect.

The data save code is similar to the previous one, and we need to process the data save operations for GridView1 and GridView2 separately, as shown in the following code.

The GRIDVIEW2 dictionary project details save operation is shown below.

Record deletion of master and slave tables here it is necessary to introduce, because the master-slave table common a right-click menu delete operation.

Then, we need to determine whether the operation from the table or the main table records, they are processed separately, and then prompt for the operation is successful, if successful, we can remove this line, to avoid re-updating the data caused by the focus loss.

The above is the introduction of various in the GridView interface directly input data and save the processing operation, although the general situation below, we recommend the content through a separate pop-up interface to display and input, but for some fields less, or like directly record users, This way is also a very good experience, but also to achieve the purpose of fast entry, can be used as my development framework data of the very rules of the input supplement.

Use the WinForm paging control in the DevExpress program to enter data directly and save

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.