Created
May 3, 2015 20:25
-
-
Save fpopic/8b3f84c295ff0b949fbf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Windows.Forms; | |
using System.Data.Entity; | |
using System.Collections; | |
using System.Threading.Tasks; | |
namespace EF_Firma | |
{ | |
public partial class SortFilterForm : Form | |
{ | |
private class FilterElement | |
{ | |
public string Opis { get; set; } | |
public Stupac Naziv { get; set; } | |
public string Vrijednost { get; set; } | |
public override string ToString() | |
{ | |
return Opis + " : " + Vrijednost; | |
} | |
} | |
private enum Stupac | |
{ | |
NazivMjesta, PbrMjesta, OznakaDrzave, NazivDrzave, PostanskiNazivMjesta | |
} | |
FirmaEntities context; | |
public SortFilterForm() | |
{ | |
InitializeComponent(); | |
} | |
private async void SortFilterForm_Load(object sender, EventArgs e) | |
{ | |
string text = buttonSortAndFilter.Text; | |
buttonSortAndFilter.Text = "Loading..."; | |
buttonSortAndFilter.Enabled = false; | |
await LoadData(); | |
FillCombos(); | |
buttonSortAndFilter.Text = text; | |
buttonSortAndFilter.Enabled = true; | |
} | |
private void FillCombos() | |
{ | |
List<KeyValuePair<string, Stupac>> list = new List<KeyValuePair<string, Stupac>>{ | |
new KeyValuePair<string, Stupac>("Naziv mjesta", Stupac.NazivMjesta), | |
new KeyValuePair<string, Stupac>("Naziv države", Stupac.NazivDrzave), | |
new KeyValuePair<string, Stupac>("Pbr mjesta", Stupac.PbrMjesta), | |
new KeyValuePair<string, Stupac>("Oznaka države", Stupac.OznakaDrzave), | |
new KeyValuePair<string, Stupac>("Poštanski naziv mjesta", Stupac.PostanskiNazivMjesta), | |
}; | |
comboBoxColumns.DisplayMember = "Key"; | |
comboBoxColumns.ValueMember = "Value"; | |
comboBoxColumns.DataSource = list; | |
for (int i = 1; i <= 4; i++) { | |
//za ostale comboboxove koristit ćemo nove liste s početnim praznim elementom | |
var listaSPraznim = new List<KeyValuePair<string, Stupac>>(list); | |
listaSPraznim.Insert(0, new KeyValuePair<string, Stupac>("", Stupac.NazivDrzave)); | |
ComboBox combo = groupBoxSort.Controls["comboSort" + i] as ComboBox; | |
if (combo != null) { | |
combo.DisplayMember = "Key"; | |
combo.ValueMember = "Value"; | |
combo.DataSource = listaSPraznim; | |
} | |
ComboBox comboPoredak = groupBoxSort.Controls["comboSortPoredak" + i] as ComboBox; | |
if (comboPoredak != null) { | |
comboPoredak.SelectedIndex = 0; | |
} | |
} | |
} | |
private async Task LoadData() | |
{ | |
context = new FirmaEntities(); | |
var query = context.Mjesto.Include(m => m.Drzava).AsNoTracking(); | |
mjestoBindingSource.DataSource = await query.ToListAsync(); | |
} | |
private void SortFilterForm_FormClosing(object sender, FormClosingEventArgs e) | |
{ | |
context.Dispose(); | |
} | |
private async void buttonSortAndFilter_Click(object sender, EventArgs e) | |
{ | |
string text = buttonSortAndFilter.Text; | |
buttonSortAndFilter.Text = "Loading..."; | |
buttonSortAndFilter.Enabled = false; | |
var query = context.Mjesto.Include(m => m.Drzava).AsNoTracking(); | |
query = ApplyFilter(query); | |
IOrderedQueryable<Mjesto> sortedQuery = ApplySort(query); | |
if (sortedQuery != null) { | |
mjestoBindingSource.DataSource = await sortedQuery.ToListAsync(); | |
} | |
else { | |
mjestoBindingSource.DataSource = await query.ToListAsync(); | |
} | |
buttonSortAndFilter.Text = text; | |
buttonSortAndFilter.Enabled = true; | |
} | |
private IOrderedQueryable<Mjesto> ApplySort(IQueryable<Mjesto> query) | |
{ | |
IOrderedQueryable<Mjesto> sortedQuery = null; | |
for (int i = 1; i <= 4; i++) { | |
ComboBox combo = groupBoxSort.Controls["comboSort" + i] as ComboBox; | |
ComboBox comboPoredak = groupBoxSort.Controls["comboSortPoredak" + i] as ComboBox; | |
if (combo.SelectedIndex > 0) { | |
Stupac stupac = (Stupac) combo.SelectedValue; | |
bool descending = comboPoredak.SelectedIndex == 1; | |
switch (stupac) { | |
case Stupac.PbrMjesta: | |
if (sortedQuery == null) { | |
if (descending) | |
sortedQuery = query.OrderByDescending(m => m.PostBrMjesta); | |
else | |
sortedQuery = query.OrderBy(m => m.PostBrMjesta); | |
} | |
else { | |
if (descending) | |
sortedQuery = sortedQuery.ThenByDescending(m => m.PostBrMjesta); | |
else | |
sortedQuery = sortedQuery.ThenBy(m => m.PostBrMjesta); | |
} | |
break; | |
case Stupac.NazivMjesta: | |
if (sortedQuery == null) { | |
if (descending) | |
sortedQuery = query.OrderByDescending(m => m.NazMjesta); | |
else | |
sortedQuery = query.OrderBy(m => m.NazMjesta); | |
} | |
else { | |
if (descending) | |
sortedQuery = sortedQuery.ThenByDescending(m => m.NazMjesta); | |
else | |
sortedQuery = sortedQuery.ThenBy(m => m.NazMjesta); | |
} | |
break; | |
case Stupac.OznakaDrzave: | |
if (sortedQuery == null) { | |
if (descending) | |
sortedQuery = query.OrderByDescending(m => m.OznDrzave); | |
else | |
sortedQuery = query.OrderBy(m => m.OznDrzave); | |
} | |
else { | |
if (descending) | |
sortedQuery = sortedQuery.ThenByDescending(m => m.OznDrzave); | |
else | |
sortedQuery = sortedQuery.ThenBy(m => m.OznDrzave); | |
} | |
break; | |
case Stupac.NazivDrzave: | |
if (sortedQuery == null) { | |
if (descending) | |
sortedQuery = query.OrderByDescending(m => m.Drzava.NazDrzave); | |
else | |
sortedQuery = query.OrderBy(m => m.Drzava.NazDrzave); | |
} | |
else { | |
if (descending) | |
sortedQuery = sortedQuery.ThenByDescending(m => m.Drzava.NazDrzave); | |
else | |
sortedQuery = sortedQuery.ThenBy(m => m.Drzava.NazDrzave); | |
} | |
break; | |
case Stupac.PostanskiNazivMjesta: | |
if (sortedQuery == null) { | |
if (descending) | |
sortedQuery = query.OrderByDescending(m => m.PostNazMjesta); | |
else | |
sortedQuery = query.OrderBy(m => m.PostNazMjesta); | |
} | |
else { | |
if (descending) | |
sortedQuery = sortedQuery.ThenByDescending(m => m.PostNazMjesta); | |
else | |
sortedQuery = sortedQuery.ThenBy(m => m.PostNazMjesta); | |
} | |
break; | |
} | |
} | |
} | |
return sortedQuery; | |
} | |
private IQueryable<Mjesto> ApplyFilter(IQueryable<Mjesto> query) | |
{ | |
foreach (FilterElement item in listBoxFilter.Items) { | |
string vrijednost = item.Vrijednost; | |
//moramo definirati lokalnu varijablu, jer bi inače parametar bila referenca na item.Vrijednost što bi se povezalo tek naknadno | |
//pa bi svi parametri bili isti -> vidi PPIJ :) | |
switch (item.Naziv) { | |
case Stupac.PbrMjesta: | |
int pbr = int.Parse(vrijednost); | |
query = query.Where(m => m.PostBrMjesta == pbr); | |
break; | |
case Stupac.OznakaDrzave: | |
query = query.Where(m => m.OznDrzave == vrijednost); | |
break; | |
case Stupac.NazivDrzave: | |
query = query.Where(m => m.Drzava.NazDrzave.Contains(vrijednost)); //case insensitive ako se radi o sql upitu | |
break; | |
case Stupac.NazivMjesta: | |
query = query.Where(m => m.NazMjesta.Contains(vrijednost)); | |
break; | |
case Stupac.PostanskiNazivMjesta: | |
query = query.Where(m => m.PostNazMjesta.Contains(vrijednost)); | |
break; | |
} | |
} | |
return query; | |
} | |
private void listBoxFilter_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.Delete) { | |
ArrayList list = new ArrayList(listBoxFilter.SelectedItems); | |
foreach (var item in list) { | |
listBoxFilter.Items.Remove(item); | |
} | |
e.Handled = true; | |
} | |
} | |
private void textBoxUvjet_KeyDown(object sender, KeyEventArgs e) | |
{ | |
if (e.KeyCode == Keys.Enter) { | |
string vrijednost = textBoxUvjet.Text; | |
if (!string.IsNullOrWhiteSpace(vrijednost)) { | |
KeyValuePair<string, Stupac> pair = (KeyValuePair<string, Stupac>) comboBoxColumns.SelectedItem; | |
FilterElement filterElement = new FilterElement() | |
{ | |
Naziv = pair.Value, | |
Vrijednost = vrijednost, | |
Opis = pair.Key | |
}; | |
listBoxFilter.Items.Add(filterElement); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment