Recently, some netizens asked me how to implement batch modification of CYQ. Data, so I spent some time writing the following example:
The example is a Winform application. After the contents of the cells in the DataGridView control are updated automatically to the database.
First, the final result graph is shown as follows:
The next project solution diagram is as follows:
Because it is a Demo, I generally choose to use a text database as an example.
I created a new Users. ts with the following content:
ID, int; UserName, string; CreateTime, datetime;
The table name is "Users" and the field is "ID, UserName, CreateTime ".
PS: Text Database. It can be used in the CodeFirst entity class or in the traditional enumeration mode. previous articles used the CodeFirst mode for demonstration. Here is an enumeration demonstration.
The corresponding standard enumeration file TableNames. cs contains the following content:
/// <Summary> /// Brief description of TableNames /// </summary> public enum TableNames {Users,} public enum Users {ID, UserName, CreateTime}
Because it is just an example, you can use the tool that comes with V5 to generate it, or you can create your own handwriting.
Next, let's take a look at some of the Code:
First, define a global MDataTable field: public MDataTable table;
Next, write a Load function. When the page is loaded, the data is displayed and then bound to the list control:
Private void LoadData () {using (MAction action = new MAction (TableNames. Users) {table = action. Select (); table. Bind (gvUsers );}}
FormLoad code:
Private void Form1_Load (object sender, EventArgs e) {LoadData (); if (table. Rows. Count <100) {// Add data in batches. // Add rows to the table in batches. MDataRow row = null; for (int I = 0; I <100; I ++) {row = table. newRow (); row. set (Users. userName, "User:" + DateTime. now. millisecond); row. set (Users. createTime, DateTime. now); table. rows. add (row);} // call the batch addition of tables. Table. AcceptChanges (AcceptOp. Insert); // rebind to the control. Table. Bind (gvUsers );}}
Bind data first, so that the table is not Null. If there is no data at the beginning, the table also has a data structure.
If there are less than 100 rows of data, the following lines of code are inserted in batches and then re-bound to the control.
How to Perform Volume update?
Private void gvUsers_CellValueChanged (object sender, DataGridViewCellEventArgs e) {if (table. acceptChanges (AcceptOp. update) {index ++; labTip. text = "the data has been updated to the database... "+ index;} else {labTip. text = "data update failed ";}}
The method is very simple. You only need to call the cell column value to change the state.
Of course, because it is batch, you can also put the code in a button and click the button to confirm the update.
In theory, the volume update ends here. Here, we need to add more content, that is, the Chinese display of the column:Generally, after the query, the column names are all in English, but the system mostly displays Chinese characters. Here we provide a solution: private void gvUsers_DataBindingComplete (object sender, DataGridViewBindingCompleteEventArgs e) {FieldKeyValue. formatHeaderText (gvUsers );}
We can translate the column header in the DataBindingComplete event of the control.
Here I encapsulate it into a FieldKeyValue class. The Code is as follows: public class FieldKeyValue {public static void FormatHeaderText (DataGridView gv) {for (int I = 0; I <gv. columns. count; I ++) {gv. columns [I]. headerText = GetName (gv. columns [I]. headerText );}
} Public static string GetName (string key) {return GetName (key, null);} public static string GetName (string key, string tableName) {Dictionary <string, string> dictionary = GetFieldDescriptionDictionary (); if (dictionary! = Null) {if (tableName! = Null & dictionary. containsKey (tableName + "_" + key) {return dictionary [tableName + "_" + key];} else if (dictionary. containsKey (key) {return dictionary [key] ;}} return key ;}private static Dictionary <string, string> _ Dic = null; private static Dictionary <string, string> GetFieldDescriptionDictionary () {if (_ Dic = null) {_ Dic = new Dictionary <string, string> (); # Add _ Dic to the region Dictionary. add ("ID", "Number"); _ Dic. add ("UserName", "UserName"); _ Dic. add ("CreateTime", "creation date"); # endregion} return _ Dic ;}
You only need to add the dictionary correspondence. Because sometimes different table names and fields may have different translations, the dictionary supports:
_ Dic. Add ("TableA_ID", "Number"); _ Dic. Add ("TableB_ID", "ID ");
This class will give priority to the query "Table name_field name". If it cannot be found, it will find "field name ".
Finally, download the Demo project source code: CYQ.Data_V5_Test_Win.rar
For Winform paging, you can download my Winform paging control: C # Winform General paging control (source code download is provided)