Issue with DevExpress XtraTabControl - foreach

I am having a DevExpress XtraTabControl with 3 XtraTabPages.
I am trying to remove a Tabpage based on a condition and after removing for the last iteration,it is getting error.
My Code is
foreach (DevExpress.XtraTab.XtraTabPage ptp in tabContactsDetails.TabPages)
{
if (tabContactsDetails.TabPages.Contains(ptp))
{
if (ptp.Name == "tabPTP")
{
if (maxid == String.Empty || maxid == null || maxid == "lblHiddenDebtorID")
{
tabContactsDetails.TabPages.Remove(ptp);
}
else
{
}
}
}
}
and I am getting an error like
Collection was modified; enumeration operation may not execute.

You cannot change a collection while iterating through it!
What I do is the following:
List<XtraTabPage> tabPagesToBeRemoved = new List<XtraTabPage>();
foreach (XtraTabPage ptp in tabContactsDetails.TabPages)
{
if (shouldBeRemoved())
{
tabPagesToBeRemoved.Add(ptp);
}
}
foreach (XtraTabPage pageToBeRemoved in tabPagesToBeRemoved)
{
tabContactsDetails.TabPages.Remove(pageToBeRemoved);
}

Related

Check if some value already exist in database ASP.NET MVC?

How can I check if some value already exist in database I am doing MVC with Entity Framework and I want to check if element with composite key already exist in database application does someone has any suggestion i GOT the Json object but my done() method doesn't work?
I tried with JsonResult method from my controller
public JsonResult Check(int? id1, int? id2)
{
IQueryable<InspekcijskaKontrola> listaKontrola = db.InspekcijskeKontrole.Include(i => i.InspekcijskaTijela).Include(i => i.Proizvod).Select(i => i);
InspekcijskaKontrola inKontrola = listaKontrola.Where(i => i.InspekcijskoTijeloId == id1).Where(i => i.ProizvodId == id2).Select(i => i).Single();
if (inKontrola!=null)
{
return Json(inKontrola, JsonRequestBehavior.AllowGet);
}
return Json(new InspekcijskaKontrola { InspekcijskoTijeloId = -1, ProizvodId = -1 }, JsonRequestBehavior.AllowGet);
}
And I tried to rised modal dialog from my view with script
function prikazi() {
var zahtjev = $.getJSON("/InspekcijskeKontrole/Check?id1=" + $("#kombo3").val() + "&id2=" + $("#kombo4").val());
zahtjev.done(function (kontrola) {
if (kontrola.InspekcijskoTijeloId != -1 && kontrola.ProizvodId != -1) {
$("#p1").text("Inspekcijska kontrola za " + kontrola.ProizvodId + " je vec izvrsena");
$("#modalni1").modal({ backdrop: "static" });
}
});
}
Providing a bit of code would really help. However, right off the bat I could suggest you use something like this to search for a key(or more) from an object:
if(dbContext.Items.Any(anyObjectName=>anyObjectName.firstKey == ValueYouLookFor
&& anyObjectName.secondkey == AnotherValue))
{
//logic to apply if object exists
}

GMap.Net tooltips with multiple overlays

I have a map program that uses 2 overlays with markers on both overlays. The problem I am having is if I hover on a marker in the overlay[0] the tooltip is underneath the markers on overlay[1]. I can override GMapBaloonTool. Is there a way when the tooltip is rendered to force it to the top?
3 years later...
The problem is that the map draws overlays one by one. Each overlay has it's own tooltips. Overlays draw later are drawn above the other layers. This may be by design. For me was not ok. So, I changed GmapOverlays.cs, method OnRender: added new parameter, drawOnlyToolTips.
public virtual void OnRender(Graphics g, bool drawOnlyToolTips = false)
{
if (Control != null)
{
if (Control.RoutesEnabled && !drawOnlyToolTips)
{
foreach (GMapRoute r in Routes)
{
if (r.IsVisible)
{
r.OnRender(g);
}
}
}
if (Control.PolygonsEnabled && !drawOnlyToolTips)
{
foreach (GMapPolygon r in Polygons)
{
if (r.IsVisible)
{
r.OnRender(g);
}
}
}
if (Control.MarkersEnabled)
{
// markers
if (!drawOnlyToolTips)
{
foreach (GMapMarker m in Markers)
{
//if(m.IsVisible && (m.DisableRegionCheck || Control.Core.currentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y)))
if (m.IsVisible || m.DisableRegionCheck)
{
m.OnRender(g);
}
}
}
// tooltips above
foreach (GMapMarker m in Markers)
{
//if(m.ToolTip != null && m.IsVisible && Control.Core.currentRegion.Contains(m.LocalPosition.X, m.LocalPosition.Y))
if (m.ToolTip != null && m.IsVisible && drawOnlyToolTips)
{
if (!string.IsNullOrEmpty(m.ToolTipText) && (m.ToolTipMode == MarkerTooltipMode.Always || (m.ToolTipMode == MarkerTooltipMode.OnMouseOver && m.IsMouseOver)))
{
m.ToolTip.OnRender(g);
}
}
}
}
}
}
Next, in GMapControl.cs, method protected virtual void OnPaintOverlays(Graphics g), I changed the code in:
foreach (GMapOverlay o in Overlays)
{
if (o.IsVisibile)
{
o.OnRender(g, false); //call without drawing the tooltips
}
}
foreach (GMapOverlay o in Overlays)
{
if (o.IsVisibile)
{
o.OnRender(g, true); //draw only tooltips, after everything else is drawn
}
}

An exception of type 'System.OutOfMemoryException' occurred in itextsharp.dll but was not handled in user code

I am using iTextSharp to create pdf. I have 100k records, but I am getting following exception:
An exception of type 'System.OutOfMemoryException' occurred in
itextsharp.dll but was not handled in user code At the line:
bodyTable.AddCell(currentProperty.GetValue(lst, null).ToString());
Code is:
var doc = new Document(pageSize);
PdfWriter.GetInstance(doc, stream);
doc.Open();
//Get exportable count
int columns = 0;
Type currentType = list[0].GetType();
//PREPARE HEADER
//foreach visible columns check if current object has proerpty
//else search in inner properties
foreach (var visibleColumn in visibleColumns)
{
if (currentType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key) != null)
{
columns++;
}
else
{
//check child property objects
var childProperties = currentType.GetProperties();
foreach (var prop in childProperties)
{
if (prop.PropertyType.BaseType == typeof(BaseEntity))
{
if (prop.PropertyType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key) != null)
{
columns++;
break;
}
}
}
}
}
//header
var headerTable = new PdfPTable(columns);
headerTable.WidthPercentage = 100f;
foreach (var visibleColumn in visibleColumns)
{
if (currentType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key) != null)
{
//headerTable.AddCell(prop.Name);
headerTable.AddCell(visibleColumn.Value);
}
else
{
//check child property objects
var childProperties = currentType.GetProperties();
foreach (var prop in childProperties)
{
if (prop.PropertyType.BaseType == typeof(BaseEntity))
{
if (prop.PropertyType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key) != null)
{
//headerTable.AddCell(prop.Name);
headerTable.AddCell(visibleColumn.Value);
break;
}
}
}
}
}
doc.Add(headerTable);
var bodyTable = new PdfPTable(columns);
bodyTable.Complete = false;
bodyTable.WidthPercentage = 100f;
//PREPARE DATA
foreach (var lst in list)
{
int col = 1;
foreach (var visibleColumn in visibleColumns)
{
var currentProperty = currentType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key);
if (currentProperty != null)
{
if (currentProperty.GetValue(lst, null) != null)
bodyTable.AddCell(currentProperty.GetValue(lst, null).ToString());
else
bodyTable.AddCell(string.Empty);
col++;
}
else
{
//check child property objects
var childProperties = currentType.GetProperties().Where(p => p.PropertyType.BaseType == typeof(BaseEntity));
foreach (var prop in childProperties)
{
currentProperty = prop.PropertyType.GetProperties().FirstOrDefault(p => p.Name == visibleColumn.Key);
if (currentProperty != null)
{
var currentPropertyObjectValue = prop.GetValue(lst, null);
if (currentPropertyObjectValue != null)
{
bodyTable.AddCell(currentProperty.GetValue(currentPropertyObjectValue, null).ToString());
}
else
{
bodyTable.AddCell(string.Empty);
}
break;
}
}
}
}
}
doc.Add(bodyTable);
doc.Close();
A back of the envelope computation of the memory requirements given the data you provided for memory consumption gives 100000 * 40 * (2*20+4) = 167MBs. Well within your memory limit, but it is just a lower bound. I imagine each Cell object is pretty big. If each cell would have a 512 byte overhead you could be well looking at 2GB taken. I reckon it might be even more, as PDF is a complex beast.
So you might realistically be looking at a situation where you are actually running out of memory. If not your computers, then at least the bit C# has set aside for its own thing.
I would do one thing first - check memory consumption like here. You might even do well to try with 10, 100, 1000, 10000, 100000 rows and see up until what number of rows the program works.
You could perhaps try a different thing altogether. If you're trying to print a nicely formatted table with a lot of data, perhaps you could output an HTML document, which can be done incrementally and which you can do by just writing stuff to a file, rather than using a third party library. You can then "print" that HTML document to PDF. StackOverflow to the rescue again with this problem.

Including the max and offset criteria inside GORM criteriaBuilder returns an error

Can I make this code shorter?
if(count == null && from = null) {
creditAdviceList = CreditAdvice.findAll {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count != null && from == null) {
creditAdviceList = CreditAdvice.findAll(max: count) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count == null && from != null) {
creditAdviceList = CreditAdvice.findAll(offset: from) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
else if(count != null && from != null) {
creditAdviceList = CreditAdvice.findAll(max: count, offset: from) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
}
You see, its a series if statement for each possible scenario. Imagine if one would to use also order and cache in the parameter- there will be basically 16 unique if statements!
I've tried this [more] shorter code:
creditAdviceList = CreditAdvice.findAll {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
if(count != null) {
maxResults(count)
}
if(from != null) {
firstResult(from)
}
}
But it gives me an error:
...No signature of method: grails.gorm.DetachedCriteria.maxResults() is applicable for argument types: (java.lang.Integer)...
I tried to convert offset to int, Integer, String, etc. I also omit the if statement inside the criteria, but the same error message occur.
findAll with a closure passed is using a DetachedCriteria internally, which is not the same as the result you would get from createCriteria mentioned in the docs. If groovy would find "something close" enough, it would tell you in the error message. The easiest way to deal with your max/from demands would be with simply with a map (which is the first argument passed). E.g.:
def qcfg = [:]
if (count) {
qcfg.count = count
}
if (from) {
qcfg.offset = from
}
creditAdviceList = CreditAdvice.findAll(qcfg) { ... }
mix, match, extract, shorten as you see fit
As far as I see, the only difference is the pagination options. If my eyes are not tricking me, yes, you can:
Map paginationArgs = [max: count, offset: from].findAll {
it.value != null
}
List<CreditAdvice> creditAdviceList = CreditAdvice.findAll(paginationArgs) {
ilike('id', "%$idFilter%")
.....
ilike('statusCode', statusCodeFilter)
}
You can style it differently, but basically you can build the pagination arguments first, and pass them to the findAll. No duplicated code, clearer responsability of the conditions. To clarify, I'm adding all the options and then filtering them to exclude the ones that are null.

how to avoid uitableview cell refresh flicker when reload cell

I used the monotouch.dialog to develop one PrivateMsg talk screen. But when I reload the cell , the cell flicker, how to avoid it ?
add new pm msg accross 2 step: 1. append a element cell: State.Insert(msg is sending)=> 2. reload the element cell status: State.Updated(msg send sucessced)
Pls find the attachment:screen recording file and the source code:
void HandleOnPMChanged(object sender, PMChangedEventArgs e)
{
if (e.PMSID != this.mPMSession.PMSID)
return;
this.BeginInvokeOnMainThread(() =>
{
if (e. State == State.Insert) //step1
{
element = this.GetNewPMElement(itemData);
pmSection.Add(element);
this.ScrollToBottomRow();
} else if (e.State == State.Update && e.PM != null) //step2
{
var element = FindElement(e.PM.Guid.GetHashCode());
if (element != null)
{
var indexPaths = new NSIndexPath [] { element.IndexPath };
this.TableView.ReloadRows(indexPaths, UITableViewRowAnimation.None); //this line will flicker
//remark: this.ScrollToBottomRow();
}
}
else if (e. State == State.Insert)
{
element = this.GetNewPMElement(itemData);
pmSection.Add(element);
this.ScrollToBottomRow(); //step1
}
});
}
public void ScrollToBottomRow()
{
try
{
if (pmSection.Count < 1)
return;
NSIndexPath ndxPath = pmSection[pmSection.Count - 1].IndexPath;
if (ndxPath != null)
this.TableView.ScrollToRow(ndxPath, UITableViewScrollPosition.Bottom, false); //Bottom, false);
}
catch (Exception ex)
{
Util.ReportMsg("PMDVC ScrollToBottomRow Exception:", ex.Message);
}
}
issue has been fixed.
EstimatedHeight return bigger than actual value.

Resources