VB.NET SQL过滤器不显示NULL

时间:2015-01-15 17:27:22

标签: sql database vb.net datagridview

    Sub KlantenFilterZoeken(ByVal DataGridI As DataGridView, ByVal Tbl As String, ByVal NaamStraatHuisnummerBusnummerPostcodePlaatsTelefoon As String, ByVal DBCON As String)
    Dim Delen() As String = NaamStraatHuisnummerBusnummerPostcodePlaatsTelefoon.Split("|")

    Dim objConnection As New SqlConnection(DBCON)
    Dim objDt As New DataTable
    DataGridI.DataSource = Nothing
    objConnection.Open()
    Dim sSQL As String = "SELECT * FROM " & Tbl & " Where " & "KlantID Like @KlantID AND naam Like @naam AND straat Like @straat AND huisnummer Like @huisnummer AND postcode LIKE @postcode AND plaats LIKE @plaats AND telefoon LIKE @telefoon"
    Dim objCommand As SqlCommand = New SqlCommand(sSQL, objConnection)
    'Parameters
    objCommand.Parameters.AddWithValue("@KlantID", "%" & Delen(0) & "%")
    objCommand.Parameters.AddWithValue("@naam", "%" & Delen(1) & "%")
    objCommand.Parameters.AddWithValue("@straat", "%" & Delen(2) & "%")
    objCommand.Parameters.AddWithValue("@huisnummer", "%" & Delen(3) & "%")
    objCommand.Parameters.AddWithValue("@postcode", "%" & Delen(4) & "%")
    objCommand.Parameters.AddWithValue("@plaats", "%" & Delen(5) & "%")
    objCommand.Parameters.AddWithValue("@telefoon", "%" & Delen(6) & "%")

    Dim objAdapter = New SqlDataAdapter(objCommand)
    Dim objAdap As SqlDataAdapter = New SqlDataAdapter(sSQL, objConnection)
    objAdapter.Fill(objDt)
    DataGridI.DataSource = objDt
    DataGridI.Columns(0).Visible = True
    objConnection.Close()
    objConnection.Dispose()
End Sub

这是我的过滤数据网格视图的代码,它有效,但问题是这个代码“隐藏”了值为NULL的电话号码,我不希望这样:

http://i.stack.imgur.com/VpXra.png

如何解决此问题

1 个答案:

答案 0 :(得分:0)

我不确定你想做什么。但是如果你试图避免空单元格,你可以做这样的事情。

Dim objAdapter = New SqlDataAdapter(objCommand)
Dim objAdap As SqlDataAdapter = New SqlDataAdapter(sSQL, objConnection
objAdapter.Fill(objDt)

Dim i As Integer = objDt.Rows.Count
For i = 0 To objDt.Rows.Count Step 1
    If (String.IsNullOrEmpty(objDt.Rows(i)("telefoon"))) Then
        Dim phValue = "NULL"
        objDt.Rows(i)("telefoon") = phValue
    End If
Next

DataGridI.DataSource = objDt
DataGridI.Columns(0).Visible = True
objConnection.Close()
objConnection.Dispose()