VB.Net 2003-如何创建具有绑定列功能的DataGrid按钮列?

时间:2019-02-12 06:53:20

标签: vb.net visual-studio-2003

我正在VB.Net 2003中创建一个DataGrid,它从SQL获取数据。所有包含SQL数据的列都是BoundColumn,最后带有一个额外的ButtonColumn来更改列的状态。

在这种状态下,我可以单击一行的按钮,并且该行上的相应数据可以更改而不会出现问题。但是我的问题出在我必须向DataGrid添加过滤器功能时。

一旦对数据进行了过滤,我尝试单击第一行的按钮,则看不到任何更改。直到我删除过滤器并注意到第一条未过滤的行上的数据已更改。

1. Before filter data:

001 | QW34 | OK | <BUTTON>
002 | XC00 | OK | <BUTTON>
003 | GH66 | OK | <BUTTON>

2. After filter data:

002 | XC00 | OK | <BUTTON>

3. Click the button to change OK to NG and vice versa.

4. Filter data remains the same. Return back to original table state.

001 | QW34 | NG | <BUTTON>
002 | XC00 | OK | <BUTTON>
003 | GH66 | OK | <BUTTON>

希望这可以解释我的观点。基本上,由于button列只是普通的ButtonColumn,所以它没有绑定到BoundColumn的行,因此,只要过滤器中的行发生变化,Button列就保持不变。

如何使“按钮列”具有与“绑定列”相同的属性?在VB.Net 2003中我没有意识到的addiitoanl属性可以做到这一点吗?

这是将SQL数据绑定到DataGrid的子项。与子项一起过滤数据。

Public Sub BindGrid()

    Dim strSql As String = "Select * from [SQL3].[dbo].[Final] order by [Course Code]"
    Dim dtb As New DataTable
    SQLDB.Open()
    Dim dad As New SqlDataAdapter(strSql, SQL)
    dad.Fill(dtb)
    SQL.Close()
    DataGrid1.DataSource = dtb
    DataGrid1.DataBind()
    dtb.Clear()

End Sub

Public Sub filter()

    If DropDownList1.SelectedItem.ToString = "[ALL]" Then

        Call BindGrid()

    Else

        Dim strSelectCmd As String = "Select * from [SQL3].[dbo].[Final] where result = '" & DropDownList1.SelectedItem.ToString & "'"
        Dim dtb As New DataTable
        SQL.Open()
        Dim dad As New SqlDataAdapter(strSelectCmd, SQL)
        dad.Fill(dtb)
        SQL.Close()
        DataGrid1.DataSource = dtb
        DataGrid1.DataBind()

    End If

End Sub

这是每行中的按钮列的子项,用于更改标志状态并更新为SQL。

Private Sub CellClick_CellValueChanged(ByVal sender As Object, ByVal e As DataGridCommandEventArgs) Handles DataGrid1.ItemCommand

    Session("Employee_No") = e.Item.Cells(2).Text
    Session("Course_Code") = e.Item.Cells(0).Text
    Session("Flag") = e.Item.Cells(8).Text

    'Change Flag Status in SQL
    If Session("Flag") = "Active" Then
        Dim updateState As String = "update [SQLDB3].[dbo].[ZOJT_EMP_COURSE]" _
                            & "set [Flag] = 'Inactive'" _
                            & "where Employee_No = '" & Session("Employee_No") & "'" _
                            & "and CourseCode = '" & Session("Course_Code") & "'"

        Dim conn = SQLDB
        Dim command As New SqlCommand(updateState, conn)

        conn.open()
        command.ExecuteNonQuery()
        conn.close()

        Label3.Text = Session("Course_Code")

        Call filter()

        conn.dispose()
        command.Dispose()

        'Response.Redirect(HttpContext.Current.Request.Url.ToString(), True)

    ElseIf Session("Flag") = "Inactive" Then
        Dim updateState As String = "update [SQLDB3].[dbo].[ZOJT_EMP_COURSE]" _
                            & "set [Flag] = 'Active'" _
                            & "where Employee_No = '" & Session("Employee_No") & "'" _
                            & "and CourseCode = '" & Session("Course_Code") & "'"

        Dim conn = SQLDB
        Dim command As New SqlCommand(updateState, conn)


        conn.open()
        command.ExecuteNonQuery()
        conn.close()

        Label3.Text = Session("Course_Code")

        Call filter()

        conn.dispose()
        command.Dispose()


    End If

End Sub

0 个答案:

没有答案