How to use DataGridView to bind data source to display data in C#

1. Build a form, add DataGridView control on the form, select the small triangle in the upper right corner of the control

2. Click to select data source-add project data source 

 

3. Select the data source type, here I am using the database

4. Select the database model, which only shows a data set, select the data set, and then the next step

5. Click New Connection

6. Select the data source, I use sql server

7. Add a connection, select your server name, enter the user name and password, select the database name, click test connection, the test is successful

 

 8. Select the database object, where I need to query the computer information, check the Line_Info table (Note: If the DataGridView only needs to display certain columns in the table, check the specific fields in this step)

Then it appears as follows

9. If the column name is not what you want, you can modify it yourself: click the small triangle-edit column

10. Modify the column name here in the HeaderText property

11. Next, you need to add a query, the query name can be modified by yourself, and then click the query builder to proceed to the next step

12. First select the column you need, then write the sql statement below, and finally click to execute the query, and the second picture below will come out. Here I want to query the computer record with the card number 1, and enter 1 in the query parameter And click OK

The following query results appear

 At this point, we have finished using the DataGridView to bind the data source and display the data, and then the following line will be added above the DataGridView control of the form. At this time, we enter the card number and click on the query, and the data we need can be displayed in the DataGridView. Out. (The code will be automatically generated)

DataGridView does not perform data source binding, and directly uses code to display the query data in the DataGridView table:

https://blog.csdn.net/wtt15100/article/details/107015583

Guess you like

Origin blog.csdn.net/wtt15100/article/details/106985457
Recommended