首页 > 解决方案 > 如果存在匹配,则在 GridView A 和 B 和 GridView A 中查找匹配日期和自动勾选复选框 - VB.Net

问题描述

我有一个 Gridview 生成选定月份的总日期并以表格格式显示它们,以及一个复选框列。

勾选复选框时,会将日期以及来自其他字段的附加数据上传到 SQL 数据库中。取消勾选它将从数据库中删除数据行。

实际上,如果我决定更改月份或开始新会话,GridView 无法记住在上次会话中勾选了哪一行的复选框。

我不认为我可以使用 SQL 查询来帮助解决这个问题,因为 GridView 内容是通过代码生成的并且不使用数据库。所以我制作了另一个 GridView,这个从 SQL 收集相关日期,这些日期在上传到数据库时从附加字段中匹配信息。

我的想法是让第一个 GridView 检查来自第二个 GridView 的数据,并在加载时自动勾选正确的复选框。

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ShowBtn.Click

    Call load_Calander()

End Sub

Sub load_Calander()

    '~ code to generate date column in GridView1 ~'
    Dim year As Integer = Convert.ToInt32(CurrentYear.Text)
    Dim month As Integer = Convert.ToInt32(Mth_Num.Text)
    If month = 1 Then

        Mth_Txt.Text = "Jan"

    ElseIf month = 2 Then

        Mth_Txt.Text = "Feb"

    ElseIf month = 3 Then

        Mth_Txt.Text = "Mar"

    ElseIf month = 4 Then

        Mth_Txt.Text = "Apr"

    ElseIf month = 5 Then

        Mth_Txt.Text = "May"

    ElseIf month = 6 Then

        Mth_Txt.Text = "Jun"

    ElseIf month = 7 Then

        Mth_Txt.Text = "Jul"

    ElseIf month = 8 Then

        Mth_Txt.Text = "Aug"

    ElseIf month = 9 Then

        Mth_Txt.Text = "Sep"

    ElseIf month = 10 Then

        Mth_Txt.Text = "Oct"

    ElseIf month = 11 Then

        Mth_Txt.Text = "Nov"

    ElseIf month = 12 Then

        Mth_Txt.Text = "Dec"

    End If

    Dim myList As List(Of [myClass]) = New List(Of [myClass])()

    If month = 1 OrElse month = 3 OrElse month = 5 OrElse month = 7 OrElse month = 8 OrElse month = 10 OrElse month = 12 Then

        For i As Integer = 1 To 31

            Dim item As [myClass] = New [myClass]()
            item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
            Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
            item.[Day] = dateValue.ToString("ddddddddd")

            myList.Add(item)

        Next

    ElseIf month = 4 OrElse month = 6 OrElse month = 9 OrElse month = 11 Then

        For i As Integer = 1 To 30

            Dim item As [myClass] = New [myClass]()
            item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
            Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
            item.[Day] = dateValue.ToString("ddddddddd")
            myList.Add(item)

        Next

    ElseIf DateTime.IsLeapYear(year) Then

        For i As Integer = 1 To 29

            Dim item As [myClass] = New [myClass]()
            item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
            Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
            item.[Day] = dateValue.ToString("ddddddddd")
            myList.Add(item)

        Next

    Else

        For i As Integer = 1 To 28

            Dim item As [myClass] = New [myClass]()
            item.[Date] = String.Format("{0} {1} {2}", i.ToString("00"), Mth_Txt.Text, CurrentYear.Text)
            Dim dateValue As DateTime = New DateTime(Convert.ToInt32(CurrentYear.Text), Convert.ToInt32(Mth_Num.Text), i)
            item.[Day] = dateValue.ToString("ddddddddd")
            myList.Add(item)

        Next

    End If

    GridView1.DataSource = myList
    GridView1.DataBind()

    'code that generates GridView2 (SQL data)
    Dim cmd As New SqlCommand
    Dim DateTB As New DataSet

    Dim DateCmd As String = "select ScheduleDate FROM [SQLIOT].[dbo].[ZEPB_PreventiveDateSchedule] " _
                            & " where line_desc = '" & LineList.SelectedItem.Text & "' and process = '" & ProcessList.SelectedItem.Text & "' " _
                            & " and machine = '" & MachineList.SelectedItem.Text & "' and Step = '" & StepList.SelectedItem.Text & "' " _
                            & " and inspection = '" & InspectionList.SelectedItem.Text & "' and  specification = '" & SpecificationList.SelectedItem.Text & "' "

    Dim da As New SqlDataAdapter(DateCmd, Conn)

    cmd.Connection = Conn

    Conn.Open()
    da.Fill(DateTB, "TempStore")

    Dim dvgroup As DataView = DateTB.Tables("TempStore").DefaultView

    GridView2.DataSource = dvgroup
    GridView2.DataBind()

    Conn.Close()

    'code to cross-reference and auto tick checkbox - Currently empty...
    

目前,我对如何编写交叉引用部分有点困惑。

另外,稍微解释一下程序是如何工作的。:

  1. 当第一次加载时,有一堆下拉列表,用户可以在其中选择他们的数据以及月份。
  2. 单击一个按钮,日历 GridView 将加载基于月份选择器的完整日期。
  3. 用户将单击任何 GridView 的行复选框以从下拉列表中上传信息,以及从 GridView 中选择的日期到 SQL 数据库。

希望这能为我的问题提供更多背景信息。

标签: asp.netvb.netgridview

解决方案


推荐阅读