Bu konumuzda C# Sınıf ve Liste kullanımını gösteren bir örnek oluşturacağız. Örneğimizde Sql Server veritabanında bulunan bir tablodan Sınıf(Class) ve List kullanarak nasıl veri çekebileceğinizi, List ile ilgili çeşitli metotların kullanımını ve basit LINQ sorgularını öğreneceksiniz.
Açıklama:
Form açıldığında Customer tablosundaki veriler bir Liste içine alınacak ve DataGridView içinde görüntülenecek.
Bu liste üzerinde bir LINQ sorgusu ile ülkelerin benzersiz bir şekilde(her ülke bir defa) combobox içine eklenmesi sağlanacak.
Combobox’ ta seçilen ülkeye göre kayıtların DataGridView içinde yine List içinde bir LINQ sorgusu yazılarak filtrelenmesi sağlanacak.
Örneğimize geçelim.
Kullanılacak veritabanı ve tablo:
Form Tasarımı:
Class lar:
Not: Class oluşturmak için;
C# kodları ve projenin videosu:
Customer.cs sınıfı için kodlarımız:
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ClassListCustomer { internal class Customer { string customerId; string companyName; string contactName; string contactTitle; string address; string city; string region; string postalCode; string country; string phone; string fax; public string CustomerId { get => customerId; set => customerId = value; } public string CompanyName { get => companyName; set => companyName = value; } public string ContactName { get => contactName; set => contactName = value; } public string ContactTitle { get => contactTitle; set => contactTitle = value; } public string Address { get => address; set => address = value; } public string City { get => city; set => city = value; } public string Region { get => region; set => region = value; } public string PostalCode { get => postalCode; set => postalCode = value; } public string Country { get => country; set => country = value; } public string Phone { get => phone; set => phone = value; } public string Fax { get => fax; set => fax = value; } } } |
CustomerProvider.cs sınıfı için kodlarımız:
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 51 52 53 54 55 56 57 58 59 60 61 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace ClassListCustomer { internal class CustomerProvider { SqlConnection con=new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True"); public List<Customer> customers { get; set; } public CustomerProvider() { customers=new List<Customer>(); GetCustomers(); } private void GetCustomers() { con.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con); SqlDataReader dr = cmd.ExecuteReader(); while (dr.Read()) { Customer customer = new Customer(); customer.CustomerId=dr[0].ToString(); customer.CompanyName=dr[1].ToString(); customer.ContactName=dr[2].ToString(); customer.ContactTitle=dr[3].ToString(); customer.Address=dr[4].ToString(); customer.City=dr[5].ToString(); customer.Region=dr[6].ToString(); customer.PostalCode=dr[7].ToString(); customer.Country=dr[8].ToString(); customer.Phone=dr[9].ToString(); customer.Fax=dr[10].ToString(); customers.Add(customer); } dr.Close(); con.Close(); } public List<string> FillCombobox() { return customers.Select(x=>x.Country).Distinct().ToList(); } public List<Customer> FilterCustomer(string country) { return customers.Where(x => x.Country == country).ToList(); } } } |
Form1.cs için kodlarımız:
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 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace ClassListCustomer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } CustomerProvider provider = new CustomerProvider(); private void Form1_Load(object sender, EventArgs e) { dgvCustomers.DataSource = provider.customers; cmbCountry.DataSource=provider.FillCombobox(); } private void cmbCountry_SelectedIndexChanged(object sender, EventArgs e) { dgvCustomers.DataSource = provider.FilterCustomer(cmbCountry.Text); } private void btnListele_Click(object sender, EventArgs e) { cmbCountry.SelectedIndex = -1; dgvCustomers.DataSource = provider.customers; } } } |
Video:
Yorum Yap