Thursday, December 25, 2014

WPF Search from ListView

In DAL use code like this:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Search Customer from listview
        public List<CustomerInfo> SearchCustomer(string searchValue)
        {
            dataContextObj = new JSMSEntities();

            List<CustomerInfo> customerInfoList = new List<CustomerInfo>();
            foreach (var p in (from c in dataContextObj.tbl_Customer 
                               where c.CustomerName.Contains(searchValue) || c.CustomerMobile.Contains(searchValue)
                               select c).Distinct())
            {
                CustomerInfo _customerInfoObj = new CustomerInfo();
                _customerInfoObj.Id = p.ID;
                _customerInfoObj.CustomerName = p.CustomerName;
                _customerInfoObj.CustomerAddress = p.CustomerAddress;
                _customerInfoObj.CustomerPhone = p.CustomerPhone;
                _customerInfoObj.CustomerMobile = p.CustomerMobile;
                _customerInfoObj.CustomerFax = p.CustomerFax;
                _customerInfoObj.CustomerEmail = p.CustomerEmail;
                _customerInfoObj.CustomerNote = p.CustomerNote;

                customerInfoList.Add(_customerInfoObj);
            }

            return customerInfoList;
        }

In BLL use code like this:

1
2
3
4
5
//BLL Search customer
        public List<CustomerInfo> SearchCustomer(string searchValue)
        {
            return customerInfoGetwayObj.SearchCustomer(searchValue);
        }

In xaml.cs file use code like this:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
//Search Customer
        private void txtCustomerSearch_TextChanged(object sender, TextChangedEventArgs e)
        {
            _customerInfoList = new List<CustomerInfo>();
            _customerInfoList = _customerInfManagerObj.SearchCustomer(txtCustomerSearch.Text);
            CustomerInfoListView.Items.Clear();

            if (_customerInfoList.Count > 0)
            {
                foreach (var item in _customerInfoList)
                {
                    CustomerInfoListView.Items.Add(item);
                }
            }
        }

Wednesday, December 24, 2014

WPF ListView Data Display

In DAL write code like this:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public List<SaleInfo> LoadAllSaleList()
        {
            dataContext = new JSMSEntities();
            List<SaleInfo> saleList = new List<SaleInfo>();

            foreach (var p in (from c in dataContext.tbl_Sale 
                               select new 
                               { 
                                   c.SaleDateTime, 
                                   c.InvoiceNumber, 
                                   c.tbl_Customer.CustomerName, c.TotalAmount, 
                                   c.TotalPay, c.TotalDue 
                               }).Distinct())
            {
                SaleInfo allsaleObj = new SaleInfo();

                allsaleObj.SaleDateTime = (DateTime)p.SaleDateTime;
                allsaleObj.InvoiceNumber = p.InvoiceNumber;
                allsaleObj.CustomerName = p.CustomerName;
                allsaleObj.TotalAmount = (int)p.TotalAmount;
                allsaleObj.TotalPay = (int)p.TotalPay;
                allsaleObj.TotalDue = (int)p.TotalDue;

                saleList.Add(allsaleObj);
            }

            return saleList;
        }

In BLL write code like this :

1
2
3
4
public List<SaleInfo> LoadAllSaleList()
        {
            return _saleGetwayObj.LoadAllSaleList();
        }


In XAML add ListView like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
        <ListView 
 HorizontalAlignment="Left" 
 Height="509" 
 Margin="10,52,0,0" 
 VerticalAlignment="Top" 
 Width="624"
 x:Name="SaleListView">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn DisplayMemberBinding="{Binding Id}" Header="" Width="0"/>
                    <GridViewColumn DisplayMemberBinding="{Binding SaleDateTime, StringFormat='yyyy-MM-dd'}" Header="Date" Width="120"/>
                    <GridViewColumn DisplayMemberBinding="{Binding InvoiceNumber}" Header="Invoice" Width="120"/>
                    <GridViewColumn DisplayMemberBinding="{Binding CustomerName}" Header="Name" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalAmount}" Header="Price" Width="90"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalPay}" Header="Total Pay" Width="100"/>
                    <GridViewColumn DisplayMemberBinding="{Binding TotalDue}" Header="Total Due" Width="90"/>
                </GridView>
            </ListView.View>
        </ListView>


In UI add reference of BLL and ENTITY, then write code like this:

1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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.Shapes;
using BLL;
using ENTITY;

namespace JSMS.UI
{
    /// <summary>
    /// Interaction logic for SaleReportUI.xaml
    /// </summary>
    public partial class SaleReportUI : Window
    {
        List<SaleInfo> _allSaleListObj = new List<SaleInfo>();
        SaleManager _saleManagerObj = new SaleManager();
        public SaleReportUI()
        {
            InitializeComponent();

            LoadAllSaleList();
        }

        private void LoadAllSaleList()
        {
            _allSaleListObj = new List<SaleInfo>();
            _allSaleListObj = _saleManagerObj.LoadAllSaleList();

            SaleListView.Items.Clear();

            if (_allSaleListObj.Count > 0)
            {
                foreach (var item in _allSaleListObj)
                {
                    SaleListView.Items.Add(item);
                }
            }

        }
    }
}


Tuesday, December 23, 2014

WPF XAML Horizontal Menu


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
     <Menu Height="26" Name="topMenu" VerticalAlignment="Top" Margin="0,0,0,-5" FontFamily="arial">
                <MenuItem Header="File" Name="FileMenu">
                    <MenuItem Header="Shop Setting"  Name="mnuShopSetting" Background="Transparent" Click="mnuShopSetting_Click"  />
                    <Separator />
                    <MenuItem Header="Exit" Name="mnuExit" Click="mnuExit_Click"/>
                </MenuItem>

                <MenuItem Header="Supplier" Name="SupplierMenu">
                    <MenuItem x:Name="mnuNewSupplier" Header="New Supplier" Click="mnuNewSupplier_Click"  />
                    <Separator />
                    <MenuItem Header="Supplier Status Report"   />
                </MenuItem>
                </MenuItem>
            </Menu>

WPF ListView StringFormat

In XAML follow string formating like this way:


1
<GridViewColumn DisplayMemberBinding="{Binding PurchaseDate, StringFormat='yyyy-MM-dd'}" Header="Date" Width="100"/>

C# WPF, Access Parent Window Method from Child Window

For access parent window method from child window, first make parent window method as PUBLIC, then follow below code for child window.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//Declear ParentWindow as GlobalPreview
        SupplierDueListUI globalPrewindow;

public SupplierDueCollectionUI(SupplierDueListUI prewindow)
        {
            InitializeComponent();
            globalPrewindow = prewindow; // Initial prewindow
        }

private void btnUpdatePayment_Click(object sender, RoutedEventArgs e)
        {
            PurchaseInfo purchaseInfoObj = new PurchaseInfo();

            purchaseInfoObj.Id = this._purchaseInfoObj.Id;
            purchaseInfoObj.ProductId = this._purchaseInfoObj.ProductId;
            purchaseInfoObj.SupplierId = this._purchaseInfoObj.SupplierId;
            purchaseInfoObj.TotalDue = Convert.ToInt32(txtTotalDue.Text);
            purchaseInfoObj.TotalPay = Convert.ToInt32(txtTotalPay.Text);

            if (purchaseInfoObj.TotalDue <= 0)
            {
                purchaseInfoObj.IsFullPaid = true;
            }

            _purchaseManagerObj.UpdatePurchasePayment(purchaseInfoObj);

            globalPrewindow.LoadAllPurchaseDueBill(); //Load Parent window method

            MessageBox.Show("Successfully Updated", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);

            this.Close();
        }

Saturday, December 20, 2014

C# Generate an Unique code

This method use DateTime Ticks for generate an Unique code.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Generate Unique code using DateTime
        static string uniqueCode()
        {
            string characters = "K13A56Z89I";
            string time = DateTime.UtcNow.Ticks.ToString();
            var code = "";
            for (var i = 0; i < characters.Length; i += 2)
            {
                if ((i + 2) <= time.Length)
                {
                    var number = int.Parse(time.Substring(i, 2));
                    if (number > characters.Length - 1)
                    {
                        var one = double.Parse(number.ToString().Substring(0, 1));
                        var two = double.Parse(number.ToString().Substring(1, 1));
                        code += characters[Convert.ToInt32(one)];
                        code += characters[Convert.ToInt32(two)];
                    }
                    else
                        code += characters[number];
                }
            }
            return code;
        }

C# Entity, Load Data from Child window to Parent window

In child window create an event and use code like this:

1
2
3
4
5
6
7
8
//Send data to Parent form
        private void CustomerInfoListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (CustomerInfoListView.SelectedIndex > -1)
            {
                this.DialogResult = true;
            }
        }

Now in Parent window create a button event for show child window and get data. Use code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
//Get data from child window and set it to parent window
        private void btnGet_Click(object sender, RoutedEventArgs e)
        {
            CustomerInfoUI customerUI = new CustomerInfoUI(); //Child window object

            if (customerUI.ShowDialog() == true) // Set condition for show child window and do some operation
            {
                CustomerInfo customerObj = new CustomerInfo(); //Child window entity object
                customerObj = customerUI.CustomerInfoListView.SelectedItem as CustomerInfo; //Load child window listview data to entity
                txtCustomerName.Text = customerObj.CustomerName; // set data to parent window text field
                txtCustomerId.Text = customerObj.Id.ToString();// set data to parent window text field
                this._customerInfoObj.Id = customerObj.Id; // set child window ID to parent window ID

                customerUI.Close(); //Close child window
            }
        }


C# with Entity, Send Data from Parent Form to Child Form

In Child Form Create an entity object as public and set all form field with that entity property. Like This code:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
//Declare Purchase Entity Object as Public
        public PurchaseInfo _purchaseInfoObj = new PurchaseInfo();


//Load Purchase Data from Parent Window to Child Form
        public void LoadDataFromDueList()
        {
            txtProductID.Text = this._purchaseInfoObj.ProductId.ToString();
            txtProductName.Text = this._purchaseInfoObj.ProductName;
            txtSupplierName.Text = this._purchaseInfoObj.SupplierName;
            txtTotalPurchaseAmount.Text = this._purchaseInfoObj.TotalPurchaseAmount.ToString();
            txtTotalDue.Text = this._purchaseInfoObj.TotalDue.ToString();
        }


Now in Parent Form, create an Event and write code like this:


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//Collect Data from Listview and Send it to child form
        private void MakePaymentContexMenu_Click(object sender, RoutedEventArgs e)
        {
            if (PurchaseDueListView.SelectedIndex > -1)
            {
                SupplierDueCollectionUI supplierDueCollectionUI = new SupplierDueCollectionUI();
                PurchaseInfo purchaseObj = new PurchaseInfo();
                purchaseObj = PurchaseDueListView.SelectedItem as PurchaseInfo; // select and load data from listview

                supplierDueCollectionUI._purchaseInfoObj = purchaseObj; // send it to public entity _purchaseInfoObj
                supplierDueCollectionUI.LoadDataFromDueList(); // Call data load method from child form
                supplierDueCollectionUI.ShowDialog(); // show child form
            }
        }