尝试更新绑定到数据集的 listView 时,我遇到了一些奇怪的问题。
数据集从 SQL 获取信息,需要每 5 分钟自动更新一次。目前我正在使用 backgroudWorker 获取新信息并将其插入数据集中。
然后在 RunWorkerCompleted 事件上我这样做
listView1.DataContex = dataSet.Tables["table"].DefaultView
DoWork 事件包含类似这样的内容
dataSet.Clear();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT [ContainerID] ,[UserID], [Spur], [Wave] ,[Date] FROM [ScannedItems] " +
"WHERE [Message] LIKE '%Success%'" +
"UNION ALL " +
"SELECT [ContainerID] ,[UserID], [Spur], [Wave] ,[Date] FROM [Workstation].[dbo].[AllSpurs] " +
"WHERE [Message] LIKE '%" + "Success" + "' ORDER BY [Date] DESC", connection);
DataTableMapping mapping = adapter.TableMappings.Add("Table", "Packages Scanned");
mapping.ColumnMappings.Add("ContainerID", "Cont");
mapping.ColumnMappings.Add("UserID", "User");
mapping.ColumnMappings.Add("Spur", "Spur");
mapping.ColumnMappings.Add("Wave", "Wave");
mapping.ColumnMappings.Add("Date", "Date");
connection.Open();
adapter.FillSchema(dataSet, SchemaType.Mapped);
adapter.Fill(dataSet);
connection.Close();
这在我调用 worker.RunWorkerAsync(); 时有效 在启动时。
但是,当计时器调用它时,表会变为空白(所有数据都已删除)。如果我尝试在 listVIew 中滚动,我会收到错误消息
Object reference not set to the instance of an object
但是,如果我单击其中一列的标题,则新数据将变得可见并且可以滚动。
谁能让我知道我做错了什么。
这是 listView 的 XAML
<ListView ItemContainerStyle="{StaticResource cnListView}" ItemsSource="{Binding Path=Table}" Margin="7,5,31,7" Name="listView2" SelectionMode="Multiple" util:GridViewSort.AutoSort="True" LostFocus="LostFocus">
<ListView.View>
<GridView x:Name="gridView1" AllowsColumnReorder="False" ColumnHeaderTemplate="{StaticResource cnDataViewHeader}">
<GridViewColumn DisplayMemberBinding="{Binding Path=Cont}" util:GridViewSort.PropertyName="Cont" Header="ContainerID" Width="350" />
<GridViewColumn DisplayMemberBinding="{Binding Path=User}" util:GridViewSort.PropertyName="User" Header="User" Width="200" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Spur}" util:GridViewSort.PropertyName="Spur" Header="Spur" Width="120" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Wave, StringFormat='dd-MM-yyyy HH:mm:ss'}" util:GridViewSort.PropertyName="Wave" Header="Wave" Width="250" />
<GridViewColumn DisplayMemberBinding="{Binding Path=Date, StringFormat='dd-MM-yyyy HH:mm:ss'}" util:GridViewSort.PropertyName="Date" Header="Time Scanned" Width="250" />
</GridView>
</ListView.View>
</ListView>
太感谢了
较小的项目完整代码:
XAML
<Window x:Class="backWorkerTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="502" Width="989">
<Grid>
<ListView ItemsSource="{Binding MeetCollection}" Height="340" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listView1" VerticalAlignment="Top" Width="914">
<ListView.View>
<GridView>
<GridViewColumn Header="Test" DisplayMemberBinding="{Binding Path=Test}"/>
<GridViewColumn Header="Test2" DisplayMemberBinding="{Binding Path=Test2}"/>
</GridView>
</ListView.View>
</ListView>
<Button Content="Button" Height="63" Name="button1" Width="288" Click="button1_Click" Margin="368,388,311,12" />
</Grid>
C#
namespace backWorkerTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
DataSet dataSet = new DataSet();
BackgroundWorker backWorker = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
dataSet.Tables.Add("Stores");
backWorker.DoWork += new DoWorkEventHandler(backWorker_DoWork);
backWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backWorker_RunWorkerCompleted);
backWorker.RunWorkerAsync();
}
void GetData()
{
using (SqlConnection con = new SqlConnection(@"Data Source=CN-LT08\Nick;Initial Catalog=SorterAdmin;" +
"User=sa; Password=Altrincham09"))
{
SqlDataAdapter sql = new SqlDataAdapter("SELECT [Store Number], [Store Name] FROM [Store_Info]", con);
DataTableMapping mapping = sql.TableMappings.Add("Table", "Stores");
mapping.ColumnMappings.Add("Store Number", "Test");
mapping.ColumnMappings.Add("Store Name", "Test2");
con.Open();
sql.FillSchema(dataSet, SchemaType.Mapped);
sql.Fill(dataSet);
con.Close();
}
}
private void button1_Click(object sender, RoutedEventArgs e)
{
backWorker.RunWorkerAsync();
}
private void backWorker_DoWork(object sender, DoWorkEventArgs e)
{
dataSet.Clear();
GetData();
}
private void backWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
listView1.ItemsSource = dataSet.Tables["Stores"].DefaultView;
listView1.UpdateLayout();
}
}
}