Home > Common Problem > How datagridview displays data

How datagridview displays data

coldplay.xixi
Release: 2023-02-07 15:39:56
Original
6082 people have browsed it

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".

How datagridview displays data

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

How datagridview displays data

Select the data type, next step, select the data set, next step

How datagridview displays data

Select the data connection and click New Connection

How datagridview displays data

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.

How datagridview displays data

After the addition is completed, the execution window data will be displayed

How datagridview displays data

Another method is to pass Write code to load data

<span style="font-family:KaiTi_GB2312;font-size:18px;">    &#39;定义一个函数查找规定日期内的收取金额
    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) &#39;调用sqlhelper
        Return dat &#39;返回查到的数据表
    End Function</span>
<span style="font-family:KaiTi_GB2312;font-size:18px;"> Dim db As New DAL.CkeckMoneyDAL &#39;定义一个D层对象,用来调用D层函数
    &#39;定义一个函数,用来检查D层查找金额返回的数据
    Public Function check_return(model As Model.RechargeModel) As DataTable
        Dim dat = db.check_money(model)
        If dat.Rows.Count = 0 Then &#39;检查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 &#39;定义一个实体层对象,用来接收起止时间
        thisRecharge.start_datetime = DateTimePicker1.Text
        thisRecharge.end_datetime = DateTimePicker2.Text
        Dim ub As New BLL.CheckMoneyBLL &#39;定义一个B层对象,用来调用B层函数
        If ub.check_return(thisRecharge) Is Nothing Then
            MsgBox("该时间段内没有记录,请又一次设置时间段!", MsgBoxStyle.Exclamation, vbOKOnly)
        Else
            dvwTitles.DataSource = ub.check_return(thisRecharge) &#39;把查到的数据传给datagridview
        End If</span>
Copy after login

Display results:

How datagridview displays data

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template