Hello,
I have a DataGridView and i what to fill with 50000 record but it taken about 45s.
Is there a faster way.
In the form whare i have the datagridview i use this code to fill it:
DataTable dataIN = new DataTable();
public winContracteActive()
{
InitializeComponent();
incarcaProduse();
}
private void winContracteActive_Load(object sender, EventArgs e)
{
}
private void incarcaProduse()
{
try
{
AsyncProcessDelegate d = delegate()
{
dataIN = InterogariDB.incarcaProduse();
this.Invoke((MethodInvoker)delegate
{
citireDate("contracteActive");
});
};
RunAsyncOperation(d);
}
catch (Exception Ex)
{
string msg = "Cod eroare: 001. \n\nEroare: \n" + Ex.Message;
DoHandleException(msg);
}
}
private void citireDate(string tipInterogare)
{
try
{
AsyncProcessDelegate d = delegate()
{
ContractItem[] produseContract = InterogariDB.filtrareProduse(dataIN, tipInterogare);
this.Invoke((MethodInvoker)delegate
{
dContracte.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
dContracte.SuspendLayout();
try
{
dContracte.DataSource = produseContract;
}
finally
{
dContracte.ResumeLayout();
}
});
};
RunAsyncOperation(d);
}
catch (Exception Ex)
{
string msg = "Cod eroare: 001. \n\nEroare: \n" + Ex.Message;
DoHandleException(msg);
}
}
To retrieve the data from database i have a class and use:
internal static DataTable incarcaProduse()
{
Program.Connection.CommandText = "select ContractItems.ContractItemId, ContractItems.ContractId, ContractItems.Name, ContractItems.Description, ContractItems.Payment, ContractItems.Quantity, "
+ "ContractItems.QuantityUnit, IIF(ContractItems.DeletedId Is Null, 0, ContractItems.DeletedId) AS idStergere, IIF(ContractItems.GramajBrut Is Null, 0, ContractItems.GramajBrut) AS GramajBrut, " +
" IIF(ContractItems.CategorieProdus Is Null, 0, ContractItems.CategorieProdus) AS CategorieProdus"
+ ", Contracts.CreateDate, Contracts.StartDate, Contracts.EndDate FROM Contracts INNER JOIN ContractItems ON Contracts.ContractId = ContractItems.ContractId";
DataTable dataTI = new DataTable();
Program.Connection.FillDataTable(dataTI, true);
return dataTI;
}
internal static ContractItem[] filtrareProduse(DataTable dataIN, string tipInterogare)
{
DataTable dataFI = new DataTable();
switch (tipInterogare)
{
case "contracteActive":
dataFI = dataIN.AsEnumerable()
.Where(i => i.Field<int>("idStergere") == 0)
.OrderBy(i => i.Field<int>("ContractId"))
.CopyToDataTable();
break;
case "toateContractele":
dataFI = dataIN.AsEnumerable()
.Where(i => i.Field<int>("idStergere") != 0)
.OrderBy(i => i.Field<int>("ContractId"))
.CopyToDataTable();
break;
}
if (dataFI.Rows.Count == 0)
{
throw new Exception(string.Format("Nu sunt contracte in baza de date."));
}
return Lista<ContractItem>(dataFI, delegate(DataRow rezultatTB)
{
return new ContractItem(rezultatTB);
});
}