MonthCalendar datechanged事件

时间:2017-02-02 10:36:27

标签: sql-server vb.net entity-framework datagridview

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

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

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

1 个答案:

答案 0 :(得分:0)

我认为这就像你需要的东西。它只是简单地用“Entity Framework”类替换“search”函数中所有与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

显然,您必须将任何变量或字段名称(例如monthcalendar或gridview)更改为您正在使用的任何名称。我也使用Time.Date,因为您的时间列将包含特定时间,而月份日历包含没有时间的日期。

此外,我不知道“Pacientets”是否是正确的查询权限。我不说你的语言,但听起来像是一份病人名单。如果您给予时间,可能您有另一个实体描述了与特定时间和特定患者相关的约会列表?