
Grid z danymi przechowywanymi w ArrayList [C#]
Opublikowano: 2009-07-28 , wyświetlono: 5970
Kontrolka System.Windows.Forms.DataGrid z .NET wyświetla dane w formacie tabeli. W większości przykładów zilustrowane jest wyświetlanie danych pobieranych z obiektów ADO.NET. Ja często potrzbuję wyświetlić w tej kontrolce listę obiektów przechowywanych w ArrayList. Ponizej kod za pomocą, którego realizuję tego typu przypadki.
Uwaga: Kod nie jest pełnym przykładem, zawarte są tylko te elementy, które mają ilustrować temat.
Uwaga: Kod nie jest pełnym przykładem, zawarte są tylko te elementy, które mają ilustrować temat.
// klasa Customer, ktorej obiekty beda w tablicy pokazanej w grid'zie
public class Customer
{
private string _code;
private string _name;
private string _city;
private string _zip;
private string _address;
private string _nip;
public string Code
{
get { return _code; }
set { _code = value; }
}
public string Name
{
get { return _name; }
set { _name = value; }
}
public string City
{
get { return _city; }
set { _city = value; }
}
public string Zip
{
get { return _zip; }
set { _zip = value; }
}
public string Address
{
get { return _address; }
set { _address = value; }
}
public string Nip
{
get { return _nip; }
set { _nip = value; }
}
public Customer()
{
_code = "";
_name = "";
_city = "";
_zip = "";
_nip = "";
_address = "";
}
public Customer(string code, string name, string city,
string zip, string address, string nip)
{
_code = code;
_name = name;
_city = city;
_zip = zip;
_address = address;
_nip = nip;
_balance = balance;
}
}
// ...
// deklaracja grid'a
private System.Windows.Forms.DataGrid gridCustomer;
// ...
// kod wypelniajacy tablice obiektami
ArrayList listCustomer = new ArrayList();
Customer customer;
customer = new Customer("0001", "klient1", "miasto1", "11-000", "adr1", "000-000-00-01");
listCustomer.Add(customer);
customer = new Customer("0002", "klient2", "miasto2", "12-000", "adr2", "000-000-00-02");
listCustomer.Add(customer);
customer = new Customer("0003", "klient3", "miasto3", "13-000", "adr3", "000-000-00-03");
listCustomer.Add(customer);
// stworzenie obiektu odpowiedzialnego za wyswietlanie listy
System.Windows.Forms.DataGridTableStyle dataGridStyle = new DataGridTableStyle();
DataGridTextBoxColumn textColumn;
textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = "Name";
textColumn.HeaderText = "Nazwa";
textColumn.Width = 140;
dataGridStyle.GridColumnStyles.Add(textColumn);
textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = "Nip";
textColumn.HeaderText = "Nip";
textColumn.Width = 80;
dataGridStyle.GridColumnStyles.Add(textColumn);
textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = "City";
textColumn.HeaderText = "Miasto";
textColumn.Width = 100;
dataGridStyle.GridColumnStyles.Add(textColumn);
textColumn = new DataGridTextBoxColumn();
textColumn.MappingName = "Code";
textColumn.HeaderText = "Kod";
textColumn.Width = 0;
dataGridStyle.GridColumnStyles.Add(textColumn);
// przyporzadkowane listy do grid'a
dataGridStyle.MappingName = "ArrayList";
gridCustomer.TableStyles.Clear();
gridCustomer.TableStyles.Add(dataGridStyle);
gridCustomer.DataSource = listCustomer;
// ...