【发布时间】: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