How to display the queried data in DataGridView

Source: Internet
Author: User

Background introduction:

The T_line_info table in the database holds the records of the students on the machine, that is, we need to query the computer record of the table, where the specific content is:




The interface is designed as follows:


Right-click the DataGridView Control, select Edit column, and design its column name.


Code Implementation section:

U-Layer:

Public Class Frmqueryonrecord Private Sub btn_query_click (sender as Object, e as EventArgs) Handles Btn_query.click Dim student As New Login.Model.LineInfo Dim buser As New Login.BLL.LoginManager ' pass input card number to entity Stude Nt.cardno = Txtcardno. Text datagridview1.visible = True ' in the database to find the card number of the machine information Dim utable = Buser.queryonrecord (student) I F uTable.Rows.Count > 0 Then ' Show data in DataGridView Datagridview1.item (1, Datagridview1.newrowindex ). Value = utable.rows (0) ("Studentname") Datagridview1.item (2, Datagridview1.newrowindex). Value = utable.rows (0) ("Ondate") Datagridview1.item (3, Datagridview1.newrowindex). Value = utable.rows (0) ("OnTime") Datagridview1.item (4, Datagridview1.newrowindex). Value = utable.rows (0) ("Offdate") Datagridview1.item (5, Datagridview1.newrowindex). Value = utable.rows (0) ("OnTime") Datagridview1.item (6, Datagridview1.newrowindex). Value = utable.roWS (0) ("Moeny") Datagridview1.item (7, Datagridview1.newrowindex). Value = utable.rows (0) ("Consumemoney") Else ' did not find content when DataGridView1.Rows.Clear () Ms Gbox ("No record found!") ") End If End SubEnd Class



B Layer:

   Public Function Queryonrecord (student as Login.Model.LineInfo) as DataTable        Dim f As New dfactory.dbfactory        Dim i Student as Login.Interface.ILineInfo        istudent = F.queryonlineinfo        ' receive datatable        Table = istudent. Query_onrecord (student)        ' return table to U-layer return        table    End Function

Interface:

    the type returned by public Interface Ilineinfo ' is a DataTable    Function Query_onrecord (student as Login.Model.LineInfo) as Datatableend Interface

Entity:

Public Class lineinfo    ' and field one by one in the Machine record table corresponds to public    Cardno as String public studentname as    string    public Ondate As String public    OnTime As String public offdate as String public offtime as    string    public money As String public    Consumemoeny as Stringend Class


The Getdatatable method in SqlHelper

 Public Function getdatatable (ByVal cmdtxt as String, ByVal Cmdtype as CommandType, ByVal paras as SqlParameter ()) as Data Table Dim strconnstr as String = System.Configuration.ConfigurationManager.AppSettings ("connstr") ' defines a database connection          Object Dim conn As SqlConnection = new SqlConnection (strconnstr) Dim cmd as New SqlCommand ' Define a Command object        Dim adataset As DataSet Dim adaptor As SqlDataAdapter ' defines an adapter object cmd = New SqlCommand (cmdtxt, conn) adaptor = new SqlDataAdapter (cmd) adataset = new DataSet Cmd.commandtype = Cmdtype cmd. Parameters.addrange (paras) Try ' Open database connection If Conn. State = Connectionstate.closed Then Conn. Open () End If ' fills the dataset adaptor. Fill (Adataset) Return Adataset. Tables (0) Catch ex as Exception MsgBox (ex. Message, "database operation") Finally ' closes the database connection If Conn. State = Connectionstate.opEN then Conn. Close () End If end Try End Function


Layer D:

Public Function Queryonrecord (student as Login.Model.LineInfo) as DataTable Implements Ilineinfo.query_onrecord        ' Look for        Dim sql As String = "SELECT * from T_line_info where cardno=" & Student.cardno        Dim cmd As Sqlcomm in the Machine record table by card number  and = new SqlCommand (SQL, conn)        Dim helper As New Login.DAL.sqlHelper        ' Set parameter        Dim paras As SqlParameter () = {New SqlParameter ("@cardno", Student.cardno)}        Try            conn. Open ()            ' calls the Getdatatable method in SqlHelper to            Return Helper. Getdatatable (SQL, CommandType.Text, paras)               Catch ex as Exception            MsgBox (ex. Message, "")        end Try    End Function

Operation Result:



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.