简体   繁体   English

SQL Server CE数据库中的数据输入在运行时未显示在dgv中

[英]Data entry into SQL Server CE database not displaying in dgv during runtime

I have a SQL Server CE database with a table name records and 22 columns and in my form in vb.net I have 19 textboxes and 3 datetimepickers for each of the respective textboxes and also a bound dgv, which I dragged from the query of my tables. 我有一个带有表名称记录和22列的SQL Server CE数据库,在vb.net中的表单中,我有19个文本框和3个datetimepickers(分别用于每个文本框)以及绑定的dgv,我从查询中将其拖动到表。

When I run the program and input data into the textboxes and click save it save with the msg "record saved" as programmed but nothing displays in my dgv 当我运行该程序并将数据输入到文本框中,然后单击“保存”时,将其保存为已编程的味精“记录已保存”,但在dgv中没有任何显示

Here's my code: 这是我的代码:

Imports System
Imports System.Data
Imports System.Data.SqlServerCe

Public Class Form1
    Dim Cmd As SqlCeCommand
    Dim con As SqlCeConnection
    Dim affector As Integer

    Dim ConnectionString As String = ("Data Source=C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet.PATIENT_RECORD' table. You can move, or remove it, as needed.'
    Dim strconnection As String = ("Data Source=C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")

 Try
        con = New SqlServerCe.SqlCeConnection("strconnection")
        Dim sql As SqlServerCe.SqlCeCommand = New SqlServerCe.SqlCeCommand("Query", con)
        con.Open()
        If con.State = ConnectionState.Closed Then
            MessageBox.Show("Database has failed to connect. System will now terminate.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End
        Else 
            Exit Sub
        End If
    Catch
        MsgBox("Connection Established")
    End Try
End Sub

'save button'
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSave.Click
    Try
        con = New SqlCeConnection(ConnectionString)
        con.Open()
        sCommand = New SqlCeCommand("INSERT INTO RECORD([Patient_name],[Patient_Address],[Patient_Date_Of_Birth],[Patient_Phone Number],[Patient_CardNO],[Blood_Group],[Date_Admitted],[Date_Discharged],[Treatment1],[Treatment2],[Treatment3],[Treatment4],[Treatment5],[Treatment6],[Price_Unit1],[Price_Unit2],[Price_Unit3],[Price_Unit4],[Price_Unit5],[Price_Unit6],[Sub_total],[VAT],[Prev_bal],[Net_Total]) values ('" + Patient_AddressTextBox.Text + "','" + Patient_AddressTextBox.Text + "','" + Patient_Date_Of_BirthDateTimePicker.Value.Date + "','" + Phone_NumberTextBox.Text + "','" + Hospital_NumberTextBox.Text + "','" + Blood_Group_TextBox.Text + "','" + Date_AdmittedDateTimePicker.Value.Date + "','" + Date_DischargedDateTimePicker.Value.Date + "','" + Treatment1TextBox.Text + "','" + Treatment2TextBox.Text + "','" + Treatment3TextBox.Text + "','" + Treatment4TextBox.Text + "','" + Treatment5TextBox.Text + "','" + Treatment6TextBox.Text + "','" + Sub_TotalTextBox.Text + "','" + VATTextBox.Text + "','" + Previous_BalTextBox.Text + "','" + Net_TotalTextBox.Text + "',)", con)
        affector = sCommand.ExecuteNonQuery
    Catch
        MessageBox.Show("New record has been saved.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Try
End Sub

 Private Sub BtnRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRecord.Click
    strcon = ("Data Source= C:\Users\chinedu\documents\visual studio 2010\Projects\GUU SPECIALIST HOSPITAL 2\GUU SPECIALIST HOSPITAL 2\Database1.sdf")
    con = New SqlCeConnection(strcon)
    Dim query As String = " SELECT * from Records"
    Dim adpt As New SqlCeDataAdapter(query, con)
    Dim ds As New Database1DataSet()
    adpt.Fill(ds, "Records")
    RecordsDataGridView.DataSource = ds.Tables(0)

You need to DataBind() your GridView 您需要DataBind()您的GridView

In Your BtnRecord_Click event should be like this 在您的BtnRecord_Click事件中应该是这样的

 RecordsDataGridView.DataSource = ds.Tables(0)
 RecordsDataGridView.DataBind() `This is your missing line.

Also you can make it to load automatically after a new record is added. 您还可以使其在添加新记录后自动加载。 Just add those 2 lines to your Button4_Click event 只需将这两行添加到Button4_Click事件中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM