Search This Blog

Saturday, January 28, 2012

WPF - Windows Presentation Foundation.

WPF - Windows Presentation Foundation

                 WPF is used to create windows based application in asp.net framework. We can give impressive, attractive and effective GUI to the windows based application with the help of WPF.
                 WPF is the technology which comes with .NET Framework 3.0.


VERSIONS

                 Microsoft has released five major WPF versions: 
                 WPF 3.0 (Nov 2006), 
                 WPF 3.5 (Nov 2007), 
                 WPF 3.5sp1 (Aug 2008), 
                 WPF 4 (April 2010), and 
                 WPF 4.5 (August 2012).
 The latest Version of WPF is WPF 4.5 which released on Aug,12 by Microsoft.

What WPF can do

A WPF interface can combine images, text, 2D and 3D graphics, and more.

The Ability for Developers and Designers to Work Together

Vector Graphics System for Harnessing the Power of Graphics Hardware Acceleration


Intelligent Layout makes Design Easier


Advanced Styling Capabilities for Creating Beautiful Interfaces


What is XAML???


XAML stands for eXtensible Application Markup Language—pronounced “zammel.” 

With this new markup, UIs can be defined without the need to program, very similar to creating 

an HTML web page. 

Like Windows Forms, you build WPF forms using the interactive designer to drag and drop 

items on the UI and customize in the properties box. 

But unlike Windows Forms applications where the designer generates code in C#, VB.NET to 

create controls on the form, in a WPF application, the interactive designer generates a XAML script.

When you run the program, the XAML compiler converts the XAML into instances of objects 

using the .NET Framework. 

This is unlike most other markup languages, which are typically an interpreted language without

 such a direct tie to a backing type system.

GUI PAGE HAVING XAML FILE


XAML File

<Window x:Class="gridview.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="442" Width="583" Loaded="Window_Loaded">
    <Grid Height="402" Width="558">
        <Grid.RowDefinitions>
            <RowDefinition Height="404*" />
            <RowDefinition Height="38*" />
            <RowDefinition Height="6*" />
        </Grid.RowDefinitions>
        <ListView Name="Gridview1" ItemsSource="{Binding}" Margin="12,241,0,7" Grid.RowSpan="2">
            <ListView.View>
                <GridView>

                    <!--<GridViewColumn Header="Username">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock ></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>-->
                    <GridViewColumn Header="ID" DisplayMemberBinding="{Binding Path=id}"></GridViewColumn>

                    <GridViewColumn Header="Username" DisplayMemberBinding="{Binding Path=username}"></GridViewColumn>

                    <GridViewColumn Header="Password" DisplayMemberBinding="{ Binding Path=password}"></GridViewColumn>
                    <GridViewColumn Header="Edit">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <Button Name="Edit" Content="Edit" Click="Edit_Click"></Button>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>
        <Label Content="Username" Height="28" HorizontalAlignment="Left" Margin="52,12,0,0" Name="label1" VerticalAlignment="Top" />
        <Label Content="Password" Height="28" HorizontalAlignment="Left" Margin="52,59,0,0" Name="label2" VerticalAlignment="Top" Width="63" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
        <TextBox Height="23" HorizontalAlignment="Left" Margin="149,0,0,278" Name="textBox2" VerticalAlignment="Bottom" Width="120" />
        <Button Content="Insert" Height="23" HorizontalAlignment="Left" Margin="149,111,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
        <Button Content="Update" Height="34" HorizontalAlignment="Left" Margin="63,166,0,0" Name="update" VerticalAlignment="Top" Width="100" Click="update_Click" />
        <Button Content="Delete" Height="34" Margin="231,166,227,0" Name="delete" VerticalAlignment="Top" Click="delete_Click" />
    </Grid>
</Window>


.CS FILE


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;
namespace gridview
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void fill_grid()
        {
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlDataAdapter sda1 = new SqlDataAdapter("select * from tbl_reg",con1);
            DataTable dt1 = new DataTable();
            sda1.Fill(dt1);
            //Gridview1.DataContext = dt1.DefaultView;
            Gridview1.DataContext = dt1.DefaultView;
                //Gridview1.DataContext = dt1.DefaultView;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            fill_grid();
        }

       
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "")
            {
                SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
                SqlCommand cmd1 = new SqlCommand("insert into tbl_reg values('" + textBox1.Text + "','" + textBox2.Text + "')", con1);
                con1.Open();
                cmd1.ExecuteNonQuery();
                con1.Close();
                fill_grid();
                MessageBox.Show("Record is Successfully Inserted into the Database");
            }
            else
            {
                MessageBox.Show("Please Enter The Username and Password");
            }
        }

       

        private void Edit_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            textBox1.Text = drv.Row[1].ToString();
            textBox2.Text = drv.Row[2].ToString();
             
        }

        private void update_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("update tbl_reg set username='" + textBox1.Text + "',password='" + textBox2.Text + "'  where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record is Successfully Updated !!!");

        }

        private void delete_Click(object sender, RoutedEventArgs e)
        {
            DataRowView drv = (DataRowView)Gridview1.SelectedItem;
            string id1 = drv.Row[0].ToString();
            SqlConnection con1 = new SqlConnection("Data Source=TOPS17;Initial Catalog=TestGridview;Integrated Security=True");
            SqlCommand cmd1 = new SqlCommand("delete from tbl_reg where id='" + id1 + "'", con1);
            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
            fill_grid();
            MessageBox.Show("Record Successfully Deleted from the Database");

        }

   
    }
}

1 comment:

  1. If You are interested for Learning WPF Technologies or Asp.net Project Training then visit website:
    www.vataliyatuitionclasses.com

    ReplyDelete