将派生的 DataTable 绑定到 SQL 制作的 DataTable 以填充 Datagrid
我很茫然,到处寻找如何以我理解的方式做到这一点,但仍然没有运气。我试图向客户展示空闲时间以及每天的安排。他们的日程安排保存在服务器上并显示他们的繁忙时间。我使用 SQL 查询提取该数据并将其放入名为 dtSched 的数据表中。然后,我创建了另一个名为 dtTime 的数据表,以 15 分钟为增量列出从上午 6:00 到晚上 10:00 的时间。我现在尝试做的是结合两个数据表来显示 dtTime 中列出的所有时间,并显示客户端安排时间的位置,这样我就可以显示空行以允许添加约会并显示安排的时间,这样就不会在其中添加约会时间段。
这是我创建 dtTime 表(所有时间)的代码:
Dim strStartDate As DateTime
Dim strEndDate As DateTime
strStartDate = DateValue(Now()) & " 6:00 AM"
strEndDate = DateValue(Now()) & " 10:00 PM"
While strStartDate <= strEndDate
strStartDate = strStartDate.AddMinutes(15)
Dim dr As System.Data.DataRow = dt.NewRow()
dr("Time") = strStartDate
dt.Rows.Add(dr)
End While
这是我的 sql 派生数据表 dtSched(计划时间):
Dim conn = New SqlConnection("Connection")
Dim strSQL As String
strSQL = "SELECT * FROM MYTABLE WHERE SCHED DATE = 'Date'"
Me.dataAdapter = New SqlDataAdapter(strSQL, conn)
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim dtSched As New DataTable()
dtSched.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(dtSched)
我试图使用 GetData 执行来绑定两个数据表,但它不起作用:
Me.dataGrid.DataSource = Me.BindingSource1
GetData("SELECT dt.Time, dtSched.Date, dtSched.ID, dtSched.Client, dtSched.StartTime, dtSched.Reason FROM dt LEFT JOIN dtSched ON dt.Time = dtSched.StartTime")
我正在尝试通过 dt.Time 和 dtSched.StartTime 连接两个数据表。然后填充数据网格。任何人都能提供的任何帮助都将是非常棒的!
谢谢!
I am at a loss and have searched everywhere to figure out how to do this in a way I understand but still no luck. I am trying to show the time the client has free and what is scheduled on a day-to-day basis. Their schedule is kept on a server and displays their busy time. I pulled that data using a sql query and dropped that into a datatable called dtSched. I then created another datatable called dtTime to list the time from 6:00 AM - 10:00 PM by 15 minute increments. What I am now trying to do is combine both datatables to display all the time listed in dtTime and display where the client has time scheduled so I can show empty rows to allow appointments to be added and display scheduled time so appointments are not added in that time slot.
Here is my code to create the dtTime table (all time):
Dim strStartDate As DateTime
Dim strEndDate As DateTime
strStartDate = DateValue(Now()) & " 6:00 AM"
strEndDate = DateValue(Now()) & " 10:00 PM"
While strStartDate <= strEndDate
strStartDate = strStartDate.AddMinutes(15)
Dim dr As System.Data.DataRow = dt.NewRow()
dr("Time") = strStartDate
dt.Rows.Add(dr)
End While
Here is my sql-derived datatable dtSched (scheduled time):
Dim conn = New SqlConnection("Connection")
Dim strSQL As String
strSQL = "SELECT * FROM MYTABLE WHERE SCHED DATE = 'Date'"
Me.dataAdapter = New SqlDataAdapter(strSQL, conn)
Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
Dim dtSched As New DataTable()
dtSched.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(dtSched)
I was trying to use a GetData execution to tie the two datatables but it did not work:
Me.dataGrid.DataSource = Me.BindingSource1
GetData("SELECT dt.Time, dtSched.Date, dtSched.ID, dtSched.Client, dtSched.StartTime, dtSched.Reason FROM dt LEFT JOIN dtSched ON dt.Time = dtSched.StartTime")
I am trying to connect both datatables by dt.Time and dtSched.StartTime. Then fill the datagrid. Any assistance anyone can provide would be downright awesome!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我已经弄清楚了!是的!
如果有人遇到同样的问题,我将两个数据表并使用以下代码进行合并:
在合并之前,我将列名添加到 dtSched 表中的 dtTime 表中,然后将 dtTime 添加到我的数据网格中,这是一个裹!我还添加了第三个数据表,它就像手套一样工作。多么令人欣慰啊!
Never mind, I figured it out! Yea!
In case anyone has the same problem, I took my two datatables and did a merge using the following code:
I added the column names to my dtTime table that were in the dtSched table before merging and then boud dtTime to my datagrid and it was a wrap! I also added a third data table to the mix and it worked like a glove. What a relief!