When using the GridView to directly input data in the DevExpress program, the list selection function is added.

Source: Internet
Author: User

When using the GridView to directly input data in the DevExpress program, the list selection function is added.

In my previous article, "using Winform paging control in DevExpress program to directly input and save data" describes how to directly input data on the GridView and its encapsulated paging control, this section describes how to save and verify data, but does not involve data list selection. This method is also widely used in projects. This article will go on to explore how to directly input data on the GridView and add the dictionary selection list function.

1. Review of direct data entry in the GridView

In the integrated data entry case, we can see the convenience of direct data entry in the list, as shown below.

1) directly input and save on the GridView

2) Direct Entry and Storage Based on WInform paging controls

In this way, the data is verified and saved on the Griview.

After the verification is passed, the database is submitted. The first method we need to do is to locate the current record in the record set, convert it to a specific object class object, and then write the new record or update the processing, as shown below.

 

2. Select input from the drop-down list based on data dictionary

The following content is used as a supplement to direct data input. It provides a drop-down list Input Method Based on Data dictionary.

First, let's take a look at the overall effect, and then analyze the mysteries step by step.

For example, you can select a gender.

And a drop-down list that can be searched

And display and processing of data in multiple selection boxes

Or the implementation of the button selection dialog box

These operations provide a variety of options for list input and enrich the user input mode.

So how can we implement these functions based on the GridView?

First, we will build a database table based on the model. The database table design is as follows.

Then, the bottom-Layer Code of the Framework is generated based on the Database2Sharp code generation tool, and the WInform interface code is generated. The bound data part of the generated interface code is as follows.

/// <Summary> /// bind list data /// </summary> private void BindData () {// entity this. winGridViewPager1.DisplayColumns = "Name, Sex, Nationality, BirthDate, Height, Weight, City, Area, State, Favorites, Introduction, Creator, CreateTime"; this. winGridViewPager1.ColumnNameAlias = BLLFactory <Test>. instance. getColumnNameAlias (); // field Column Display name escape string where = GetConditionSql (); var list = BLLFactory <Test>. instance. findWithPager (where, this. winGridViewPager1.PagerInfo); this. winGridViewPager1.DataSource = new WHC. pager. winControl. sortableBindingList <TestInfo> (list); this. winGridViewPager1.PrintTitle = "Personnel Test Information Report ";}

To add the corresponding direct input method, we need to set the dictionary binding. The processing method is as follows. We use the SetRepositoryItems function to encapsulate the required processing.

/// <Summary> /// set the drop-down category content corresponding to GridControl to facilitate escape // </summary> private void SetRepositoryItems (GridView gridview) {var sexList = new List <CListItem> () {new CListItem ("male", "1"), new CListItem ("female", "2 "), new CListItem ("unknown", "0")}; gridview. columns. columnByFieldName ("Sex "). createLookUpEdit (). bindDictItems (sexList, false); gridview. columns. columnByFieldName ("City "). createLookUpEdit (). bindDictItems ("city"); gridview. columns. columnByFieldName ("Nationality "). createSearchLookUpEdit (). bindDictItems ("ethnic"); gridview. columns. columnByFieldName ("Area "). createLookUpEdit (). bindDictItems ("market partition"); gridview. columns. columnByFieldName ("State "). createLookUpEdit (). bindDictItems ("processing status"); gridview. columns. columnByFieldName ("Favorites "). createCheckedComboBoxEdit (). bindDictItems ("interests"); gridview. columns. columnByFieldName ("Introduction "). createMemoEdit (); gridview. columns. columnByFieldName ("Creator "). createButtonEdit (). buttonClick + = (object sender, ButtonPressedEventArgs e) => {FrmSelectCustomer dlg = new FrmSelectCustomer (); if (dlg. showDialog () = System. windows. forms. dialogResult. OK) {if (gridview. getFocusedRow () = null) {gridview. addNewRow (); // Add a row if the first time} gridview. setFocusedRowCellValue ("Creator", dlg. customerName) ;}}; gridview. optionsBehavior. readOnly = false; gridview. optionsBehavior. editable = true ;}

Then add the method call in the BindData function above.

SetRepositoryItems(this.winGridViewPager1.gridView1);

The CreateLookUpEdit, CreateSearchLookUpEdit, CreateCheckedComboBoxEdit, CreateButtonEdit, and other methods are the content display controls at the underlying layer of the framework. To facilitate direct use as extension functions, the rules are similar

The code is similar to the following processing method.

/// <Summary> /// edit the column of the created GridView as SearchLookUpEdit /// </summary> /// <param name = "gridColumn"> GridColumn column object </param >/// <returns> </returns> public static RepositoryItemSearchLookUpEdit CreateSearchLookUpEdit (this GridColumn gridColumn) {RepositoryItemSearchLookUpEdit repositoryItem = new RepositoryItemSearchLookUpEdit {AutoHeight = false, NullText = ""}; gridColumn. view. gridControl. repositoryItems. add (repositoryItem); gridColumn. columnEdit = repositoryItem; return repositoryItem ;}

 

Of course, we also need to register the Response Processing Event, the Code is as follows.

Private void RegisterEvent () {var grd = this. winGridViewPager1.gridControl1; var grv = this. winGridViewPager1.GridView1; grv. initGridView (GridType. newItem, false); # The region list handles the grv event. initNewRow + = delegate (object sender, InitNewRowEventArgs e) {GridView = grd. focusedView as GridView; gridView. setFocusedRowCellValue ("ID", Guid. newGuid (). toString (); gridView. setFocusedRowCellValue ("Creator ", LoginUserInfo. name); gridView. setFocusedRowCellValue ("CreateTime", DateTime. now) ;}; grv. validateRow + = delegate (object sender, ValidateRowEventArgs e) {var result = grd. validateRowNull (e, new string [] {"Name"}); // submit the database GridView = grd after the verification is passed. focusedView as GridView; if (result) {var newInfo = grv. getFocusedRow () as TestInfo; if (newInfo! = Null) {result = BLLFactory <Test>. Instance. InsertUpdate (newInfo, newInfo. ID); if (! Result) {e. valid = false; e. errorText = string. format ("Data writing error");} else {base. showMessageAutoHide () ;}}}; # endregion}

Then, when the form is initialized, you can call the above registration event.

/// <Summary> // personnel test information /// </summary> public partial class FrmTest: BaseDock {public FrmTest () {InitializeComponent (); InitDictItem (); this. winGridViewPager1.OnPageChanged + = new EventHandler (winGridViewPager1_OnPageChanged); this. winGridViewPager1.OnStartExport + = new EventHandler (winGridViewPager1_OnStartExport); this. winGridViewPager1.OnEditSelected + = new EventHandler (winGridViewPager1_OnEditSelected); this. winGridViewPager1.OnAddNew + = new EventHandler (winGridViewPager1_OnAddNew); this. winGridViewPager1.OnDeleteSelected + = new EventHandler (winGridViewPager1_OnDeleteSelected); this. winGridViewPager1.OnRefresh + = new EventHandler (winGridViewPager1_OnRefresh); this. winGridViewPager1.AppendedMenu = this. contextMenuStrip1; this. winGridViewPager1.ShowLineNumber = true; this. winGridViewPager1.BestFitColumnWith = false; // whether to set to auto-adjust the width. If false, this is not set. winGridViewPager1.gridView1. performancechanged + = new EventHandler (gridview1_performancechanged); this. winGridViewPager1.gridView1. customColumnDisplayText + = new DevExpress. xtraGrid. views. base. customColumnDisplayTextEventHandler (gridView1_CustomColumnDisplayText); this. winGridViewPager1.gridView1. rowCellStyle + = new DevExpress. xtraGrid. views. grid. rowCellStyleEventHandler (gridView1_RowCellStyle); // associate the carriage return key to query foreach (Control control in this. layoutControl1.Controls) {control. keyUp + = new System. windows. forms. keyEventHandler (this. searchControl_KeyUp);} // register the corresponding GridView Processing EventRegisterEvent ();}

When the data source changes, we set the width of each column so that the content can be displayed properly.

/// <Summary> /// after data is bound, the width of each column is allocated. /// </summary> private void gridview1_performancechanged (object sender, EventArgs e) {if (this. winGridViewPager1.gridView1. columns. count> 0 & this. winGridViewPager1.gridView1. rowCount> 0) {// set 100 width foreach (DevExpress. xtraGrid. columns. gridColumn column in this. winGridViewPager1.gridView1. columns) {column. width = 100;} // Name, Sex, BirthDate, Height, Weight, City, Area, State, Favorites, Introduction, Creator, createTime // special SetGridColumWidth ("BirthDate", 120); SetGridColumWidth ("CreateTime", 120); SetGridColumWidth ("Introduction", 200 ); setGridColumWidth ("Favorites", 200 );}}

In this way, the implementation of direct data entry processing is completed based on the development framework, which is very convenient. Of course, if no encapsulated GridView is used directly, there are basically not many changes, the same idea.

Related Article

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.