Sql server MonthCalendar日期更改事件

Sql server MonthCalendar日期更改事件,sql-server,vb.net,entity-framework,datagridview,Sql Server,Vb.net,Entity Framework,Datagridview,我有一个项目用于保存特定日期的患者预约。我的项目拥有一个与sqlserver数据库链接的datagridview。该网格包含一个名为“时间”的列。我在sqlserver中分配了此列的值。当我输入表单时,datagridview包含10个空行,因此用户可以在特定时间保存患者。该表单还包含一个monthcalendar。我想要的是显示每个monthcalendar日期的空行网格,以便在不同日期保存约会 我在下面看到了这个,但他使用了数据集,我正在使用实体模型。我不知道如何实现它,请帮助我 Publi

我有一个项目用于保存特定日期的患者预约。我的项目拥有一个与sqlserver数据库链接的datagridview。该网格包含一个名为“时间”的列。我在sqlserver中分配了此列的值。当我输入表单时,datagridview包含10个空行,因此用户可以在特定时间保存患者。该表单还包含一个monthcalendar。我想要的是显示每个monthcalendar日期的空行网格,以便在不同日期保存约会

我在下面看到了这个,但他使用了数据集,我正在使用实体模型。我不知道如何实现它,请帮助我

Public Function connect()

    Dim sql As String = "Data Source=RINOR-PC\SQLEXPRESS;Initial Catalog=Ordinanca;Integrated Security=True"
    Return New Sqlconnection(sql)
End Function

Public Function search(pmtData As Date) As DataTable

    Using con As SqlConnection = connect()

        Dim dt As Datatable = New Datatable()
        Try

            Dim query As String = "SELECT * FROM CLIENTS WHERE DT='" + pmtData + "'"
            Dim cmd As SqlCommand = New sqlCommand(query, con)
            Dim da As SqldataAdapter = New sqlDataadapter(cmd)
            da.Fill(dt)

        Catch ex As Exception

            Messagebox.show(ex.Message)
        Finally
            con.close()
        End Try

        Return dt

    End Using
End Function

Private Sub monthcalendar1_datechanged(sender As Object, e As DateRangeEventArgs) Handles monthcalendar1.datechanged

    Dim datatable As datatable
    Dim pmtdata As Date
    pmtdata = monthcalendar1.selectionstart
    datatable = search(pmtdata)
    gridview.datasource = datatable

End Sub
我的代码:

Public Class Form1
  Dim db As New OrdinancaEntities
  Dim pacienti As New Pacientet
  Dim isEditing As Boolean = True
  Dim selectedPacient As Int16

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BindingDatagridView()
    FillData("")
  End Sub

  Sub BindingDatagridView()
    dgvPacientet.DataSource = db.Pacientets.ToList()
  End Sub

  Sub FillData(searchText As String)
    Dim data As List(Of Pacientet)
    Dim context As New OrdinancaEntities
    If searchText = "" Then
        data = context.Pacientets.OrderBy(Function(t) t.Id).ToList()
    Else
        data = context.Pacientets.Where(Function(t) t.Emri.StartsWith(searchText) Or t.Mbiemri.StartsWith(searchText)).OrderBy(Function(t) t.Id).ToList()
    End If
    dgvPacientet.AutoGenerateColumns = False
    dgvPacientet.DataSource = data
    SelectClient()
  End Sub

  Sub EnableButton(bPërditëso As Boolean)
    btnPërditëso.Enabled = bPërditëso
  End Sub

  Sub SelectClient()
    If dgvPacientet.SelectedRows.Count > 0 Then
        selectedPacient = CInt(dgvPacientet.SelectedRows.Item(0).Cells("Id").Value.ToString())

        txtEmri.Text = dgvPacientet.SelectedRows.Item(0).Cells("Emri").Value.ToString()
        txtMbiemri.Text = dgvPacientet.SelectedRows.Item(0).Cells("Mbiemri").Value.ToString()

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Emri_prindit").Value) Then
            txtEmri_prindit.Text = dgvPacientet.SelectedRows.Item(0).Cells("Emri_prindit").Value.ToString()
        Else
            txtEmri_prindit.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Datelindja").Value) Then
            txtDatelindja.Text = dgvPacientet.SelectedRows.Item(0).Cells("Datelindja").Value.ToString()
        Else
            txtDatelindja.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Numri_telefonit").Value) Then
            txtNumri.Text = dgvPacientet.SelectedRows.Item(0).Cells("Numri_telefonit").Value.ToString()
        Else
            txtNumri.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Diagnoza").Value) Then
            txtDiagnoza.Text = dgvPacientet.SelectedRows.Item(0).Cells("Diagnoza").Value.ToString()
        Else
            txtDiagnoza.Text = ""
        End If
        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Tretmani_planifikuar").Value) Then
            txtTretmani_planifikuar.Text = dgvPacientet.SelectedRows.Item(0).Cells("Tretmani_planifikuar").Value.ToString()
        Else
            txtTretmani_planifikuar.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Tretmani_kryer").Value) Then
            txtTretmani_kryer.Text = dgvPacientet.SelectedRows.Item(0).Cells("Tretmani_kryer").Value.ToString()
        Else
            txtTretmani_kryer.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Pagesa").Value) Then
            txtPagesa.Text = dgvPacientet.SelectedRows.Item(0).Cells("Pagesa").Value.ToString()
        Else
            txtPagesa.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Pagoi").Value) Then
            txtPagoi.Text = dgvPacientet.SelectedRows.Item(0).Cells("Pagoi").Value.ToString()
        Else
            txtPagoi.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Doktori").Value) Then
            cboDoktori.Text = dgvPacientet.SelectedRows.Item(0).Cells("Doktori").Value.ToString()
        Else
            cboDoktori.Text = ""
        End If

        If Not IsNothing(dgvPacientet.SelectedRows.Item(0).Cells("Dt_terminit").Value) Then
            Me.MonthCalendar1.SelectionStart = dgvPacientet.SelectedRows.Item(0).Cells("Dt_terminit").Value.ToString()
        Else
            Me.MonthCalendar1.SelectionStart = Date.Now
        End If
    End If
  End Sub

  Private Sub btnPërditëso_Click(sender As Object, e As EventArgs) Handles btnPërditëso.Click

    If isEditing Then
        Dim context As New OrdinancaEntities
        Dim client As Pacientet = (From c In context.Pacientets
                               Where c.Id = selectedPacient
                                  Select c).FirstOrDefault()
        client.Emri = txtEmri.Text
        client.Mbiemri = txtMbiemri.Text
        client.Emri_prindit = txtEmri_prindit.Text
        client.Datelindja = txtDatelindja.Text
        client.Numri_telefonit = txtNumri.Text
        client.Diagnoza = txtDiagnoza.Text
        client.Tretmani_planifikuar = txtTretmani_planifikuar.Text
        client.Tretmani_kryer = txtTretmani_kryer.Text
        client.Pagesa = txtPagesa.Text
        client.Pagoi = txtPagoi.Text
        client.Doktori = cboDoktori.SelectedItem
        client.Dt_terminit = Me.MonthCalendar1.SelectionStart
        context.SaveChanges()
        BindingDatagridView()
    End If
    isEditing = True
    FillData("")
    MessageBox.Show("Të dhënat u insertuan me sukses.")
  End Sub

  Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
    ClearText()
  End Sub

  Sub ClearText()
    txtEmri.Clear()
    txtMbiemri.Clear()
    txtNumri.Clear()
    txtDiagnoza.Clear()
    txtPagesa.Clear()
    txtPagoi.Clear()
    cboDoktori.Text = "Zgjedh doktorin"
    txtEmri.Focus()
    txtTretmani_planifikuar.Clear()
    txtTretmani_kryer.Clear()
    txtDatelindja.Clear()
    txtEmri_prindit.Clear()
  End Sub

  Private Sub txtKerko_TextChanged(sender As Object, e As EventArgs) Handles txtKerko.TextChanged
    FillData(txtKerko.Text)
  End Sub

  Private Sub dgvPacientet_RowEnter(sender As Object, e As DataGridViewCellEventArgs) Handles dgvPacientet.RowEnter
    SelectClient()
  End Sub
End Class

我想这正是你所需要的。它只是用对实体框架类更简单的调用来替换“搜索”函数中所有与DataTable相关的功能:

Private Sub monthcalendar1_datechanged(sender As Object, e As DateRangeEventArgs) Handles monthcalendar1.datechanged
  Dim pmtdata As Date
  pmtdata = monthcalendar1.selectionstart
  Dim records = db.Pacientets.Where(Function(p)  p.dt_terminit.Equals(pmtdata)).ToList()

  'add extra rows if not all rows populated
  If (records.Count() < 10) Then
    Dim timeSlots = db.TimeSlots 'get the list of all possible appointment times in one day, this is a new table you must create and populate in advance

    For Each(slot in timeSlots)
      'here, check that the slots's time is not in any of the items in the "records" list
      'if it's not, add it to the Pacientes table with only time and date completed.
    Next

    'requery the DB
    records = db.Pacientets.Where(Function(p)  p.dt_terminit.Equals(pmtdata)).ToList()
  End If

  gridview.datasource = records
End Sub
Private子monthcalendar1\u datechanged(发送方作为对象,e作为DateRangeEventArgs)处理monthcalendar1.datechanged
Dim pmtdata作为日期
pmtdata=monthcalendar1.selectionstart
Dim records=db.pacientes.Where(函数(p)p.dt_terminit.Equals(pmtdata)).ToList()
'如果未填充所有行,则添加额外行
如果(records.Count()<10),则
Dim timeSlots=db.timeSlots'获取一天内所有可能约会时间的列表,这是一个必须提前创建和填充的新表
对于每个(时隙中的插槽)
'在这里,检查插槽的时间是否不在“记录”列表中的任何项目中
如果不是,则将其添加到Pacientes表中,并仅填写时间和日期。
下一个
“重新询问数据库
记录=db.pacientes.Where(函数(p)p.dt_terminit.Equals(pmtdata)).ToList()
如果结束
gridview.datasource=记录
端接头
显然,您必须将任何变量或字段名(如monthcalendar或gridview)更改为您正在使用的任何名称。我也使用了
Time.Date
,因为您的时间列将包含特定的时间,而月历包含没有时间的日期


另外,我不知道“Pacientes”是否是正确的实体。我不会说你的语言,但听起来像是病人名单。如果您给出时间,可能您有另一个实体,它描述了与特定时间相关的预约列表,以及特定患者?

是否将“搜索”返回的数据表替换为您的实体列表?只要实体的属性名与gridview匹配,它就应该工作?@Adyson我不能在使用实体模型时使用sqlcommand、sqlconnection和dataAdapter,这就是我要说的。将搜索函数中的所有代码替换为用于检索实体的代码。从你的问题来看,听起来你已经写过了。您仍然可以使用实体列表绑定到gridview。请您更具体一点,编写一些代码来理解您,非常感谢。好的,您已经有了获取实体的代码,对吗?编辑问题,向我展示代码,我们应该能够将其与其他代码结合起来嗨,我尝试了你的答案,但正如我在输入表单时提到的那样,datagridview包含10行空字符串。我在sqlserver中填充了这些行,我让它们除了时间之外都是空的,在时间栏中,我插入了从09:00到18:00的小时数,因此,当我输入表单时,我选择包含特定时间的行,在文本框中写入患者信息,然后单击按钮Perditeso,这意味着用您的语言更新,然后在该特定时间插入患者。使用您的代码,gridview没有行,我需要在每次输入表单时在sql中创建的行,因为我指定了插入患者的时间。祝您有个美好的一天!希望得到你的答案所以…你在SQL中创建了10行,但它们没有返回?这些行在数据库中的哪个表中?他们是坐在沙发上还是另一张桌子上?您的“时间”列是否也包含日期和一天中的时间?这些行是用Sql创建的,当我单击“更新”时,这些行会显示出来,但当我尝试选择一行时,它会变成一个错误。行是在Sql表名为Pacientet的表Pacientet中创建的,时间列只包含一天中的时间“时间列只包含一天中的时间”。那么在这种情况下,你认为它与约会有什么关系呢?在最初的问题中,您暗示此时间列足以匹配时间和日期。您是否有另一列包含日期,我编写的查询可以与之进行比较?否则,您如何确切地知道预约时间?