How to display data in datagridview: 1. Find the "DataSource" property in the property window and select "Dataset"; 2. Select "Data Connection" and click "New Connection"; 3. Join the link dialog Change the data source in the box; 4. Select the database in the data field, and then click "OK".
The operating environment of this article: Windows 7 system, DataGridView control, Dell G3 computer.
Methods for datagridview to display data:
The first method is to set the data through properties
First find the DataSource property in the property window , click the drop-down box. This is the first time to use it. If you have added it before, click to add the data source
Select the data type, next step, select the data set, next step
Select the data connection and click New Connection
Change the data source (the SQL Server database I use) in the Join Link dialog box and click the drop-down Select the server name in the box (the dot defaults to your own server). Then select the database under Connect to Data. If the test link passes, click OK. At this time, you return to the data configuration wizard. Click Next to select the table you want to display in the database.
After the addition is completed, the execution window data will be displayed
Another method is to pass Write code to load data
<span style="font-family:KaiTi_GB2312;font-size:18px;"> '定义一个函数查找规定日期内的收取金额 Public Function check_money(ByVal tstRecharge As Model.RechargeModel) As DataTable Dim strSQL As String = "select * from Recharge_info where rechargeDateTime between @startdatetime and @enddatetime" Dim params As SqlParameter() = {New SqlParameter("@startdatetime", tstRecharge.start_datetime), New SqlParameter("@enddatetime", tstRecharge.end_datetime)} Dim helper As New SqlHelper Dim dat = helper.GetDataTable(strSQL, CommandType.Text, params) '调用sqlhelper Return dat '返回查到的数据表 End Function</span> <span style="font-family:KaiTi_GB2312;font-size:18px;"> Dim db As New DAL.CkeckMoneyDAL '定义一个D层对象,用来调用D层函数 '定义一个函数,用来检查D层查找金额返回的数据 Public Function check_return(model As Model.RechargeModel) As DataTable Dim dat = db.check_money(model) If dat.Rows.Count = 0 Then '检查D层返回的数据表中是否有数据 Return Nothing Else Return dat End If End Function</span> <span style="font-family:KaiTi_GB2312;font-size:18px;"> Dim thisRecharge As New Model.RechargeModel '定义一个实体层对象,用来接收起止时间 thisRecharge.start_datetime = DateTimePicker1.Text thisRecharge.end_datetime = DateTimePicker2.Text Dim ub As New BLL.CheckMoneyBLL '定义一个B层对象,用来调用B层函数 If ub.check_return(thisRecharge) Is Nothing Then MsgBox("该时间段内没有记录,请又一次设置时间段!", MsgBoxStyle.Exclamation, vbOKOnly) Else dvwTitles.DataSource = ub.check_return(thisRecharge) '把查到的数据传给datagridview End If</span>
Display results:
Both methods have their own advantages and disadvantages. We need to choose according to our actual situation. . When we only need to display the data of the entire table or there is not much data in the table, the first method is the easiest and does not require writing any code. Suppose we want the data to be displayed selectively. For example, the picture above only displays the data from June 18th to June 20th. Although it seems cumbersome to write code, the display results are clear at a glance, which prevents us from searching for a large amount of data. The data we need greatly improves efficiency.
Related free learning recommendations: php programming (video)
The above is the detailed content of How datagridview displays data. For more information, please follow other related articles on the PHP Chinese website!