Uh ne znam sta da uradim konkretno... Evo i xaml i xaml.cs koda, pa ako neko moze da napise gde treba izmena, i kakva, da bi mi ovo konacno proradilo.
Imam zadatak da se kreira WPF aplikacija (C#) za aukcijsku prodaju. Napravila sam SQL bazu i imam u aplikaciji listbox koji prikazuje sve aukcije i textbox koji pokazuje cenu odabrane aukcije. To je nazalost sve sto sam sama uspela..
Potrebno je jos:
1) da ubacim neki tajmer koji ce u intervalu od jedne sekunde vaditi iz baze sve aktuelne aukcije i prikazati ih u listboxu i
2) kada korisnik klikne na neku stavku iz liste, da se poveca cena proizvoda na koji je kliknuo.
Evo sta je gotovo za sad:
MainWindow.xaml
Code:
<Window x:Class="WpfApplication8.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="388" Width="538" >
<Grid Height="199">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ListView IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" Height="206" HorizontalAlignment="Left" Margin="20,-19,0,0" Name="listBox1" VerticalAlignment="Top" Width="479" Grid.ColumnSpan="2" >
<ListView.View>
<GridView ColumnHeaderToolTip="Aukcije">
<GridViewColumn DisplayMemberBinding="{Binding Path=NazivAukcije}" Header="NazivAukcije" Width="300"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Cena1}" Header="Cena" Width="150 "/>
</GridView>
</ListView.View>
</ListView>
<TextBlock Height="32" HorizontalAlignment="Left" Margin="-1,-72,0,0" Name="textBlock1" Text="Aukcijska prodaja" VerticalAlignment="Top" Width="517" Grid.ColumnSpan="2" TextAlignment="Center" FontFamily="Verdana" FontSize="18" FontWeight="SemiBold" Foreground="#FF137CF5" />
</Grid>
</Window>
i MainWindow.xaml.cs
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.SqlClient;
using System.Timers;
using System.Windows.Threading;
namespace WpfApplication8
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataTable aukcijeTable = new DataTable();
SqlConnection conn = new SqlConnection(@"Data Source=\.JOVANA;Initial Catalog=aukcije;Integrated Security=True");
SqlDataAdapter aukcDa = new SqlDataAdapter("select * from aukcije", conn);
aukcDa.Fill(aukcijeTable);
listBox1.DataContext = aukcijeTable;
}
private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =( @"Data Source=\.JOVANA;Initial Catalog=aukcije;Integrated Security=True");
SqlCommand command = conn.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "uspPovecajCenuArtiklaZaJedan";
command.Parameters.AddWithValue("@ArtikLIDe", listBox1.SelectedValue);
conn.Open();
command.ExecuteNonQuery();
}
}
/* Javlja se greska -An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll
Additional information: No mapping exists from object type System.Data.DataRowView to a known managed provider native type.
Uradjeno je ovo gore preko sql uskladistene procedure. Dakle klikom na selektovani red povecava se cena.
Treba negde da povezem listbox1.SelectedValue verovatno sa ValueMember, ali gde i kako? :( */
private void DispatcherTimer(object sender, RoutedEventArgs e)
{
DispatcherTimer dispatcherTimer = new DispatcherTimer();
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 1);
dispatcherTimer.Start();
}
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(aukcDa);
aukcDa.Update(aukcijeTable);
Console.WriteLine("Aukcije updated successfully");
Console.ReadLine();
}
public SqlDataAdapter aukcDa { get; set; } /*Jel ok ovo? Jer mi nije prepoznavao aukcDa i aukcijeTable,
pa mi ovo ponudio da generise. */
public DataTable aukcijeTable { get; set; }
}
}