private void DeleteButton2_Click(object sender, RoutedEventArgs e)
        {
            MessageBoxResult result;

            result = MessageBox.Show("Вы действительно хотите удалить эту запись?", "Удаление", MessageBoxButton.YesNo, MessageBoxImage.Question);

            if (result == MessageBoxResult.Yes)
            {
                using (FContext db = new FContext())
                {
                    if (IncomeGrid.SelectedItems.Count > 0)
                    {
                        for (int i = 0; i < IncomeGrid.SelectedItems.Count; i++)
                        {
                            Income1 income = IncomeGrid.SelectedItems[i] as Income1;
                            if (income != null)
                            {
                                var item = db.Incomes.Find(income.ID);
                                db.Incomes.Remove(item);
                            }
                        }
                    }
                    db.SaveChanges();
                }
            }
            Refresh();
        }
        public FinanceManager(User users)
        {
            InitializeComponent();

            MainWindow.user = users;

            db        = new FContext();
            user_name = users.NameUser;
            db.Users.Load();
            db.ViewCosts.Load();
            db.ViewIncomes.Load();

            db.PlaningCosts.Load();
            db.Incomes.Load();
            db.Currencies.Load();
            db.Costs.Load();
            db.Accounts.Load();
            db.Transfers.Load();
            string sqlExpression  = "SELECT Costs.Pdate,ViewCosts.ViewCosts, Costs.Amount, Accounts.Name ,Costs.Description, Users.NameUser, Costs.ID FROM Costs left join Accounts On Accounts.ID=Costs.Account left join Users On Accounts.NameUser=Users.ID left join ViewCosts On Costs.Type = ViewCosts.ID Where Users.NameUser='******'";
            string sqlAccount     = " Select Accounts.Name, Accounts.Amount, Currencies.Shortname , ISNULL(d.[Costs],0), ISNULL(b.[Incomes],0), Accounts.Description , Users.NameUser, Accounts.ID  From Accounts full join Users On Accounts.NameUser=Users.ID full join ( Select  sum(Costs.Amount)[Costs], Account  From Costs  group by Account) d  on d.Account=Accounts.ID  left join Currencies on Accounts.Currency=Currencies.ID full join (Select  sum(Incomes.Amount)[Incomes], Account From Incomes  group by Account) b  on b.Account=Accounts.ID Where Users.NameUser='******'";
            string sqlIncome      = "SELECT Incomes.Pdate,ViewIncomes.ViewIncomes, Incomes.Amount, Accounts.Name ,Incomes.Description, Users.NameUser,Incomes.ID FROM Incomes left join Accounts On Accounts.ID=Incomes.Account left join Users On Accounts.NameUser=Users.ID left join ViewIncomes On Incomes.Type = ViewIncomes.ID Where Users.NameUser='******'";
            string sqlTransfer    = "SELECT Transfers.Pdate,ac1.Name,ac2.Name, Transfers.Amount, Users.NameUser, Transfers.ID FROM Transfers left join [Accounts] ac1 ON Transfers.Account1=ac1.ID left join  [Accounts] ac2  on Transfers.Account2=ac2.ID  left join Users On Users.ID=ac2.NameUser AND Users.ID=ac1.NameUser Where Users.NameUser='******'";
            string sqlplaning     = "SELECT PlaningCosts.Pdate,ViewCosts.ViewCosts, PlaningCosts.Amount, Accounts.Name ,PlaningCosts.Description, Users.NameUser, PlaningCosts.ID FROM PlaningCosts left join Accounts On Accounts.ID=PlaningCosts.Account left join Users On Accounts.NameUser=Users.ID left join ViewCosts On PlaningCosts.Type = ViewCosts.ID where Users.NameUser='******'";
            string sqlPlaneIncome = "SELECT PlaningIncomes.Pdate,ViewIncomes.ViewIncomes, PlaningIncomes.Amount, Accounts.Name ,PlaningIncomes.Description, Users.NameUser, PlaningIncomes.ID FROM PlaningIncomes left join Accounts On Accounts.ID = PlaningIncomes.Account left join Users On Accounts.NameUser = Users.ID left join ViewIncomes On PlaningIncomes.Type = ViewIncomes.ID where Users.NameUser = '******'";

            using (SqlConnection sqlconnection1 = new SqlConnection("Data Source = 1-ПК\\SQLSERVER; MultipleActiveResultSets=True; Trusted_Connection = Yes; DataBase = Finances"))
            {
                sqlconnection1.Open();

                SqlCommand    command = new SqlCommand(sqlExpression, sqlconnection1);
                SqlDataReader reader  = command.ExecuteReader();
                List <Cost1>  costs   = new List <Cost1>();


                if (reader.HasRows)
                {
                    try
                    {
                        while (reader.Read())
                        {
                            string pdata = reader.GetDateTime(0).ToShortDateString();
                            Cost1  cost  = new Cost1(pdata, reader.GetString(3), reader.GetString(1), reader.GetDecimal(2), reader.GetString(4), reader.GetInt32(6));
                            costs.Add(cost);
                        }
                        CostsGrid.ItemsSource = costs;
                        CostsStatus.Text      = "Количество записей: " + CostsGrid.Items.Count;
                    }

                    catch { }

                    reader.Close();
                }
                else
                {
                    reader.Close();
                }



                SqlCommand    commandAccount = new SqlCommand(sqlAccount, sqlconnection1);
                SqlDataReader reader1        = commandAccount.ExecuteReader();

                List <Account1> accounts = new List <Account1>();


                if (reader1.HasRows)
                {
                    if (reader1.HasRows)
                    {
                        try
                        {
                            while (reader1.Read())
                            {
                                decimal rest = reader1.GetDecimal(1) - reader1.GetDecimal(3) + reader1.GetDecimal(4);


                                Account1 account = new Account1(reader1.GetString(0), reader1.GetString(2), reader1.GetDecimal(1), rest, reader1.GetString(5), reader1.GetInt32(7));

                                accounts.Add(account);
                            }
                            AccountGrid.ItemsSource = accounts;
                            AccountStatus.Text      = "Количество счетов: " + AccountGrid.Items.Count;
                        }
                        catch { }
                    }

                    reader1.Close();
                }

                SqlCommand     commandIncome = new SqlCommand(sqlIncome, sqlconnection1);
                SqlDataReader  reader2       = commandIncome.ExecuteReader();
                List <Income1> incomes       = new List <Income1>();

                if (reader2.HasRows)
                {
                    try
                    {
                        while (reader2.Read())
                        {
                            string  pdata  = reader2.GetDateTime(0).ToShortDateString();
                            Income1 income = new Income1(pdata, reader2.GetString(3), reader2.GetString(1), reader2.GetDecimal(2), reader2.GetString(4), reader2.GetInt32(6));
                            incomes.Add(income);
                        }
                        IncomeGrid.ItemsSource = incomes;
                        IncomeStatus.Text      = "Количество записей: " + IncomeGrid.Items.Count;
                    }
                    catch { }
                    reader2.Close();
                }

                SqlCommand       commandTransfer = new SqlCommand(sqlTransfer, sqlconnection1);
                SqlDataReader    reader3         = commandTransfer.ExecuteReader();
                List <Transfer1> transfers       = new List <Transfer1>();

                if (reader3.HasRows)
                {
                    try
                    {
                        while (reader3.Read())
                        {
                            string    pdata    = reader3.GetDateTime(0).ToShortDateString();
                            Transfer1 transfer = new Transfer1(pdata, reader3.GetString(1), reader3.GetString(2), reader3.GetDecimal(3), reader3.GetInt32(5));
                            transfers.Add(transfer);
                        }
                        TransfersGrid.ItemsSource = transfers;
                        Status.Text = "Количество записей: " + TransfersGrid.Items.Count;
                    }
                    catch { }
                    reader3.Close();

                    SqlCommand          command3     = new SqlCommand(sqlplaning, sqlconnection1);
                    SqlDataReader       reader6      = command3.ExecuteReader();
                    List <PlaningCost1> planingcosts = new List <PlaningCost1>();

                    if (reader6.HasRows)
                    {
                        try
                        {
                            while (reader6.Read())
                            {
                                string       pdata = reader6.GetDateTime(0).ToShortDateString();
                                PlaningCost1 cost  = new PlaningCost1(pdata, reader6.GetString(3), reader6.GetString(1), reader6.GetDecimal(2), reader6.GetString(4), reader6.GetInt32(6));
                                planingcosts.Add(cost);
                            }
                            CostGrid.ItemsSource = planingcosts;
                            CostStatus.Text      = "Количество записей: " + CostGrid.Items.Count;
                        }
                        catch { }
                        reader6.Close();
                    }


                    SqlCommand            planeIncome = new SqlCommand(sqlPlaneIncome, sqlconnection1);
                    SqlDataReader         reader4     = planeIncome.ExecuteReader();
                    List <PlaningIncome1> incom       = new List <PlaningIncome1>();

                    if (reader4.HasRows)
                    {
                        try
                        {
                            while (reader4.Read())
                            {
                                string         pdata  = reader4.GetDateTime(0).ToShortDateString();
                                PlaningIncome1 income = new PlaningIncome1(pdata, reader4.GetString(3), reader4.GetString(1), reader4.GetDecimal(2), reader4.GetString(4), reader4.GetInt32(6));
                                incom.Add(income);
                            }
                            IncomsGrid.ItemsSource = incom;
                            IncomesStatus.Text     = "Количество записей: " + IncomsGrid.Items.Count;
                        }
                        catch { }
                        reader4.Close();
                    }
                }
            }
        }