c# - Insert new line every 4 items in a string? -
i have string sets thumbnails 1 after other no line breaks. images wrap downwards next line 1 time reach max width of area in. want insert line break after every 4th thumbnail, , code have works, keeps adding new line after 1st image -- looks wrong. 7 thumbnail images, there should 1 line break added , after 4th image.
here class info used phone call upon , build string thumbnail info sql database:
protected list<glassitem> getglassitems(guid currentpage) { var items = new list<glassitem>(); using (mysqlconnection cn = new mysqlconnection(configurationmanager.connectionstrings["sitefinity"].tostring())) { cn.open(); mysqlcommand cmd = new mysqlcommand("select id, brandid, glassname, thumbnail, largeimage, ordinal, (select window_brands.brandname window_brands window_brands.brandpage = brandid) brandname window_brand_cutglass brandid = ?pageid , thumbnail not null , largeimage not null order ordinal", cn); cmd.parameters.add(new mysqlparameter("pageid", currentpage.tostring())); using (mysqldatareader reader = cmd.executereader()) { while (reader.read()) { if (reader["thumbnail"].tostring().length > 1 && reader["largeimage"].tostring().length > 1) { items.add(new glassitem { largeimagepath = revere.getimagepath(reader["largeimage"].tostring()), thumbnailpath = revere.getimagepath(reader["thumbnail"].tostring()), glassname = reader["glassname"].tostring() }); } } } cn.close(); } homecoming items; } this bit of code makes utilize of getglassitems class. new line meant inserted after every 4th item, adding new line after first item:
protected string buildcutglass(guid currentpage) { var items = getglassitems(new guid(currentpage.tostring())); stringbuilder glass = new stringbuilder(); (int = 0; < items.count; i++) { glass.append(string.format("<div class='glassitem'><a href='{0}' class='cutglasspopup icon'><img src='{1}' alt='{2}' width='77' height='77' /></a><a href='{0}' class='cutglasspopup'>{2}</a></div>", items[i].largeimagepath, items[i].thumbnailpath, items[i].glassname)); console.write("count: " + items.count.tostring() + "<br />"); if (i == 4) { // new line every 4 items glass.append("<div style='clear: both;'></div>"); } } homecoming glass.tostring(); } how can edit above code new line no longer beingness added after first thumbnail?
you need check modulus operator.
if (i % 4 == 0) this allow insert new row every 4th row.
but insert new line first item well, since i 0 , 0 % 4 homecoming 0. modify check as:
if(i != 0 && % 4 == 0) your current check i == 4 add together line after first 4 lines.
c# sql count newline stringbuilder
No comments:
Post a Comment