WPF中的数据网格

本文关键字:数据网 网格 数据 WPF | 更新日期: 2023-09-27 18:12:05

嗨,我正试图将WPF数据网格连接到Access数据库,但当我尝试调用数据时,我无法获得任何更新和错误。

XML代码
<Window x:Class="SRT.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="627.49" Width="930.777">
<Grid Margin="0,0,-8,11">
    <TabControl HorizontalAlignment="Left" Height="583" VerticalAlignment="Top" Width="921">
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5" Margin="0,0,6,0">
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="11,178,0,0" VerticalAlignment="Top" Width="128" Name="date"/>
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="146,178,0,0" VerticalAlignment="Top" Width="201" Name="Agent_Name"/>
                <ComboBox HorizontalAlignment="Left" Height="28" Margin="358,178,0,0" VerticalAlignment="Top" Width="165" Name="Status"/>
                <Button Content="Refresh" HorizontalAlignment="Left" Height="28" Margin="528,178,0,0" VerticalAlignment="Top" Width="110" x:Name="Refresh" Click="Refresh_Click"/>
                <Button Content="Get Data" HorizontalAlignment="Left" Height="28" Margin="643,178,0,0" VerticalAlignment="Top" Width="110" x:Name="getdata" Click="getdata_Click"/>
                <Label Content="Date" HorizontalAlignment="Left" Height="27" Margin="11,146,0,0" VerticalAlignment="Top" Width="128"/>
                <Label Content="SRT Agent Name" HorizontalAlignment="Left" Height="27" Margin="146,146,0,0" VerticalAlignment="Top" Width="201"/>
                <Label Content="Case Status" HorizontalAlignment="Left" Height="27" Margin="358,146,0,0" VerticalAlignment="Top" Width="165"/>
                <Frame Content="Update" HorizontalAlignment="Left" Height="136" Margin="11,10,0,0" VerticalAlignment="Top" Width="743"/>
                <TextBox HorizontalAlignment="Left" Height="30" Margin="25,79,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="97"/>
                <ComboBox HorizontalAlignment="Left" Margin="127,79,0,0" VerticalAlignment="Top" Width="162" Height="30"/>
                <ComboBox HorizontalAlignment="Left" Height="30" Margin="294,79,0,0" VerticalAlignment="Top" Width="121"/>
                <Button Content="Update" HorizontalAlignment="Left" Height="30" Margin="420,79,0,0" VerticalAlignment="Top" Width="96"/>
                <Label Content="ID Number" HorizontalAlignment="Left" Height="27" Margin="25,47,0,0" VerticalAlignment="Top" Width="97"/>
                <Label Content="Updated Status" HorizontalAlignment="Left" Height="27" Margin="127,47,0,0" VerticalAlignment="Top" Width="162"/>
                <Label Content="Callback Attempts" HorizontalAlignment="Left" Height="27" Margin="294,47,0,0" VerticalAlignment="Top" Width="121"/>
                <TextBox HorizontalAlignment="Left" Height="27" Margin="125,114,0,0" TextWrapping="Wrap" Text="Add Database Path here" VerticalAlignment="Top" Width="391" Name="path"/>
                <Label Content="Path" HorizontalAlignment="Left" Height="27" Margin="23,114,0,0" VerticalAlignment="Top" Width="97"/>
                <DataGrid HorizontalAlignment="Left" Height="158" Margin="5,217,0,0" VerticalAlignment="Top" Width="890" Name="alldata" AutoGenerateColumns="True" SelectionChanged="alldata_SelectionChanged"/>
            </Grid>
        </TabItem>
        <TabItem Header="TabItem">
            <Grid Background="#FFE5E5E5"/>
        </TabItem>
    </TabControl>
</Grid>

c#代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.OleDb;
using System.Data.Common;
namespace SRT
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
    private void Refresh_Click(object sender, RoutedEventArgs e)
    {
        OleDbConnection con2 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Sumeet''Desktop''Sitel Project''Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd2 = new OleDbCommand();
        OleDbDataAdapter da2 = new OleDbDataAdapter();
        DataTable dt2 = new DataTable();
        String fselect2 = null;
        fselect2 = "Select DISTINCT Date from SRT";
        cmd2 = new OleDbCommand(fselect2, con2);
        da2 = new OleDbDataAdapter(cmd2);
        da2.Fill(dt2);
        date.ItemsSource = dt2.DefaultView;
        date.DisplayMemberPath = dt2.Columns["Date"].ToString();
        con2.Close();
        OleDbConnection con3 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Sumeet''Desktop''Sitel Project''Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd3 = new OleDbCommand();
        OleDbDataAdapter da3 = new OleDbDataAdapter();
        DataTable dt3 = new DataTable();
        String fselect3 = null;
        fselect3 = "Select DISTINCT Agent_Name from SRT";
        cmd3 = new OleDbCommand(fselect3, con3);
        da3 = new OleDbDataAdapter(cmd3);
        da3.Fill(dt3);
        Agent_Name.ItemsSource = dt3.DefaultView;
        Agent_Name.DisplayMemberPath = dt3.Columns["Agent_Name"].ToString();
        con3.Close();
        OleDbConnection con4 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Sumeet''Desktop''Sitel Project''Support_Tracker.accdb;Persist Security Info=False;");
        OleDbCommand cmd4 = new OleDbCommand();
        OleDbDataAdapter da4 = new OleDbDataAdapter();
        DataTable dt4 = new DataTable();
        String fselect4 = null;
        fselect4 = "Select DISTINCT Status from SRT";
        cmd4 = new OleDbCommand(fselect4, con4);
        da4 = new OleDbDataAdapter(cmd4);
        da4.Fill(dt4);
        Status.ItemsSource = dt4.DefaultView;
        Status.DisplayMemberPath = dt4.Columns["Status"].ToString();
        con4.Close();

    }
    private void getdata_Click(object sender, RoutedEventArgs e)
    {
        OleDbConnection con1 = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:''Users''Sumeet''Desktop''Sitel Project''Support_Tracker.accdb;");
        OleDbCommand cmd1 = new OleDbCommand();
        OleDbDataAdapter da1 = new OleDbDataAdapter();
        DataTable dt1 = new DataTable();
        String fselect1 = null;
        fselect1 = "Select * from Org";
        cmd1 = new OleDbCommand(fselect1, con1);
        da1 = new OleDbDataAdapter(cmd1);
        da1.Fill(dt1);
        alldata.DataContext = dt1.DefaultView;  
    }
    private void alldata_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    }

}

}

它完美地完成了组合框,但不是数据网格,不确定我在这里做错了什么,因为没有显示错误请帮助

WPF中的数据网格

我的第一个想法是:将数据封装到对象中,从而实现INotification接口。然后把对象放到一个可观察集合中。然后使用此集合作为数据源。