Quantcast
Viewing all articles
Browse latest Browse all 18858

data doesn't appear in my combobox at first time when select value from listbox in WPF

Hi, I have WPF listbox which contains employees names as displayed data ,combobox for countries and combobox for cities  ,when I select data from this list I want to show employee 's country & city at this two comboboxes like that

The XAML Window:

<Window.Resources><!--get employee name and add it to list--><DataTemplate x:Key="EmpFullNameTemplate" ><TextBlock Text="{Binding EmpFullName}" FontSize="14" Foreground="Black" FontWeight="Bold" 
                       FontFamily="Arial"></TextBlock></DataTemplate></Window.Resources><Grid><Grid.RowDefinitions><RowDefinition Height="66*" /><RowDefinition Height="204*" /></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition Width="71*" /><ColumnDefinition Width="132*" /><ColumnDefinition Width="190*" /></Grid.ColumnDefinitions><!-- Emp Country--><TextBlock Name="blkEmpContries" Text=" الـبــلــد" Height="20" Margin="12,12,15,34" /><ComboBox Grid.Column="1" Name="cmbContries" Height="20" Margin="0,12,18,34" 
                  SelectionChanged="cmbContries_SelectionChanged" /><!-- Emp Country--><TextBlock Grid.Row="1" Name="blkEmpCities" Text=" الـمـحـافـظـة" Height="20" Margin="12,9,0,175" /><ComboBox Grid.Column="1" Grid.Row="1" Name="cmbEmpCities" Height="20" Margin="6,9,18,175" /><ListBox Grid.Column="2" Grid.RowSpan="2" HorizontalAlignment="Stretch" Margin="11,13,12,16" Name="lstEmployees" 
                          VerticalAlignment="Stretch" ItemsSource="{Binding Tables[0]}"  Background="Salmon"
                          ItemTemplate="{StaticResource EmpFullNameTemplate}" Foreground="Black"
                          ScrollViewer.VerticalScrollBarVisibility="Auto" 
                         BorderBrush="#FFAD7F30" SelectionChanged="lstEmployees_SelectionChanged" /></Grid>

Code behind:

  Int32 cntryID, crntEmpId, cityId;

        string ConStr = "Data Source=.;Initial Catalog=MenofiaHospital;User ID=mohamed;Password=ewies";

        #region get countries info
        // this is "" Stored Procedure
        // SELECT CountryID, CountryName FROM dbo.CountriesTbl
        private void contriList()
        {
            SqlConnection SqlCon = new SqlConnection(ConStr);
            // get all countries data      
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = SqlCon;
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "SelAllCoubtries";
            SqlCon.Open();
            SqlDataAdapter dap = new SqlDataAdapter();
            dap.SelectCommand = SqlCmd;
            DataSet ds = new DataSet();
            dap.Fill(ds);
            SqlCon.Close();
            bindContries(ds);
        }

        private void bindContries(DataSet dsGovs)
        {
            List<ContrisClass> cntrisList = new List<ContrisClass>();
            for (int i = 0; i < dsGovs.Tables[0].Rows.Count; i++)
            {
                cntrisList.Add(new ContrisClass
                                (Convert.ToInt32(dsGovs.Tables[0].Rows[i]["CountryID"]),
                                dsGovs.Tables[0].Rows[i]["CountryName"].ToString()));
            }
            dsGovs = null;
            cmbContries.ItemsSource = cntrisList;
            cmbContries.DisplayMemberPath = "CountryName";
            cmbContries.SelectedValuePath = "CountryID";
        }
        #endregion

        #region get cities name according to selected country ID
        // This is "SelAllCities" StoredProcedure
        // select CityID,CityName from CitiesTbl where CountryID = @CountryID order by CityName
        private void citiesList(int CountryID)
        {
            SqlConnection SqlCon = new SqlConnection(ConStr);
            // twons menu           
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = SqlCon;
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "SelAllCities";
            SqlCon.Open();
            SqlParameter ParCities = new SqlParameter();
            ParCities.Direction = ParameterDirection.Input;
            ParCities.ParameterName = "@CountryID";
            ParCities.SqlDbType = SqlDbType.Int;
            ParCities.Value = CountryID;
            SqlCmd.Parameters.Add(ParCities);
            SqlDataAdapter dap = new SqlDataAdapter();
            dap.SelectCommand = SqlCmd;
            DataSet ds = new DataSet();
            dap.Fill(ds);
            SqlCon.Close();
            bindTowns(ds);
        }

        private void bindTowns(DataSet dsTowns)
        {
            List<CityClass> townList = new List<CityClass>();
            for (int i = 0; i < dsTowns.Tables[0].Rows.Count; i++)
            {
                townList.Add(new CityClass
                             (Convert.ToInt32(dsTowns.Tables[0].Rows[i]["CityID"]),
                             dsTowns.Tables[0].Rows[i]["CityName"].ToString()));
            }
            dsTowns = null;
            cmbEmpCities.ItemsSource = townList;
            cmbEmpCities.DisplayMemberPath = "CityName";
            cmbEmpCities.SelectedValuePath = "CityID";
        }

        private void cmbContries_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            cntryID = Convert.ToInt32(this.cmbContries.SelectedValue);
            citiesList(cntryID);
        }
        #endregion

        #region get all employees 
        
        // this is "SelEmpsInfo" StoredProcedure from SQL Server
        // SELECT EmpID, EmpFullName FROM dbo.EmployeesTble 

        private void bindEmployeesInfo()
        {
            SqlConnection SqlCon = new SqlConnection(ConStr);
            // guests menu           
            SqlCommand SqlCmdAllEmpsInfo = new SqlCommand();
            SqlCmdAllEmpsInfo.Connection = SqlCon;
            SqlCmdAllEmpsInfo.CommandType = CommandType.StoredProcedure;
            SqlCmdAllEmpsInfo.CommandText = "SelEmpsInfo";
            SqlCon.Open();

            SqlDataAdapter dap1 = new SqlDataAdapter();
            dap1.SelectCommand = SqlCmdAllEmpsInfo;
            DataSet ds1 = new DataSet();
            dap1.Fill(ds1, "EmployeesTble");
            SqlCon.Close();
            lstEmployees.DataContext = ds1;
        }
        #endregion

       

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

        #region get Employee info according to selected value from listbox and bind it to comboboxes
        // this is "SelAllEmpInfo" StoredProcedure

        // SELECT dbo.EmployeesTble.CityID, dbo.CitiesTbl.CityName, dbo.CitiesTbl.CountryID, 
        // dbo.CountriesTbl.CountryName FROM  dbo.CitiesTbl INNER JOIN dbo.CountriesTbl ON 
        // dbo.CitiesTbl.CountryID = dbo.CountriesTbl.CountryID INNER JOIN dbo.EmployeesTble 
        // ON dbo.CitiesTbl.CityID = dbo.EmployeesTble.CityID where EmployeesTble.EmpID = @EmpID

        public ArrayList SelAllEmpInfo(Int32 EmpId)
        {
            SqlConnection SqlCon = new SqlConnection(ConStr);
            SqlCommand SqlCmd = new SqlCommand();
            SqlCmd.Connection = SqlCon;
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.CommandText = "SelAllEmpInfo";
            SqlCon.Open();
            SqlParameter ParEmpId = new SqlParameter();
            ParEmpId.Direction = ParameterDirection.Input;
            ParEmpId.ParameterName = "@EmpID";
            ParEmpId.SqlDbType = SqlDbType.BigInt;
            ParEmpId.Value = EmpId;
            SqlCmd.Parameters.Add(ParEmpId);

            SqlDataReader Dr = SqlCmd.ExecuteReader();
            ArrayList empList = new ArrayList();
            if (Dr.HasRows)
            {
                while (Dr.Read())
                {
                    empList.Add(Dr[0].ToString());
                    empList.Add(Dr[1].ToString());
                    empList.Add(Dr[2].ToString());
                    empList.Add(Dr[3].ToString());
                }
            }
            SqlCon.Close();
            return empList;
        }

        private void lstEmployees_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                DataRowView drv = lstEmployees.SelectedValue as DataRowView;

                crntEmpId = Convert.ToInt32(drv["EmpID"]);

                ArrayList empList = SelAllEmpInfo(crntEmpId);

                cmbEmpCities.DisplayMemberPath = empList[1].ToString();
                cmbEmpCities.Text = empList[1].ToString();
                cmbEmpCities.SelectedValuePath = empList[0].ToString();
                cityId = Convert.ToInt32(empList[0]);

                cmbContries.DisplayMemberPath = empList[3].ToString();
                cmbContries.Text = empList[3].ToString();
                cmbContries.SelectedValuePath = empList[2].ToString();
                contriList();
            }
            catch { }
        }

        #endregion
    }


    class ContrisClass
    {
        private int _ContryID = -1;
        private string _ContryName = "";

        public ContrisClass(int contryID, string contryName)
        {
            this._ContryID = contryID;
            this._ContryName = contryName;
        }

        public int CountryID
        {
            get { return _ContryID; }
            set { _ContryID = value; }
        }

        public string CountryName
        {
            get { return _ContryName; }
            set { _ContryName = value; }
        }
    }

    class CityClass
    {
        private int _CityID = -1;
        private string _CityName = "";

        public CityClass(int cityID, string CityName)
        {
            this._CityID = cityID;
            this._CityName = CityName;
        }
        public int CityID
        {
            get { return _CityID; }
            set { _CityID = value; }
        }
        public string CityName
        {
            get { return _CityName; }
            set { _CityName = value; }
        }
    }

my problem is when I select data from list box "employee name" nothing appear in both comboboxes , but in debug mode I see data retrieved from array list and binding to comboboxes.

any help please, thanks.


Viewing all articles
Browse latest Browse all 18858

Trending Articles