简体   繁体   English

vb.net中的数据过滤

[英]Data Filtering in vb.net

I was trying to create a filter on the datagridview using combobox . 我试图使用comboboxdatagridview上创建过滤器。 What I wanted to do is to filter the filtered data in the datagridview . 我想做的是在datagridview过滤已过滤的datagridview I have five comboboxes for the filtration. 我有五个comboboxes用于过滤。

For example, the first combobox is for the Year then I still want to filter the search into Grade level, then to Section and so on. 例如,第一个combobox用于“年份”,然后我仍要将搜索过滤到“年级”,然后再过滤到“科”,依此类推。 So, the user will be able to sort or filter his search from the database. 因此,用户将能够对数据库中的搜索进行排序或过滤。

So far I have my stored procedure code and tried it in the combo box. 到目前为止,我已经有了存储过程的代码,并在组合框中尝试了它。 The SchoolYear, Grade, Section, Gender filtering works well.. aside from Account filtering, there are two possible inputs Active and inactive. SchoolYear,Grade,Section,Gender过滤效果很好。.除了Account过滤,还有两个可能的输入Active和Inactive。

When I choose Active for filtering, there are some inactive records that shows, otherwise inactive filtering has no problem. 当我选择“活动”进行过滤时,会显示一些不活动的记录,否则,不活动的过滤不会有问题。

My stored procedure code: 我的存储过程代码:

ALTER PROCEDURE [dbo].[uspYearGradeFilter]
    @Year Nvarchar(20)= NULL,
    @Grade Nvarchar(20) = NULL,
    @Section Nvarchar(20)= NUll,
    @Gender Nvarchar(20)= NULL,
    @Account Nvarchar(20) = NULL
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        si.StudentID, si.Surname, si.FirstName, 
        si.MiddleName, si.Gender, si.BirthDay, si.TelNum,
        Birthday, getdate() AS [Today],
        Datediff(yy, BirthDay, getdate()) -
            CASE
               WHEN DATEADD(YY, DATEDIFF(YY, BirthDay, getdate()), BirthDay)
                > GETDATE()   
                  THEN 1
                  ELSE 0
            END AS [age]
    FROM 
        StudentInformation si
    JOIN 
        StudentHistory sh ON si.StudentID = sh.StudentID
    WHERE 
        sh.SchoolYear LIKE COALESCE('%'+ @Year+'%', sh.SchoolYear)
        AND sh.Levels LIKE COALESCE('%' + @Grade + '%', sh.Levels)
        AND siGender LIKE COALESCE('%' + @Gender + '%', si.gender)
        AND sh.Section LIKE COALESCE('%' + @Section + '%', sh.Section) 
        AND si.account LIKE COALESCE('%' + @Account + '%', si.account) 
END

My vb.net code 我的vb.net代码

 Private Sub BindGrid()
        Using cmd As New SqlCommand("dbo.uspYearGradeFilter", cn)

            cmd.CommandType = CommandType.StoredProcedure

            'Add a parameter for all comboboxes but only if a value is selected:

            If cboYear.SelectedIndex >= 0 Then
                Dim paramYear As New SqlParameter("@Year", SqlDbType.NVarChar, 20)
                paramYear.Value = cboYear.Text
                cmd.Parameters.Add(paramYear)
            End If

            If cboGrade.SelectedIndex >= 0 Then
                Dim paramGrade As New SqlParameter("@Grade", SqlDbType.NVarChar, 20)
                paramGrade.Value = cboGrade.Text
                cmd.Parameters.Add(paramGrade)
            End If
            If cboSection.SelectedIndex >= 0 Then
                Dim paramSection As New SqlParameter("@Section", SqlDbType.NVarChar, 20)
                paramSection.Value = cboSection.Text
                cmd.Parameters.Add(paramSection)
            End If
            If cboGender.SelectedIndex >= 0 Then
                Dim paramGender As New SqlParameter("@Gender", SqlDbType.NVarChar, 20)
                paramGender.Value = cboGender.Text
                cmd.Parameters.Add(paramGender)
            End If
            If cboAccount.SelectedIndex >= 0 Then
                Dim paramAccount As New SqlParameter("@Account", SqlDbType.NVarChar, 20)
                paramAccount.Value = cboAccount.Text
                cmd.Parameters.Add(paramAccount)
            End If


            da.SelectCommand = cmd
            dt.Clear()
            da.Fill(dt)
            dgv1.DataSource = dt

        End Using
    End Sub

Can someone please help me to figure out what's wrong with my code. 有人可以帮我找出我的代码有什么问题吗? I just can't understand because I know the logic is almost the same. 我只是不明白,因为我知道逻辑几乎是相同的。 Thanks 谢谢

Are you saying that the account value is set to either active or inactive? 您是说帐户值设置为有效还是无效? If so, inactive accounts could be picked up because you are using 如果是这样,由于您使用的是不活跃的帐户,因此可以提取该帐户

AND Si.account LIKE COALESCE('%' + @Account + '%', si.account) 

You may want to try removing the leading '%' and using the following line instead: 您可能要尝试删除前导的'%'并改用以下行:

    AND Si.account LIKE COALESCE(@Account + '%', si.account) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM