如何从DataGridBoundColumn获取属性类型

时间:2014-04-23 02:30:05

标签: wpf vb.net

我正在尝试获取数据绑定列(数据网格; SQL表)的列(属性)类型,而对于我的生活,我无法按照我希望的方式工作。

请注意此解决方案是在WPF中,而不是Windows窗体!

我试过这个没有用:

       Dim s As String
    Dim value As String = tbFilter.Text.ToString()
    Try

        For Each column As DataGridBoundColumn In DG1.Columns
            If TypeOf column.Header Is String Then
                s = s + "[" + column.Header.ToString + "] LIKE '*" + value + "*' OR "
            End If
        Next
        s = s.Substring(0, s.Length - 4)

    Catch ex As Exception

    End Try

我接受字符串并将其添加为自定义过滤器,如下所示:

        If tbFilter.Text.Length > 0 Then
        Try
            view = CollectionViewSource.GetDefaultView(DG1.ItemsSource)
            view.CustomFilter = s
            DG1.ItemsSource = view
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    Else
        Try
            view.CustomFilter = Nothing
        Catch ex As Exception

        End Try
    End If

我得到的错误是:无法在System.Int32和System.String上执行'Like'操作。

有谁知道我怎样才能确保我只连接字符串列类型?

以下是我使用Windows窗体实现它的方法:

        Dim s As String = ""
    Dim value As String = tbPackageFilter.Text.ToString
    Try
        For Each column As DataGridViewColumn In dgPackage.Columns
            If column.ValueType.Name = "String" Then
                s = s + "[" + column.Name.ToString + "] LIKE '*" + value + "*' OR "
            End If
        Next
        s = s.Substring(0, s.Length - 4)
    Catch ex As Exception

    End Try

在WPF中我不能使用DataGridViewColumn,而列(DataGridBoundColumn)没有名为ValueType的属性。

1 个答案:

答案 0 :(得分:0)

我能够弄清楚:

        Try

        For Each col As DataColumn In mydatasetuser.Tables("MyTable").Columns

            Dim type = col.DataType
            If type.Name = "String" Then
                s = s + "[" + col.ToString + "] LIKE '*" + value + "*' OR "
            End If

        Next
        s = s.Substring(0, s.Length - 4)

    Catch ex As Exception

    End Try

所以,基本上不是查看DG1.Columns而是查看了我的数据源,它是:

mydatasetuser.Tables("MyTable").Columns

很高兴这已经过了我!

谢谢你们!