0

如何转换ListViewDataTable

XAML

<ListView x:Name="listViewUserHUD" ItemsSource="{Binding CollectionUserData}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" IsSynchronizedWithCurrentItem="True" util:GridViewSort.AutoSort="True" Margin="16,59,15,61">
    <ListView.View>
        <GridView>
            <GridViewColumn DisplayMemberBinding="{Binding FileNumber}" Header="FileNumber" Width="125" util:GridViewSort.PropertyName="FileNumber"  />
            <GridViewColumn DisplayMemberBinding="{Binding ShiftDate}" Header="ShiftDate" Width="125" util:GridViewSort.PropertyName="ShiftDate"/>
            <GridViewColumn DisplayMemberBinding="{Binding TimeCreated}" Header="TimeCreated" Width="125" util:GridViewSort.PropertyName="TimeCreated" />
            <GridViewColumn DisplayMemberBinding="{Binding Remarks}" Header="Remarks" Width="350" util:GridViewSort.PropertyName="Remarks" />
        </GridView>
    </ListView.View>
</ListView>

代码不起作用

var listView1 = new ListView();

DataTable table = new DataTable();

foreach (ListViewItem item in listView1.Items)
{
    table.Columns.Add(item.ToString());
    foreach (var it in item.SubItems)
    table.Rows.Add(it.ToString());
}

这是我现在拥有的代码,但我正在尝试这样做。每次生成项目时如何转换ListView为?DataTableListView

4

3 回答 3

1

最好从您的数据源对象中创建表格:

// Create the `DataTable` structure according to your data source
DataTable table = new DataTable();
table.Columns.Add("FileNumber", typeof(int));
table.Columns.Add("ShiftDate", typeof(DateTime));
table.Columns.Add("TimeCreated", typeof(DateTime));
table.Columns.Add("Remarks", typeof(string));

// Iterate through data source object and fill the table
foreach (var item in CollectionUserData)
{
    table.Rows.Add(item.FileNumber, item.ShiftDate, item.TimeCreated, item.Remarks);
}
于 2013-03-27T07:22:32.510 回答
0

尝试这个:

var table = listViewUserHUD.DataSource as DataTable
于 2013-03-27T07:14:54.193 回答
0
 var table = List_bill.DataContext as DataTable;
于 2019-07-31T06:09:27.843 回答