DataGridView two ways to display data

Source: Internet
Author: User

1. Brief introduction

DataGridView space is the most common control for displaying data, which is highly configurable and extensible.

2. Display Data

DataGridView Display data Generally we use two methods, one is to set the Datasoure property directly to bind the data. This method does not need to write any code operation is relatively simple, but it shows the entire table of data. If the entire table has more data and we don't need all the data, we should consider the second approach. Connect the database by writing code and read the data from the database, and finally pass the returned data to DataGridView. This approach seems more complex, but it only shows the data we need, and the efficiency of the PHARE is significantly higher than the thousands of data.

Here's a quick introduction to both of these methods:

The first Pass property setting

First locate the DataSource property in the Properties window and click the drop-down box. For the first time, click Add Data source for each previous add

Select the data type, next, select the dataset, next

Select a data connection, click New Connection

In the Add Link dialog box, change the data source (the SQL Server database I used), and click the drop-down box to select the server name (the point number defaults to its own server). Then select the database at the bottom of the connection to the data, and test the link through, click OK. Then go back to the Data Configuration Wizard and click Next to select the table you want to display in the database.

When the add is complete, run the form data to show it

The second method is to load the data by writing code

<span style = "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' defines a function to find the amount to be charged within a specified date public Function Check_money (ByVal tstrecharge as Model.rechargemodel) as DataTable D        Im 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) ' Call sqlhelper return dat ' Returns the found data table End Function</span> 
<span style = "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > Dim db as New DAL. Ckeckmoneydal ' defines a D-layer object that calls the D-layer function ' to define a function that checks the data returned by the D-Layer lookup amount to the public Function Check_return (model as Model.rechargemodel) As DataTable Dim dat = Db.check_money (model) If dat. Rows.Count = 0 Then ' checks if the data table returned by the D layer has data return nothing else return dat End If End fun Ction</span> 
<span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > Dim thisrecharge as New Model.rechargemodel ' defines an entity-level object to receive the start and end time        thisrecharge.start_datetime = Datetimepicker1.text        thisrecharge.end_datetime = Datetimepicker2.text        Dim ub as New BLL. Checkmoneybll ' defines a B-layer object that is used to invoke the B-layer function        If ub.check_return (thisrecharge)            is nothing and then MsgBox ("There is no record in that time period, Please reset the time period! ", MsgBoxStyle.Exclamation, vbOKOnly)        Else            dvwtitles.datasource = Ub.check_return (thisrecharge) ' Pass the found data to DataGridView        End if</span>
Show Results:

Each of the two methods has advantages and disadvantages, we need to choose according to their actual situation. When we only need to show the data of the whole table or the data in the table is not many, the first method is the simplest one, and no need to write any code. If we want the data to have a selective display, for example, only the data from the period from June 18 to June 20 is displayed, although it seems tedious to write the code, the display results at a glance, avoiding the data we need from a large amount of data, greatly improving the efficiency.

There is no shortcut on the road to learning, but there is always one that will be more suitable for you ...

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.