将派生的 DataTable 绑定到 SQL 制作的 DataTable 以填充 Datagrid

发布于 2024-12-07 08:43:18 字数 1510 浏览 1 评论 0原文

我很茫然,到处寻找如何以我理解的方式做到这一点,但仍然没有运气。我试图向客户展示空闲时间以及每天的安排。他们的日程安排保存在服务器上并显示他们的繁忙时间。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一瞬间的火花 2024-12-14 08:43:18

没关系,我已经弄清楚了!是的!

如果有人遇到同样的问题,我将两个数据表并使用以下代码进行合并:

        Dim pk1(0) As DataColumn
        Dim pk2(0) As DataColumn

        pk1(0) = dtTime.Columns("Time")
        dtTime.PrimaryKey = pk1

        pk2(0) = dtSched.Columns("Time")
        dtSched.PrimaryKey = pk2
        dtTime.Merge(tblSched, False, MissingSchemaAction.Ignore)

        Me.BindingSource1.DataSource = dtTime

在合并之前,我将列名添加到 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:

        Dim pk1(0) As DataColumn
        Dim pk2(0) As DataColumn

        pk1(0) = dtTime.Columns("Time")
        dtTime.PrimaryKey = pk1

        pk2(0) = dtSched.Columns("Time")
        dtSched.PrimaryKey = pk2
        dtTime.Merge(tblSched, False, MissingSchemaAction.Ignore)

        Me.BindingSource1.DataSource = dtTime

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文