2013-05-05 159 views
0

嗨我想连接到Access数据库的WPF数据网格,但我不能得到任何更新,并且当我尝试调用数据时没有错误。WPF中的DataGrid

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) 
    { 

    } 



} 

}

它做了ComboBox完美,但没有数据网格不知道我在做什么错在这里,因为没有显示错误 请帮忙

回答

1

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

+0

其实我是一个完整的新手,当它来到这可以代码为我的例子与我的代码我真的很感激。 – 2013-05-05 08:47:40

+0

只需通过C#通知对象或INotification界面以及它的用途即可Google。你会在网上找到大量的例子。如果您使用实体框架而不是OleDbConnection来访问数据库,那么您甚至不必自己编写这些对象。 – Michael 2013-05-05 08:58:52

+0

http://stackoverflow.com/questions/11432488/how-to-use-entity-framework-for-ms-access-database – Michael 2013-05-05 09:01:54