private void frmUserDetails_Load(object sender, EventArgs e) { using (AnnonserEntities1 db = new AnnonserEntities1()) { List <Advert> advert = db.Adverts.Where(s => s.UserID == this.UserID).ToList(); listboxmyad.DisplayMember = "Title"; foreach (Advert ad in advert) { listboxmyad.Items.Add(ad); } } using (AnnonserEntities1 db = new AnnonserEntities1()) { List <Category> category = db.Categories.ToList(); ComboBoxItem item = new ComboBoxItem(); item.Value = 0; item.Text = "Alla kategorier"; cboCategory.Items.Add(item); foreach (Category cat in category) { ComboBoxItem listItem = new ComboBoxItem(); listItem.Value = cat.CategoryID; listboxmyad.ValueMember = "AdID"; listItem.Text = cat.Categoryname; cboCategory.Items.Add(listItem); } cboCategory.SelectedIndex = 0; } }
private void Ad_Load(object sender, EventArgs e) { //db.AdLoad(cboCategori, listBox1); using (AnnonserEntities1 db = new AnnonserEntities1()) { List <Category> category = db.Categories.ToList(); ComboBoxItem item = new ComboBoxItem(); item.Value = 0; item.Text = "Alla kategorier"; cboCategori.Items.Add(item); foreach (Category cat in category) { ComboBoxItem listItem = new ComboBoxItem(); listItem.Value = cat.CategoryID; listBox1.ValueMember = "AdID"; listItem.Text = cat.Categoryname; cboCategori.Items.Add(listItem); } cboCategori.SelectedIndex = 0; } }
private void cmdDeleteAd_Click(object sender, EventArgs e) { Advert selectedad = (Advert)(listboxmyad.SelectedItem); using (AnnonserEntities1 db = new AnnonserEntities1()) { //Här görs databasanrop dvs en command med en sql fråga som läser ut just denna leverantören Advert advert = db.Adverts.Where(s => s.AdID == selectedad.AdID).FirstOrDefault(); db.Adverts.Remove(advert); db.SaveChanges(); MessageBox.Show("Annons borttagen"); } }
private void cmdSortByPrice_Click(object sender, EventArgs e) { USer user = new USer(); UserID = user.UserID; listBox1.DataSource = null; using (AnnonserEntities1 db = new AnnonserEntities1()) { List <Advert> adverts; adverts = db.Adverts.Where(a => a.Title.Contains(txtSearch.Text)).OrderByDescending(a => a.Price).ToList(); listBox1.DisplayMember = "Title"; listBox1.DataSource = adverts; } }
private void listboxmyad_SelectedIndexChanged(object sender, EventArgs e) { if (listboxmyad.SelectedItem != null) { Advert selectedad = (Advert)(listboxmyad.SelectedItem); using (AnnonserEntities1 db = new AnnonserEntities1()) { Advert advert = db.Adverts.Where(s => s.AdID == selectedad.AdID).FirstOrDefault(); txtAdInfo.Text = advert.Description; txtAdTitel.Text = advert.Title; txtPrice.Text = (advert.Price).ToString(); txtLocation.Text = advert.Location; cboCategory.Text = ar.GetCategoryName(advert.CategoryID); } } }
private void cmdCreateNew_Click(object sender, EventArgs e) { using (AnnonserEntities1 db = new AnnonserEntities1()) { Advert advert = new Advert(); advert.Title = txtAdTitel.Text; advert.Description = txtAdInfo.Text; advert.Price = Convert.ToInt32(txtPrice.Text); advert.Location = txtLocation.Text; advert.AdvertDate = DateTime.Now; advert.CategoryID = cboCategory.SelectedIndex; advert.UserID = this.UserID; db.Adverts.Add(advert); db.SaveChanges(); MessageBox.Show("Annons sparad"); } }
private void cmdUpdateAd_Click(object sender, EventArgs e) { Advert selectedad = (Advert)(listboxmyad.SelectedItem); using (AnnonserEntities1 db = new AnnonserEntities1()) { //Här görs databasanrop dvs en command med en sql fråga som läser ut just denna leverantören Advert advert = db.Adverts.Where(s => s.AdID == selectedad.AdID).FirstOrDefault(); advert.Title = txtAdTitel.Text; advert.Description = txtAdInfo.Text; advert.Price = Convert.ToInt32(txtPrice.Text); advert.Location = txtLocation.Text; advert.AdvertDate = DateTime.Now; advert.CategoryID = cboCategory.SelectedIndex; db.SaveChanges(); MessageBox.Show("Annons uppdaterad"); } }