Tuesday, 3 July 2012

WPF Combobox binding with using class and with out using class

XAML:



<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfApplication1"
        xmlns:global="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
   
    <Window.Resources>
        <ResourceDictionary>
            <ObjectDataProvider x:Key="CptCodeModifiersData" ObjectType="{x:Type local:ProgressNoteCharges}" MethodName="GetApplicationCptCodeModifiers"/>

        </ResourceDictionary>

    </Window.Resources>
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource CptCodeModifiersData}}"  DisplayMemberPath="deptid" SelectedValuePath="deptid" Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"  />
    </Grid>
</Window>


Class:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace WpfApplication1
{
    class ProgressNoteCharges
    {
        public DataView GetApplicationCptCodeModifiers()
        {
            SqlConnection con = new SqlConnection("Data Source=NGV33060;Initial Catalog=simplewpf;Integrated Security=True;Pooling=False");
            SqlCommand cmd = new SqlCommand("select * from emp", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataView dv = ds.Tables[0].AsDataView();
            return dv;


        }
    }
}


--------------------------------------------------------------------------------------------------------


XAML:



<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
 
    <Grid>
        <ComboBox Height="23" HorizontalAlignment="Left"  Margin="10,10,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120"  />
    </Grid>
</Window>


Code behind:



private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=NGV33060;Initial Catalog=simplewpf;Integrated Security=True;Pooling=False");
            SqlCommand cmd = new SqlCommand("select * from emp", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            //ProgressNoteCharges pnc = new ProgressNoteCharges();
            //DataView dv = pnc.GetApplicationCptCodeModifiers();
            //comboBox1.ItemsSource = dv;
            comboBox1.ItemsSource = ds.Tables[0].DefaultView;
            comboBox1.DisplayMemberPath = "deptid";
            comboBox1.SelectedValuePath = "deptid";


        }







No comments:

Post a Comment