You can use the datagridview control to display and edit table data from different data sources.
At this point, I have used the following two methods. Personal feeling,Method 1: Suitable for three layers andCodeHighly familiarProgramEmployee; Method 2: it is more suitable for future programmers who are currently initially engaged in three layers of learning. Of course, both methods are required. It is best to select different methods for different stages or scenarios.
Method 1: bind a data source to the dview
1 .---Add project data source
After that, the data source is bound. By default, the following code is automatically generated under the loading event of this form:
Private subfrmqueryrechargemoney_load (byval sender as system. Object, byval e assystem. eventargs) handles mybase. Load 'todo: This line of code loads data to the table "charge_personaldataset2.t_onpcrecord. You can move or delete it as needed. Me. t_onpcrecordtableadapter.fill (Me. charge_personaldataset2.t_onpcrecord) end sub
This code implements--That is, all information in our database table is displayed inDatagridview. Of course, we can also modify the position of this Code, for example, put it inButtonOfClickEvent. This depends on the situation.
2 .---Edit Columns
Generally, after adding the data source,DatagridviewThe column names displayed in the control are in English, that is, the field names in your database (generally, the field names in our database are in English), so,We need to modifyHeadertextAttribute to the column name that we need to display on the interface.
3 .---Add Query
In most cases, we perform queries under certain conditions, and all the queries implemented in the above steps, so we need to add certain query conditions here, that isSQLStatement. For example:
After "add" is determined, the form will appearFillby1toolstripbuttonControl for us to query:
At this time,Fillby1toolstripbuttonOfClickEvent, the following code is automatically generated:
Marked in the figure,They can all be modified with us.,We can automatically generateToolstripbuttonInstead, use the text box and the "query" button to enter the card number.
So farDatagridviewBind the data source and display the data.
Method 2: code implementation
OnlyULayer Code, becauseDatagridviewThe code is onlyULayer appears.
'Query the received amount record strstarttime = dtpstarttime. text strendtime = dtpendtime. text dtqueryreturnmoney = bqueryreturnmoney. bqueryremovemoney (strstarttime, strendtime) If dtqueryreturnmoney. rows. count = 0 then msgbox ("no charge amount record for this time period! ", Vbokonly," prompt ") Exit sub else 'sets the data source dgvreturnmoney bound to the dview control. datasource = dtqueryreturnmoney change the title of the dview to Chinese dgvreturnmoney. columns (0 ). headertext = "card number" dgvreturnmoney. columns (1 ). headertext = "refund amount" dgvreturnmoney. columns (2 ). headertext = "return time" dgvreturnmoney. columns (3 ). headertext = "checkout instructor" dgvreturnmoney. columns (4 ). headertext = "Check status" 'dview dview dgdgdgdgdgdgdgdgdgdgdgdg. columns (0 ). datapropertyname = dtqueryreturnmoney. columns (0 ). columnname dgvreturnmoney. columns (1 ). datapropertyname = dtqueryreturnmoney. columns (1 ). columnname dgvreturnmoney. columns (2 ). datapropertyname = dtqueryreturnmoney. columns (2 ). columnname dgvreturnmoney. columns (3 ). datapropertyname = dtqueryreturnmoney. columns (3 ). columnname dgvreturnmoney. columns (4 ). datapropertyname = dtqueryreturnmoney. columns (4 ). columnname
These codes are also implemented for usDatagridview. Maybe the reader may have doubts. Shouldn't method 2 be simpler as we have written above? In fact, the method is medium. Due to the display requirements, I only postedULayer Code,BLayer andDThe code at the layer still needs to be manually typed, which is the same as the code at most three layers. In method 1, you only need to follow the above steps, and no code is required for other layers.
As for the method, the three layers will not be broken. I am considering it now, we look forward to your guidance ~