Hello,
I what to populate a datagridview from a list.
I have a form, with a datagridview, i want to populate the datagridview form a list.
When the form is open, he receive an id from another form, with that id i need too load all data from a table in a list, and then from that list return the list, and read the list in my form and populate tha datagridview from tha list.
I know is a lot code to red, but please some one help me.
All code below.
I do something like that:
And the form code:
using Amanet_.Amanet.Core;
namespace Amanet_.VizualizareDate.VizualizareContracte
{
public partial class winIstoricContracte : Form
{
public Contract istoricContract;
public winIstoricContracte(int contractId)
{
InitializeComponent();
this.istoricContract = SetariAmanet.IncarcaContract(contractId);
}
private void winIstoricContracteLoad(object sender, EventArgs e)
{
if (istoricContract == null)
{
MessageBox.Show(this, string.Format("Contractul {0} nu a fost gasit.", istoricContract), "Afișare istoric contract", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
else
{
}
}
}
}
A class named "Contract.cs", partial code below:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Amanet_.Amanet.Core
{
public class Contract
{
private int clientId;
private int numarContract;
private DateTime dataCreare;
private DateTime dataTerminare;
private decimal procent;
private DateTime dataInceput;
public Contract(DataTable ValoareCautata)
{
this.numarContract = (int)ValoareCautata.Rows[0]["ContractId"];
this.clientId = (int)ValoareCautata.Rows[0]["clientId"];
this.dataCreare = (DateTime)ValoareCautata.Rows[0]["CreateDate"];
this.dataInceput = (DateTime)ValoareCautata.Rows[0]["StartDate"];
this.dataTerminare = (DateTime)ValoareCautata.Rows[0]["EndDate"];
this.procent = (decimal)ValoareCautata.Rows[0]["Procent"];
}
public IstoricContracte[] IstoricOperatii
{
get
{
return SetariAmanet.IstoricContracte(this.numarContract);
}
}
public int ClientId
{
get
{
return this.clientId;
}
set
{
this.clientId = value;
}
}
...... seme get and set.
Second class named "IstoricContracte.cs" partial code below:
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Amanet_.Amanet.Core
{
public class IstoricContracte
{
private string actiunea;
private int nristoricContract;
private int contractId;
internal const string CREARE_CONTRACT = "creare contract";
private DateTime data;
private string descriere;
internal const string PRELUNGIRE_CONTRACT = "prelungire contract";
internal const string MODIFICARE_CONTRACT = "modificare contract";
private decimal platit;
private decimal incasat;
public IstoricContracte()
{
}
public IstoricContracte(DataTable ValoareContract)
{
this.nristoricContract = (int)ValoareContract.Rows[0]["ContractHistoryId"];
this.contractId = (int)ValoareContract.Rows[0]["ContractId"];
this.actiunea = (string)ValoareContract.Rows[0]["InsertAction"];
this.descriere = (string)ValoareContract.Rows[0]["Description"];
this.platit = (decimal)ValoareContract.Rows[0]["Payment"];
this.incasat = (decimal)ValoareContract.Rows[0]["Price"];
this.data = (DateTime)ValoareContract.Rows[0]["DateInsert"];
}
.... some get and set...
The third class named "SetariAmanet.cs":
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Amanet_.Amanet.Core
{
internal static class SetariAmanet
{
private static T[] Lista<T>(DataTable valoare, Converter<DataTable, T> creaza)
{
List<T> list = new List<T>();
if (valoare != null)
{
list.Add(creaza(valoare));
}
return list.ToArray();
}
internal static Contract IncarcaContract(int contractId)
{
Program.Connection.CommandText = "select * from Contracts where ContractId=@ContractId";
Program.Connection.AddParameter("@ContractId", contractId);
DataTable Table = new DataTable();
Program.Connection.FillDataTable(Table, true);
if (Table.Rows.Count == 0)
{
throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
}
return new Contract(Table);
}
internal static IstoricContracte[] IstoricContracte(int contractId)
{
Program.Connection.CommandText = "select * from ContractHistory where ContractId=@ContractId";
Program.Connection.AddParameter("@ContractId", contractId);
DataTable Table = new DataTable();
Program.Connection.FillDataTable(Table, true);
if (Table.Rows.Count == 0)
{
throw new Exception(string.Format("Contractul {0} nu a fost gasit.", contractId));
}
return Lista<IstoricContracte>(Table, delegate(DataTable rezultatTB)
{
return new IstoricContracte(rezultatTB);
});
}
}
}