It is simpler to bind data sources to DataGridView, there are two main methods:
1, directly in the control properties to bind the data source, this method is the simplest, but it is directly connected to the database, so it is almost the same as the consequences of the DataTable, so try to avoid it.
2, through the code to the Entity collection as the DataGridView data source, now with the "Add Delete User" window as an example to introduce the next
Window interface
Description: When the user level changes, the user-level user information is displayed in the table control
Edit control: Right-click the control to select "Add Column"--Add and edit columns, only three required for editing
(1) is the name of the column, this can not be changed (2) is the corresponding property name (3) in the entity is the header information, these three can also be written in the code. Assuming that the Name property is not set, the property name of the entity that is actively changed to be the data source when the data is loaded can be used when the field is removed.
U-Layer Code
Private Sub cbolevel_selectedindexchanged (sender as Object, e as EventArgs) Handles cbolevel.selectedindexchanged Dim Euserin As New E_userinfo Dim bselectuser As New B_addremoveuser Dim userlist As New List (of E_userin FO) ' Dim userbindlist as New BindingList (of E_userinfo) ' will encounter problems with BindingList as a data source, first omit Euserin.level = Cbolevel.getitemtext (Cbolevel.selecteditem) ' Gets the current text of the user-level combo box and passes it to e-layer userlist = Bselectuser.selectuserinfo (eus ErIn) ' Save the queried information in Euserstore ' hypothesis that there is no record in the collection, exit the procedure if Userlist.count = 0 Then Exit Su b End If dtguserinfo.autogeneratecolumns = False ' do not agree to actively join column Dtguserinfo.datasource = use RList ' DataGridView The generic collection as a data source ' removes the fields that do not need to be displayed DtgUserInfo.Columns.Remove ("Password") d TgUserInfo.Columns.Remove ("level") changes the title of DataGridView to Chinese and has been set in the properties of the control, which saves the ' Dtguserinfo.columns (0). HeaderText = "username"' Dtguserinfo.columns (1). HeaderText = "Name" ' Dtguserinfo.columns (4). HeaderText = "Account holder" End Sub
B-Layer Code
Imports entity.entityimports dal.dalimports system.collectionspublic Class b_addremoveuser public Function Selectuserinfo (ByVal user as E_userinfo) as IList Dim userlist As New List (of E_userinfo) Dim duser As New D_userd AO userlist = duser.selectuserinfo (user) ' Pass the Subscriber information to layer D return userlist ' return user information End Function Public Function deleteuser (ByVal user as E_userinfo) as Boolean Dim duser as New D_userdao Return duser.deleteus ER (user) ' return database delete user successful End functionend Class
D-Layer Code
public Function Selectuserinfo (ByVal user as E_userinfo) as IList Dim mysqlhelper A s New SqlHelper ' instantiation SqlHelper Dim dt As New DataTable ' defines a DataTable datasheet Dim cmdtext A s String = "Select UserID, UserName, Operator from T_userinfo Where [email protected]" Dim paras as Sqlparamet ER () = {New SqlParameter ("@Level", user. Level)} ' to run the query Dim m in sqlhelper dt = Mysqlhelper.execselect (Cmdtext, CommandType.Text, paras) ' Ylist as New List (of E_userinfo) ' defines a collection to hold the converted generic collection myList = Converthelper.converttolist (of E_userinfo) (DT) ' calls the Entity transformation class to convert the DataTable to a generic collection return MyList ' returns the Entity collection End Function
This is to be able to use DataGridView to display the information we need, such a way easy to ignore the error caused by two: 1, the database table field names and the attribute names in the entity are different. 2. The attribute name of the entity is not the same as the data name defined in DataGridView. If it is because of the 2nd kind of error, it can only be said to be too careless. Now give the 1th kind of solution, first if a scenario: Database table "username" field name is "Userno", the corresponding attribute name in the entity is "UserID", which leads to the problem of information mismatch, so need to convert, The simplest thing to do is to use as to change the query code for the D layer above
"Select Userno as UserID, UserName, Operator from t_userinfo Where [email protected]"
As to the database field conversion function is very interesting, originally wanted to use the database to demonstrate, just SQL software is not good just forget it ...
DataGridView Binding Data sources