The Helper House (www.bkjia.com) Tutorial saw a test comment on data binding in the CodeProject. This comment has been recognized by the MVP and I am very surprised by this result. It seems that, the habit of writing code will be slightly changed in the future.
First, the test code is provided:
Reference content is as follows: Public void initList (DataTable dt) { DropDownList1.DataSource = dt; DropDownList1.DataTextField = "empName "; DropDownList1.DataValueField = "empNumber "; DropDownList1.DataBind (); }
Public void initList2 (DataTable dt) { Foreach (DataRow r in dt. Rows) { DropDownList2.Items. Add (new ListItem (r ["empName"]. ToString (), r ["empNumber"]. ToString ())); } }
Public void initList3 (DataTable dt) { Foreach (DataRow r in dt. Rows) { DropDownList3.Items. Add (new ListItem (r [0]. ToString (), r [1]. ToString ())); } } |
The efficiency of initList is more than 10 times lower than that of initList2 and initList3 (It's terrible. I haven't tested it carefully, but it's probably a bit exaggerated). initList2 and initList3 are almost the same in efficiency, however, the efficiency of initList3 is higher than that of initList2, because the code using the column number does not need to be converted to the corresponding subscript when bound, and the data in the associated data source does not need to be converted to the corresponding subscript. also, Cache is used when column numbers are used, but not every time. therefore, data binding using initList3 is the most efficient.