DataGridView Binding Data sources

Source: Internet
Author: User

It is simpler to bind data sources to DataGridView, there are two main methods:

1. Bind the data source directly in the control's properties. This is the simplest approach, but it is directly connected to the database. This is almost the same as the consequences of a 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 "Join column"--Make column join and edit. There are only three editors that need to use

(1) is the column name. This can be changed. (2) The corresponding attribute name (3) in the entity is the header information, and 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. Omit euserin.level = Cbolevel.getitemtext (Cbolevel.selecteditem) ' to get the current text of the user-level combo box and pass it to e-layer userlist = Bselec  Tuser.selectuserinfo (Euserin) ' Save the queried information in Euserstore ' if there is no record in the collection, exit the procedure if Userlist.count = 0 Then Exit Sub End If dtguserinfo.autogeneratecolumns = False ' do not agree to voluntarily join the column Dtgu Serinfo.datasource = UserList ' Removes a field that does not need to be displayed as a data source for a generic collection DataGridView DtgUserInfo.Columns.Rem Ove ("Password") DtgUserInfo.Columns.Remove ("level") changes the title of the DataGridView to Chinese and has been set in the control's properties, saving the ' Dtguse Rinfo.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 as New SqlHelper        ' instantiation sqlhelper< C3/>dim DT as New DataTable                 ' defines a DataTable datasheet        Dim cmdtext as String = "Select UserID, UserName, Operator from T_us Erinfo Where [email protected] "        Dim paras as SqlParameter () = {New SqlParameter (" @Level ", user. Level)}  ' to           run the query Dim by passing in sqlhelper dt = Mysqlhelper.execselect (Cmdtext, CommandType.Text, paras) '        MyList as New List (of E_userinfo)                       ' defines a collection to hold the converted generic collection        myList = Converthelper.converttolist (for E_userinfo) ( DT)     ' Call 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 this is due to the 2nd type of error. It's just too careless to say it. Now give the 1th kind of solution, first if a scenario: the 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 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 lev[email protected]"

As to the conversion of database fields is interesting. Originally wanted to use the database to demonstrate. Just the SQL software is not good to forget it ...

DataGridView Binding Data sources

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.