如何将ListView
转换为DataTable
?
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
生成项目时,如何将DataTable
转换为ListView
?
答案 0 :(得分: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);
}
答案 1 :(得分:0)
var table = List_bill.DataContext as DataTable;
答案 2 :(得分:-1)
试试这个:
var table = listViewUserHUD.DataSource as DataTable