0

我有一个数据库表,但架构更改太频繁,无法将列硬编码到应用程序(WPF 应用程序)中。如何将表中的所有行显示到 DataGrid 中并拥有它,以便网格中的行可编辑?

从数据库中获取数据并更新我可以做的数据库,但是考虑到我不能用各种列提前作为属性 - 因为列变化非常频繁。

有没有人遇到过这个问题?

谢谢

4

2 回答 2

0

我已经使用几个为 ListView 创建 GridView 的转换器完成了这项工作。我将列名和列数据(格式化为字符串)读入数组,并使用了这些转换器:

<ValueConversion(GetType(String()), GetType(GridView))>
Public Class TableToGridViewConverter
    Implements IValueConverter

Private Shared ReadOnly ItemConverter As New ArrayToItemConverter
Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim header() As String = TryCast(value, String())
    Dim result As New GridView
    If header IsNot Nothing Then
        For i As Integer = 0 To header.Length - 1
            result.Columns.Add(New GridViewColumn() With {.Header = header(i), .DisplayMemberBinding = New Binding() With {.Converter = ItemConverter, .ConverterParameter = i}})
        Next
    End If
    Return result
End Function

Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    Throw New NotSupportedException
End Function
End Class

<ValueConversion(GetType(String()), GetType(String))>
Public Class ArrayToItemConverter
Implements IValueConverter

Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim result As String = String.Empty
    If TypeOf value Is String() AndAlso IsNumeric(parameter) Then
        Dim values() As String = value
        Dim index As Integer = CInt(parameter)
        If index >= 0 And index < values.Length Then
            result = values(index)
        End If
    End If
    Return result
End Function

Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    Throw New NotSupportedException
End Function
End Class

诀窍是为数组中的每一列创建一个 GridViewColumn,并使用一个转换器,该转换器绑定到一个值数组,并使用列索引作为 ConverterParameter 为相应列选择值。你会像这样使用它:

                    <ListView x:Name="TableDataListView" View="{Binding Path=DisplayTable.Header, Converter={StaticResource tableToGridView}}" ItemsSource="{Binding Path=DisplayRows}">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
                            <Setter Property="VerticalContentAlignment" Value="Top"/>        
                        </Style>
                    </ListView.ItemContainerStyle>
                </ListView>

其中绑定属性 DisplayTable.Header 和 DisplayRows 将分别包含具有列名的字符串数组和包含实际数据的字符串数组数组。

于 2012-11-24T15:17:09.050 回答
0

如果它符合您的要求,可能会有一种不同且更简单的方法:只需使用 System.Data.DataTable 来保存您的数据并在 DataGrid 上设置 AutoGenerateColumns="True"。就我而言,我需要数组......

于 2012-11-24T15:23:46.850 回答