如何在VB.NET 2010中的DataGridView中显示特定的行数据?

时间:2015-07-06 14:04:04

标签: datagridview vb.net-2010

如果我以ADMIN身份登录,我只希望看到其位置为HR Employee。换句话说,位置AdminIT Staff不会出现在DataGridView中。你们能帮助我吗?我正在使用sqldatabase和visual basic .net 2010。

我的数据库:

id - UN   -  PW   - userlevel - position   - fname - lname  - email

1   admin - admin - Admin    - Admin       - admin -  admin - admin@gmail.com

2   emp   - emp   - Employee - HR Employee - emp   -  emp   - emp@gmail.com

3   emp2  - emp2  - Employee - HR Employee - emp   -  emp   - emp@yahoo.com

4   emp3  - emp3  - Employee - IT Staff    - emp   -  emp   - emp2@gmail.com

这是我的代码:

Imports System.Data.SqlClient
Public Class Employee_Details
    Dim connect As String = ("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\virgilio\Desktop\sqlserver\users.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
    Dim objconnection, objconnection2 As New SqlClient.SqlConnection(connect)
    Dim rc As Integer

    Private Sub Employee_Details_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Tb_user2TableAdapter.Fill(Me.UsersDataSet1.tb_user2)

        Dim sqlselect As String = "select * from tb_user2 where position=@position and  username=@username and password=@password"
        Dim sqladapter As New SqlDataAdapter

        Dim sqlcommand As New SqlClient.SqlCommand(sqlselect, objconnection)

        With sqlcommand.Parameters
            .Add("@position", SqlDbType.Char).Value = sqlselect
            .Add("@username", SqlDbType.Char).Value = sqlselect
            .Add("@password", SqlDbType.Char).Value = sqlselect
        End With


        If objconnection.State = ConnectionState.Closed Then objconnection.Open()

        sqladapter.InsertCommand = sqlcommand
        sqladapter.InsertCommand.ExecuteNonQuery()
        rc = sqlcommand.ExecuteScalar()


        Dim reader As SqlClient.SqlDataReader = sqlcommand.ExecuteReader

        Dim index, position As String
        index = DataGridView1.CurrentCell.RowIndex
        position = DataGridView1.Rows(index).Cells(4).Value

        Dim dv As DataView = New DataView()

        dv.Table = UsersDataSet1.tb_user2
        dv.RowFilter = "position like '" & position & "%'"

        If reader.Read = True Then

            Dim p1 As String = reader("position")
            Dim level As String = reader("userlevel")

            If p1 = "HR Employee" Then
                p1 = position
                position = dv.RowFilter

            ElseIf level = "Admin" Then
                HRIS_main.RecruitmentToolStripMenuItem.Enabled = True
                HRIS_main.TrainingToolStripMenuItem.Enabled = True
                HRIS_main.EvaluationToolStripMenuItem.Enabled = True
                Me.Hide()
                splash_frm.Show()



            End If
        End If

    End Sub

End Class

登录代码:

Imports System.Data.SqlClient
Public Class login_frm

    Dim PassTrials As Integer
    Dim connect As String = ("Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\virgilio\Desktop\sqlserver\users.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")

    Dim objconnection, objconnection2 As New SqlClient.SqlConnection(connect)

    Dim rc As Integer
    Dim user As String
    Dim userclass As String
    Dim loginCtr As Integer = 5

    Private Sub login_btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles login_btn.Click

        Dim sqlselect As String = "select * from TB_USER where username=@username and password=@password"
        Dim sqladapter As New SqlDataAdapter

        Dim sqlcommand As New SqlClient.SqlCommand(sqlselect, objconnection)

        With sqlcommand.Parameters
            .Add("@Username", SqlDbType.Char).Value = user_login_btn.Text
            .Add("@Password", SqlDbType.Char).Value = pass_login_btn.Text
        End With



        If objconnection.State = ConnectionState.Closed Then objconnection.Open()

        sqladapter.InsertCommand = sqlcommand
        sqladapter.InsertCommand.ExecuteNonQuery()
        rc = sqlcommand.ExecuteScalar()

        Dim reader As SqlClient.SqlDataReader = sqlcommand.ExecuteReader



        If user_login_btn.Text = "user" Then


            HRIS_main.RecruitmentToolStripMenuItem.Enabled = False
            HRIS_main.TrainingToolStripMenuItem.Enabled = False

        End If

        If rc = 1 Then
            MessageBox.Show("Welcome " & user_login_btn.Text)

            Me.Hide()
            splash2_frm.Show()
        Else
            user_login_btn.Clear()
            pass_login_btn.Clear()

            loginCtr -= 1

            MsgBox("Sorry, Username or Password is incorrect. You have " & loginCtr & " login attempts remaining.", MsgBoxStyle.OkOnly, "Invalid")

            If loginCtr <= 0 Then Application.Exit()

        End If


        If reader.Read = True Then
            Dim level As String = reader("userlevel")
            Dim user As String = reader("username")

            If level = "Employee" Then
                HRIS_main.RecruitmentToolStripMenuItem.Enabled = False
                HRIS_main.TrainingToolStripMenuItem.Enabled = False
                HRIS_main.EvaluationToolStripMenuItem.Enabled = False

                Me.Hide()
                splash_frm.Show()

            ElseIf level = "Supervisor" Then
                HRIS_main.RecruitmentToolStripMenuItem.Enabled = False
                HRIS_main.TrainingToolStripMenuItem.Enabled = False
                HRIS_main.EvaluationToolStripMenuItem.Enabled = False
                Me.Hide()
                splash_frm.Show()

            ElseIf level = "Admin" Then
                HRIS_main.RecruitmentToolStripMenuItem.Enabled = True
                HRIS_main.TrainingToolStripMenuItem.Enabled = True
                HRIS_main.EvaluationToolStripMenuItem.Enabled = True
                Me.Hide()
                splash_frm.Show()

            End If
            HRIS_main.lbl_username.Text = user
            HRIS_main.lbl_userlevel.Text = level

        End If
        reader.Close()
        sqlcommand.Dispose()
        If objconnection.State = ConnectionState.Open Then objconnection.Close()

        'clearing user data
        user_login_btn.Clear()
        pass_login_btn.Clear()
    End Sub

    Private Sub pass_login_btn_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles pass_login_btn.GotFocus
        If pass_login_btn.Text = "Password..." Then
            pass_login_btn.Text = ""
            pass_login_btn.PasswordChar = "*"


        End If
    End Sub

    Private Sub user_login_btn_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles user_login_btn.GotFocus
        If user_login_btn.Text = "Username..." Then
            user_login_btn.Text = ""

        End If
    End Sub

    Private Sub login_frm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

首先,您需要获得用户ID。然后检查是否是管理员。如果有管理员登录则 从表中选择*,其中UserLevel =“Admin”position =“HR Employee”