in fact, just started knocking at the computer room, just used to DataGridView time, always appear to be powerless, first to learn, read a lot of information, but according to write, I do not know where to start, so, when they use query SQL statement, and DataGridView display, this function can not be realized, but here to everyone to write, if there is any bad will, please point out:
First edition:
Window design:
The required controls:
DateTimePicker, datasets, DataGridView
Three steps to grasp:
The first step: Use the view to create a view of the results you want (assuming that a table can be solved, you can just use one query).
Second Step: Query view returns DataTable
The third step: directly let the DataSource of the GridView is equal to the data in the DataTable.
Detailed implementation method:
Imports System.Data.SqlClientImports System.Windows.FormsPublic Class frmopergetcharge Private Sub Btnqurry_click ( ByVal sender as Object, e as EventArgs) Handles Btnqurry. Click Dim start_datetime As String is defined as string because the field in the SQL table is a string type Dim End_datetime As String start_d Atetime = convert.tostring (Datetimepicker1.value + "00:00:00") ' is set to a string type can also be directly compared, plus the subsequent 00:00:00 is due to the data in the database when there is time En D_datetime = convert.tostring (datetimepicker2.value) ' receives data from DateTimePicker Dim strconnection as String = "server=m X Database=reconsitutioncharge_sys; User Id=sa; password=123456 "Dim sqlConnection1 As New SqlConnection (strconnection) Dim dataAdapter As New Sqldataadapte R Dim Dst As New DataSet ' stored in dataset Dim DT as new DataTable ' Receive query result Sqlconnection1.open () Di M SQL as String = "SELECT * from T_recharge_dal Where @Start_dateTime <= datetime and datetime <= @End_dateTime" Dim cmd as SqlCommand = New SqlCommand (sqL, SqlConnection1) cmd. Parameters.Add (New SqlParameter ("@Start_dateTime", Start_datetime)) ' Assign value, and pass cmd. Parameters.Add (New SqlParameter ("@End_dateTime", End_datetime)) ' Assignment, transfer dataadapter.selectcom Mand = cmd DataAdapter.Fill (DST, "recharge") ' passes the data in the DataAdapter to the DataSet dt = DST. Tables ("Recharge") returns the data in the dataset to the DataTable Sqlconnection1.close () Datagridview1.autogeneratecolumns = True ' Proactively create columns Datagridview1.datasource = DT ' data source End subend Class
In fact, this should also be done with three layers.