【问题标题】:MonthCalendar datechanged eventMonthCalendar datechanged 事件
【发布时间】:2017-02-02 10:36:27
【问题描述】:

我有一个项目来保存特定日期的患者约会。我的项目拥有一个与 sqlserver 数据库链接的 datagridview。网格包含一个名为“时间”的列。我在 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

【问题讨论】:

  • 用您的实体列表替换从“搜索”返回的数据表?只要您的实体的属性名称与 gridview 匹配,它应该可以工作?
  • @Adyson 我在使用实体模型时无法使用 sqlcommand、sqlconnection、dataAdapter
  • 没错,这就是我要说的。将搜索功能中的所有代码替换为您用于检索实体的代码。从您的问题看来,您已经写过了。您仍然可以使用实体列表绑定到 gridview。
  • 能不能说的具体一些,写一些代码来理解一下,先谢谢了
  • 好吧,您已经获得了获取实体的代码,对吧?编辑问题以向我展示该代码,我们应该能够将它与其他代码结合起来

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


【解决方案1】:

我认为这正是您所需要的。它只是用更简单的实体框架类调用替换了“搜索”函数中所有与 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

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

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

【讨论】:

  • 嗨,我试过你的答案,但正如我提到的,当我输入表单时,datagridview 包含 10 行空字符串。我在 sqlserver 中填充了这些行,除了时间之外,我让它们为空,在列时间我从 09:00 到 18:00 插入小时,所以当我输入表格时,我选择包含特定时间的行,在文本框中写入患者信息,然后单击按钮 Perditeso,这意味着更新您的语言,然后插入患者在那个特定的时间。
  • 使用您的代码,gridview 没有行,我每次输入表单时都需要我在 sql 中创建的那些行,因为这些行我指定了我必须在何时插入患者。祝你今天过得愉快 !期待您的回答
  • 所以...您在 SQL 中创建了 10 行,但它们没有返回?这些行在数据库中的哪个表中?他们是在 Pacientets 还是另一张桌子?您的“时间”列是否还包含日期和时间?
  • 行是在 Sql 中创建的,当我单击更新时,会显示这些行,但是当我尝试选择一行时会出现错误。行在表 Pacientet 中创建,Sql 表名为 Pacientet,时间列仅包含一天中的时间
  • “时间列仅包含一天中的时间”。那么在这种情况下,您如何期望它与日期相关?在最初的问题中,您暗示此时间列足以匹配时间 和 日期。你有另一列包含日期,我写的查询可以比较吗?否则,你怎么知道约会的确切时间?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多