Header

Saturday, 5 November 2016

ComboBox Events And Dynamic Data Binding in WPF - Part - 13

XML To Create the Comboboxes

<Canvas>
        <StackPanel Orientation="Horizontal" Height="50" Canvas.Left="130" Canvas.Top="118">
            <TextBlock Margin="0,0,20,0">Country</TextBlock>
            <ComboBox Name="cmbCountry" Width="200" Height="30" SelectionChanged="cmbCountry_SelectionChanged"
                      VerticalAlignment="Top" DisplayMemberPath="Name" SelectedValuePath="Id" SelectedValue="{Binding Id}">
            </ComboBox>
        </StackPanel>
        <StackPanel Orientation="Horizontal" Height="50" Canvas.Left="130" Canvas.Top="212">
            <TextBlock Margin="0,0,30,0">State</TextBlock>
            <ComboBox Name="cmbStates" Width="200" Height="30" VerticalAlignment="Top" DisplayMemberPath="Name" SelectedValuePath="Id">
            </ComboBox>
        </StackPanel>
    </Canvas>

Methods to fetch the data from MSSQL using Entity Framework

 void FillCountries()
        {
            WpfTutorialsEntities obj = new WpfTutorialsEntities();
            List<Country> lst = obj.Countries.ToList();
            cmbCountry.ItemsSource = lst;
        }

        private void cmbCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int CountryId = Convert.ToInt32(cmbCountry.SelectedValue);
            WpfTutorialsEntities obj = new WpfTutorialsEntities();
            List<State> lst = obj.States.Where(x => x.CountryId == CountryId).ToList();
            cmbStates.ItemsSource = lst;
        }



MSSQL Script To Create the Tables and Insert the Data

USE [WpfTutorials]
GO
/****** Object:  Table [dbo].[Countries]    Script Date: 11/05/2016 23:13:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Countries](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](256) NOT NULL,
PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[Countries] ON
INSERT [dbo].[Countries] ([Id], [Name]) VALUES (1, N'USA')
INSERT [dbo].[Countries] ([Id], [Name]) VALUES (2, N'UK')
INSERT [dbo].[Countries] ([Id], [Name]) VALUES (3, N'India')
SET IDENTITY_INSERT [dbo].[Countries] OFF
/****** Object:  Table [dbo].[States]    Script Date: 11/05/2016 23:13:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[States](
[Id] [int] IDENTITY(1,1) NOT NULL,
[CountryId] [int] NOT NULL,
[Name] [varchar](256) NOT NULL,
PRIMARY KEY CLUSTERED 
(
[Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[States] ON
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (1, 1, N'Alabama')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (2, 1, N'Alaska')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (3, 2, N'Lancashire')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (4, 2, N'London')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (5, 3, N'Goa')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (6, 3, N'Delhi')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (7, 3, N'Rajasthan')
INSERT [dbo].[States] ([Id], [CountryId], [Name]) VALUES (8, 3, N'Gujrat')
SET IDENTITY_INSERT [dbo].[States] OFF
/****** Object:  ForeignKey [FK__States__Name__0519C6AF]    Script Date: 11/05/2016 23:13:59 ******/
ALTER TABLE [dbo].[States]  WITH CHECK ADD FOREIGN KEY([CountryId])
REFERENCES [dbo].[Countries] ([Id])
GO


Note : To create the Entity Data Model please watch the Video Tutorial. It will guide you step by step to create the EDMX file.


No comments:

Post a Comment