gpt4 book ai didi

c# - 更新绑定(bind)到 dataSet dataTable 的 listView,它从 SQL c# wpf 中查找数据

转载 作者:太空宇宙 更新时间:2023-11-03 17:03:40 28 4
gpt4 key购买 nike

尝试更新绑定(bind)到数据集的 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();
}
}

最佳答案

尝试刷新方法。重新绑定(bind)后。

关于c# - 更新绑定(bind)到 dataSet dataTable 的 listView,它从 SQL c# wpf 中查找数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13288467/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com