This is the code I use to build and add data to an iGridControl and I am finding my applications memory usage is over 1GB whereas it is normally about 100mb
there are between 200 and 300 rows in the table and I have the filter manager setup
public partial class LoadsOut : Form
{
public LoadsOut()
{
InitializeComponent();
comboBoxEx1.SelectedItem = comboBoxEx1.Items[0];
}
private async Task UpdateTable(DateTime WorkingDate, string value, DateTime? ChangeTime)
{
var MMtOnly = Global.UserAuthorisations.Any(a => a.AuthArea == "LoadsOut" & a.AuthLevel == 1);
var TrucksToDisplay = new List<TsData.TruckLoad>();
int windowStart = 10;
int windowEnd = 34;
if (value == "PTZ")
{
windowStart = 10;
windowEnd = 20;
}
else if (value == "LLC")
{
windowStart = 20;
windowEnd = 34;
}
while (TsData.IsUpdatescriptRunning)
{
Application.DoEvents();
}
TrucksToDisplay.AddRange(TsData.TruckLoads.Where(t=>MMtOnly? (t.HaulierName== "MMT"):true).Where(t=>(t.DepartBy.HasValue? (t.DepartBy.Value >= WorkingDate.AddHours(windowStart) & t.DepartBy <= WorkingDate.AddHours(windowEnd)):(t.InboundLoads.Any(l=>l.CollectionTime>= WorkingDate.AddHours(windowStart+12)& l.CollectionTime <= WorkingDate.AddHours(windowEnd + 12))))));
int countV = TrucksToDisplay.Count;
int i = 0;
while (i < countV)
{
var truck = TrucksToDisplay[i];
if (truck.ChangeTime >= (ChangeTime.HasValue ? ChangeTime.Value : DateTime.MinValue))
{
MethodInvoker inv = delegate
{
iGRow row;
if (iGrid1.Rows.KeyExists(truck.TruckID.ToString()))
{
row = iGrid1.Rows[truck.TruckID.ToString()];
}
else
{
row = iGrid1.Rows.Add();
}
row.Tag = truck.ChangeTime;
row.Cells["Destinations"].Value = string.Join("/ ", TsData.OutboundLoads.Where(t => t.TruckID == truck.TruckID).GroupBy(l => l.Consignee).Select(l => l.Key.Code));
row.Cells["Haulier"].Value = truck.HaulierName;
row.Cells["References"].Value = string.Join(",", TsData.OutboundLoads.Where(t => t.TruckID == truck.TruckID).GroupBy(l => l.DeliveryReference).Select(l => l.Key));
row.Cells["ManifestNo"].Value = truck.TruckID;
row.Cells["Categories"].Value = string.Join("/ ", TsData.OutboundLoads.Where(t => t.TruckID == truck.TruckID).GroupBy(l => l.BaseCategory).Select(l => TsData.BaseCategories.Single(t => t.Value == l.Key).Name));
row.Cells["DepartBy"].Value = truck.DepartBy.HasValue ? truck.DepartBy.Value.ToString("yyyy-MM-dd HH:mm") : null;
row.Cells["ActualDepart"].Value = truck.DepartureTime.HasValue ? truck.DepartureTime.Value.ToString("HH:mm") : null;
row.Cells["DriverPhone"].Value = TsData.Drivers.Any(d => d.DriverName == truck.DriverName) ? TsData.Drivers.Where(d => d.DriverName == truck.DriverName).Single().Phone : null;
row.Cells["DriverName"].Value = truck.DriverName;
row.Cells["DriverDue"].Value = truck.DepartBy.HasValue ? truck.DepartBy.Value.AddMinutes(-45).ToString("yyyy-MM-dd HH:mm") : null;
row.Cells["Reg"].Value = truck.Registration;
row.Cells["Trailer"].Value = truck.TrailerRef;
row.Cells["TrailerType"].Value = truck.TrailerTypeRequirement;
row.Cells["BookingTime"].Value = TsData.OutboundLoads.Where(l => l.TruckID == truck.TruckID).Count() > 0 ? TsData.OutboundLoads.Where(l => l.TruckID == truck.TruckID).Min(l => l.DeliveryEta).ToString("yyyy-MM-dd HH:mm") : null;
row.Cells["ETAToDepot"].Value = truck.DepartureTime.HasValue ? (truck.DepartureTime.Value.Add(TsData.OutboundLoads.OrderBy(l => l.DeliveryEta).First().Consignee.TravelTime)).ToString("HH:mm") : null;
long[] loadids = TsData.OutboundLoads.Where(l => l.TruckID == truck.TruckID).Select(l => l.LoadID).ToArray();
row.Cells["Pallets"].Value = TsData.Orders.Where(o => loadids.Any(l => l == o.LoadID)).Sum(o => o.OvrReqStorage.HasValue ? o.OvrReqStorage : o.ReqStorage);
row.Cells["Notes"].Value = truck.Notes.Any(n => n.NoteHead == "Loads Out Notes") ? truck.Notes.First(n => n.NoteHead == "Loads Out Notes").NoteDetails : "";
row.Cells["Collections"].Value = string.Join("\n", TsData.InboundLoads.OrderBy(l=>l.CollectionTime).Where(l => l.TruckID == truck.TruckID).Select(l => l.CollectionTime.ToString("HH:mm") + " " + TsData.Customers.Single(c => c.Account_Ref == l.Account_Ref).Name + ", " + l.SiteName + (l.StockCategory.ToUpper() == "X" ? " (PTZ)" : "")));
row.Cells["ManifestNotes"].Value = string.Join("\n",truck.Notes.Where(n => n.NoteHead != "Loads Out Notes").Select(l=>l.NoteHead));
row.Cells["Loaded"].Value = truck.OutboundLoads.Any(l=>l.LoadedAt.HasValue);
row.Cells["ETAIntoCML"].Value = truck.ETAintoCML.HasValue? truck.ETAintoCML.Value.ToString("dd/MM/yy HH:mm"): "";
row.Cells["DriverArrived"].Value = truck.DriverArriveTime.HasValue? truck.DriverArriveTime.Value.ToString("dd/MM/yy HH:mm") :"";
row.Key = truck.TruckID.ToString();
row.AutoHeight();
};
this.Invoke(inv);
}
i++;
}
MethodInvoker inv2 = delegate
{
while (iGrid1.Rows.Cast<iGRow>().Any(r => !TrucksToDisplay.Any(t => t.TruckID.ToString() == r.Key)))
{
iGrid1.Rows.RemoveAt(iGrid1.Rows.Cast<iGRow>().First(r => !TrucksToDisplay.Any(t => t.TruckID.ToString() == r.Key)).Index);
}
};
this.Invoke(inv2);
}
private void dateTimeInput1_Click(object sender, EventArgs e)
{
}
private void dateTimeInput1_ValueChanged(object sender, EventArgs e)
{
UpdateTable((DateTime?)null);
}
private void LoadsOut_Shown(object sender, EventArgs e)
{
dateTimeInput1.Value = DateTime.Today;
TsData.TruckLoadsUpdated += TsData_TruckLoadsUpdated;
}
void TsData_TruckLoadsUpdated(object sender, EventArgs e)
{
MethodInvoker inv = delegate
{
UpdateTable((DateTime)sender);
};
this.Invoke(inv);
}
private void iGAutoFilterManager1_FilterApplied(object sender, TenTec.Windows.iGridLib.Filtering.iGFilterAppliedEventArgs e)
{
labelX1.Text = e.FilteredRowCount.ToString() + " Vehicles Visible";
}
private void comboBoxEx1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateTable((DateTime?)null);
}
private async void UpdateTable(DateTime? ChangeTime)
{
if (!dateTimeInput1.IsEmpty)
{
//iGAutoFilterManager1.SaveFilterToMemory("Current");
if (!ChangeTime.HasValue)
{
//iGAutoFilterManager1.ClearFilter();
iGrid1.Rows.Clear();
}
var v = (string)comboBoxEx1.Text;
UpdateRunning isrunning = new UpdateRunning() { Screen = "LoadsOut", Process = "UpdateTable", Started = DateTime.Now };
while (TsData.IsUpdatescriptRunning)
{
Application.DoEvents();
}
try
{
TsData.IsDataInUse.Add(isrunning);
await UpdateTable(dateTimeInput1.Value.Date, v, ChangeTime);
TsData.IsDataInUse.RemoveAll(i => i.Process == isrunning.Process & i.Screen == isrunning.Screen);
}
catch
{
TsData.IsDataInUse.RemoveAll(i => i.Process == isrunning.Process & i.Screen == isrunning.Screen);
}
{
//iGAutoFilterManager1.RestoreFilterFromMemory("Current");
}
iGAutoFilterManager1.ReapplyFilter();
//labelX1.Text = iGAutoFilterManager1.FilteredRowCount.ToString() + " Vehicles Visible";
}
}
private void LoadsOut_FormClosing(object sender, FormClosingEventArgs e)
{
TsData.TruckLoadsUpdated -= TsData_TruckLoadsUpdated;
}
private void iGrid1_RequestEdit(object sender, iGRequestEditEventArgs e)
{
var r = iGrid1.Rows[e.RowIndex];
switch (iGrid1.Cols[e.ColIndex].Key)
{
case "DriverName":
break;
case "Notes":
break;
case "DriverArrived":
if (e.Text == "")
{
e.Text = DateTime.Now.ToString("dd/MM/yyyy HH:mm");
}
break;
case "ETAIntoCML":
break;
default :
e.DoDefault = false;
break;
}
}
private void iGrid1_RequestDropDownControl(object sender, iGRequestDropDownControlEventArgs e)
{
var r = iGrid1.Rows[e.RowIndex];
switch (iGrid1.Cols[e.ColIndex].Key)
{
case "DriverName":
iGDropDownList list = new iGDropDownList();
while (true)
{
try
{
list.Items.Add(new iGDropDownListItem("", ""));
list.Items.AddRange(TsData.Drivers.Where(i => i.HaulierName == (string)r.Cells["Haulier"].Value).Select(i => new iGDropDownListItem(i.DriverName, i.DriverName)).ToArray());
break;
}
catch
{
Thread.Sleep(50);
}
}
e.Control = list;
break;
}
}
private async void iGrid1_BeforeCommitEdit(object sender, iGBeforeCommitEditEventArgs e)
{
var r = iGrid1.Rows[e.RowIndex];
var truck = TsData.TruckLoads.Single(i => i.TruckID == (long)r.Cells["ManifestNo"].Value);
switch (iGrid1.Cols[e.ColIndex].Key)
{
case "DriverName":
TsData.DbReturnStatus rtn;
if ((string)e.NewValue == "")
{
rtn = await truck.SetDriverName("");
}
else
{
rtn = await truck.SetDriverName((string)e.NewValue);
}
if (!rtn.Success)
{
MessageBox.Show("Failed To Write To Database, Please try again");
}
break;
case "Notes":
await truck.AddNote("Loads Out Notes", e.NewText);
break;
case "DriverArrived":
DateTime AriveTime;
if (DateTime.TryParse(e.NewText, out AriveTime))
{
var rtn2 = await truck.SetDriverTime(AriveTime);
if (!rtn2.Success)
{
MessageBox.Show("Failed To Write To Database, Please try again");
}
}
else
{
MessageBox.Show("Invalid DateTime Format, Please try again");
}
break;
case "ETAIntoCML":
DateTime eta;
if (DateTime.TryParse(e.NewText, out eta))
{
var rtn2 = await truck.UpdateETAtoCML(eta.ToString());
if (!rtn2.Success)
{
MessageBox.Show("Failed To Write To Database, Please try again");
}
}
else
{
MessageBox.Show("Invalid DateTime Format, Please try again");
}
break;
default:
break;
}
}
}