C# ile Liste Elemanlarını ListView’ de Görüntüleme
C# ile Personel türünden bir listeyi ListView denetiminde görüntülemek için yapılması gerekenler ve ListView ile ilgili bazı ayarları görebileceğiniz örneğe ait kodlar aşağıdadır.
Örneğimizde ilk olarak Personel.cs isimli sınıfımızı oluşturacağız.
Bu işlem için Solution Explorer penceresinde proje üzerinde sağ tıklayarak Add-Class seçeneğini seçebilirsiniz.

Personel class içeriği aşağıdaki gibidir.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DenemeProjeler
{
internal class Personel
{
string ad;
string soyad;
DateTime dogumTarihi;
string departman;
double maas;
string adres;
public string Ad { get => ad; set => ad = value; }
public string Soyad { get => soyad; set => soyad = value; }
public DateTime DogumTarihi { get => dogumTarihi; set => dogumTarihi = value; }
public string Departman { get => departman; set => departman = value; }
public double Maas { get => maas; set => maas = value; }
public string Adres { get => adres; set => adres = value; }
public Personel(string ad, string soyad, DateTime dogumTarihi, string departman, double maas, string adres)
{
this.Ad = ad;
this.Soyad = soyad;
this.DogumTarihi = dogumTarihi;
this.Departman = departman;
this.Maas = maas;
this.Adres = adres;
}
}
}
Form tasarımı ve kodları aşağıdaki gibi olacaktır.

C# Form Kodları :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace DenemeProjeler
{
public partial class Form1 : DevExpress.XtraEditors.XtraForm
{
public Form1()
{
InitializeComponent();
}
List personelListesi = new List();
private void Form1_Load(object sender, EventArgs e)
{
personelListesi.Add(new Personel("Ayşe", "Aydın", new DateTime(1992, 01, 31), "Bilgi İşlem", 15000, "Bağcılar"));
personelListesi.Add(new Personel("Doruk", "Beyaz", new DateTime(1989, 10, 15), "Muhasebem", 8500, "Bahçelievler"));
personelListesi.Add(new Personel("Fatma", "Kırmızı", new DateTime(1995, 10, 20), "Pazarlama", 17500, "Kadıköy"));
personelListesi.Add(new Personel("Burak", "Kara", new DateTime(1982, 10, 10), "Pazarlama", 13750, "Bağcılar"));
personelListesi.Add(new Personel("Şaziye", "Sarı", new DateTime(1984, 10, 27), "Bilgi İşlem", 8500, "Avcılar"));
personelListesi.Add(new Personel("Cemal", "Ak", new DateTime(1987, 10, 28), "Satın Alma", 12000, "Esenler"));
personelListesi.Add(new Personel("Kemal", "Akyüz", new DateTime(1996, 10, 07), "Muhasebe", 14000, "Avcılar"));
personelListesi.Add(new Personel("Burak", "Kızıl", new DateTime(1997, 10, 11), "Pazarlama", 12000, "Fatih"));
personelListesi.Add(new Personel("Alparslan", "Kaya", new DateTime(1999, 10, 12), "Muhasebe", 13000, "Bahçelievler"));
personelListesi.Add(new Personel("Alper", "Elma", new DateTime(1999, 10, 23), "Bilgi İşlem", 9250, "Büyükçekmece"));
listView1.View = View.Details;
listView1.GridLines = true;
listView1.FullRowSelect = true;
listView1.Columns.Add("Ad", 100);
listView1.Columns.Add("Soyad", 100);
listView1.Columns.Add("Doğum Tarihi", 100);
listView1.Columns.Add("Departman", 100);
listView1.Columns.Add("Maaş", 100);
listView1.Columns.Add("Adres", 100);
}
private void simpleButton1_Click(object sender, EventArgs e)
{
foreach (var personel in personelListesi)
{
ListViewItem satir = new ListViewItem(personel.Ad);
satir.SubItems.Add(personel.Soyad);
satir.SubItems.Add(personel.DogumTarihi.ToShortDateString());
satir.SubItems.Add(personel.Departman);
satir.SubItems.Add(personel.Maas.ToString());
satir.SubItems.Add(personel.Adres);
listView1.Items.Add(satir);
}
}
}
}

