我想在C#中使用字符串构建器动态创建表,并为其分配动态值 [英] I want to create table dynamically using string builder in C# and assign dynamic values to it

查看:89
本文介绍了我想在C#中使用字符串构建器动态创建表,并为其分配动态值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I want to create table dynamically using string builder in c# and assign dynamic values to It



但是输出没有显示正确的格式



我的尝试:




But output is not showing proper format

What I have tried:

if (dtJobWork1 != null)
           {
               for (int i = 0; i < dtJobWork1.Rows.Count; i++)
               {
                   if (dtJobWork1.Rows[i]["oType"] != null)
                   {
                       strOType = dtJobWork1.Rows[i]["oType"].ToString();
                   }

                   if (dtJobWork1.Rows[i]["OrderTypeAbbr"] != null)
                   {
                       StrOrderTypeAbbr = dtJobWork1.Rows[i]["OrderTypeAbbr"].ToString();
                   }
                   if (dtJobWork1.Rows[i]["OrderStatus"] != null)
                   {
                       StrOrderStatus = dtJobWork1.Rows[i]["OrderStatus"].ToString();
                       StrOrderStatus = "Test";
                   }
                   else
                   {
                       StrOrderStatus = "Test";
                   }
                   if (dtJobWork1.Rows[i]["OrderComments"] != null)
                   {
                       StrOrderComments = dtJobWork1.Rows[i]["OrderComments"].ToString();
                   }
                   if (dtJobWork1.Rows[i]["vNum"] != null)
                   {
                       StrvNum = dtJobWork1.Rows[i]["vNum"].ToString();
                   }

                   if (dtJobWork1.Rows[i]["SiteName"] != null)
                   {
                       StrSiteName = dtJobWork1.Rows[i]["SiteName"].ToString();
                   }

                   if (dtJobWork1.Rows[i]["SiteAddress"] != null)
                   {
                       StrSiteAddress = dtJobWork1.Rows[i]["SiteAddress"].ToString();
                   }

                   if (dtJobWork1.Rows[i]["CurrName"] != null)
                   {
                       StrCurrName = dtJobWork1.Rows[i]["CurrName"].ToString();
                   }
                   if (dtJobWork1.Rows[i]["OrderDate"] != null)
                   {
                       strODate = DateTime.Parse(dtJobWork1.Rows[i]["OrderDate"].ToString()).ToString("dd/MMM/yyyy");
                   }

                   if (dtJobWork1.Rows[i]["DueDate"] != null)
                   {
                       strDueDate = DateTime.Parse(dtJobWork1.Rows[i]["DueDate"].ToString()).ToString("dd/MMM/yyyy");
                   }
                   strTrCurr = dtJobWork1.Rows[i]["Currency"].ToString();

               }//for close


               #region -- Main table created
               strLblTbl.Append("<div id='divMain'>");

               if (strIsForex == "1")
               {
                   strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='width:800px;border:1px solid silver;font-family:Arial;font-size:12px;'>");
               }
               else
               {
                   strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='width:800px;border:1px solid silver;font-family:Arial;font-size:12px;'>");
               }
               #endregion

               #region -- table First Row
               strLblTbl.Append("<tr style='background-color:White;font-size:12px;font-weight:bold;'>");
               strLblTbl.Append("<td style='width:100%;height:30px;border-bottom:solid 1px silver;' valign='middle'>");

               strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='text-indent:5px;width:100%'>");
               strLblTbl.Append("<tr>");
               strLblTbl.Append("<td align = 'left' style='width:150px;'>" + strOID + "</td>");
               //if (strOType == "PO")
               //{
               strLblTbl.Append("<td style='width:50px;  ' align = 'left'>" + strOType + "</td>");

               //< div id = 'box' class='RounDiv2'></div>

               //}
               //if (strOType == "SO")
               //{
               //    strLblTbl.Append("<td style='width:50px;  ' align = 'left'><div id ='box' class='RounDiv3'></div>" + strOType + "</td>");
               //}
               //if (strIsForex == "1")
               //{
               //    decimal exchRate = 0;
               //    decimal.TryParse(strExchRATE, out exchRate);
               //    strLblTbl.Append("<td style='color:maroon;width:100px;' align = 'right'>" + strTrCurr + "</td>");
               //    //strLblTbl.Append("<td style='width:100px;' align = 'right'>" + Math.Abs(decimal.Parse(strExchRATE)).ToString("#,#0.0000#") + "  </td>");
               //    strLblTbl.Append("<td style='width:100px;' align = 'left'>@  " + GetTenDecimalValue(Math.Abs(exchRate)).ToString("#,#0.0000000000") + "  </td>");
               //}
               //else
               //{
               //    strLblTbl.Append("<td style='color:maroon;width:100px;' align = 'right'></td>");
               //    strLblTbl.Append("<td style='width:100px;' align = 'right'></td>");
               //}
               //if (strOstatus == "")
               //{
               //    strLblTbl.Append("<td style='width:200px;' align = 'right'></td>");
               //}
               //else
               //{
               strLblTbl.Append("<td style='width:200px;' align = 'right'>" + SiteName + " " + "[" + CurrencyName + "]" + "  </td>");
               //}

               strLblTbl.Append("</tr>");
               strLblTbl.Append("</table>");

               strLblTbl.Append("</td>");
               strLblTbl.Append("</tr>");
               #endregion


               #region -- table First Row
               //if (HdnSetting.Value.Trim() == "1")
               //{
               strLblTbl.Append("<tr style='background-color:White;font-size:12px;font-weight:bold;'>");
               strLblTbl.Append("<td style='width:100%;height:30px;border-bottom:solid 1px silver;' valign='middle'>");

               strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='text-indent:5px;'>");
               strLblTbl.Append("<tr>");
               strLblTbl.Append("<td align = 'left' style='width:150px;'>" + StrvNum + "</td>");
               strLblTbl.Append("<td style='width:50px;  ' align = 'left'><div id ='box'></div></td>");


               strLblTbl.Append("<td style='color:maroon;width:100px;' align = 'right'></td>");
               strLblTbl.Append("<td style='width:100px;' align = 'right'></td>");
               strLblTbl.Append("<td style='width:200px;' align = 'right'></td>");
               strLblTbl.Append("</tr>");
               strLblTbl.Append("</table>");

               strLblTbl.Append("</td>");
               strLblTbl.Append("</tr>");

               //}
               #endregion
               #region -- table First Row
               strLblTbl.Append("<tr style='background-color:White;font-size:12px;font-weight:bold;'>");
               strLblTbl.Append("<td style='width:100%;height:30px;border-bottom:solid 1px silver;' valign='middle'>");

               strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='text-indent:5px;width:100%'>");
               strLblTbl.Append("<tr>");
               strLblTbl.Append("<td align = 'left' style='width:150px;'>" + strODate + "</td>");
               strLblTbl.Append("<td style='width:50px;  ' align = 'left'>" + StrOrderStatus + "</td>");
               strLblTbl.Append("<td style='width:200px;' align = 'right'>" + strDueDate + "</td>");
               strLblTbl.Append("</tr>");
               strLblTbl.Append("</table>");
               strLblTbl.Append("</td>");
               strLblTbl.Append("</tr>");

               #endregion
               #region -- table First Row
               //if (HdnSetting.Value.Trim() == "1")
               //{
               strLblTbl.Append("<tr style='background-color:White;font-size:12px;font-weight:bold;'>");
               strLblTbl.Append("<td style='width:100%;height:30px;border-bottom:solid 1px silver;' valign='middle'>");

               strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='text-indent:5px;'>");
               strLblTbl.Append("<tr>");
               strLblTbl.Append("<td align = 'left' style='width:150px;'>" + StrOrderComments + "</td>");
               strLblTbl.Append("<td style='width:50px;  ' align = 'left'><div id ='box'></div></td>");


               strLblTbl.Append("<td style='color:maroon;width:100px;' align = 'right'></td>");
               strLblTbl.Append("<td style='width:100px;' align = 'right'></td>");
               strLblTbl.Append("<td style='width:200px;' align = 'right'></td>");
               strLblTbl.Append("</tr>");
               strLblTbl.Append("</table>");

               strLblTbl.Append("</td>");
               strLblTbl.Append("</tr>");

               //}
               #endregion
           }//if close dtJobWork1 table
           if (dtJobWork2 != null)
           {
               if (dtJobWork2.Rows.Count > 0)
               {
                   #region -- table 4th Row to show Item details Table
                   //  Item Details Table Header
                   strLblTbl.Append("<tr style='background-color:White;font-weight:bold;font-size:12px;'>");
                   strLblTbl.Append("<td style='border-top:solid 1px silver;'>");

                   strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='width:800px; text-indent:5px;'>");


                   if (strIsForex == "1")
                   {
                       strLblTbl.Append("<tr><td colspan='8'  style='padding-top:5px;padding-bottom:5px;' >Item Details</td></tr>");
                       strLblTbl.Append("<tr style='background-color:WhiteSmoke;'>");
                       strLblTbl.Append("<td style='height:18px;width:30px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>#</td>");

                       strLblTbl.Append("<td style='height:18px;width:130px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item</td>");

                       //strLblTbl.Append("<td style='height:18px;width:145px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item</td>");
                       strLblTbl.Append("<td style='height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item Code</td>");
                       if (countryCode.Equals("1"))
                       {
                           //strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; '>HSN Code  </td>");
                           strLblTbl.Append("<td style='height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; '>HSN Code  </td>");
                       }
                       else
                       {
                           //strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; '>GST Code  </td>");
                           strLblTbl.Append("<td style='height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; '>GST Code  </td>");

                       }


                       strLblTbl.Append("<td style='height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>UMO   </td>");


                       //strLblTbl.Append("<td style='height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Quantity  </td>");

                       strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Quantity  </td>");

                       //strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Rate  </td>");

                       strLblTbl.Append("<td style='height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Rate  </td>");

                       //strLblTbl.Append("<td style='height:18px;width:110px; border-bottom:solid 1px silver;  border-top:solid 1px silver;border-right:solid 1px silver; ' align = 'right'>Amount [" + strTrCurr + "]  </td>");
                       strLblTbl.Append("<td style='height:18px;width:100px; border-bottom:solid 1px silver;  border-top:solid 1px silver;border-right:solid 1px silver; ' align = 'right'>Amount [" + strTrCurr + "]  </td>");

                       //strLblTbl.Append("<td class='base' style='height:18px;width:110px; border-bottom:solid 1px silver;  border-top:solid 1px silver; ' align = 'right'>Amount [" + BaseCurr + "]  </td>");

                       strLblTbl.Append("<td class='base' style='height:18px;width:100px; border-bottom:solid 1px silver;  border-top:solid 1px silver; ' align = 'right'>Amount [" + BaseCurr + "]  </td>");
                   }
                   else
                   {
                       strLblTbl.Append("<tr><td colspan='7'  style='padding-top:5px;padding-bottom:5px;' >Item Details</td></tr>");
                       strLblTbl.Append("<tr style='background-color:WhiteSmoke;'>");
                       strLblTbl.Append("<td style='height:18px;width:30px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>#</td>");

                       //strLblTbl.Append("<td style='height:18px;width:205px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item</td>");
                       strLblTbl.Append("<td style='height:18px;width:180px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item</td>");

                       strLblTbl.Append("<td style='height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Item Code</td>");

                       strLblTbl.Append("<td style='height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>UMO</td>");

                       if (countryCode.Equals("1"))
                       {
                           //strLblTbl.Append("<td style='height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver;text-indent:5px; ' >HSN Code  </td>");
                           strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver;text-indent:5px; ' >HSN Code  </td>");
                       }
                       else
                       {
                           //strLblTbl.Append("<td style='height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver;text-indent:5px; ' >GST Code  </td>");
                           strLblTbl.Append("<td style='height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver;text-indent:5px; ' >GST Code  </td>");

                       }
                       //strLblTbl.Append("<td style='height:18px;width:110px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Quantity  </td>");
                       strLblTbl.Append("<td style='height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Quantity  </td>");

                       //strLblTbl.Append("<td style='height:18px;width:110px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Rate  </td>");
                       strLblTbl.Append("<td style='height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>Rate  </td>");

                       //strLblTbl.Append("<td style='height:18px;width:110px; border-bottom:solid 1px silver;  border-top:solid 1px silver; ' align = 'right'>Amount  </td>");
                       strLblTbl.Append("<td style='height:18px;width:100px; border-bottom:solid 1px silver;  border-top:solid 1px silver; ' align = 'right'>Amount  </td>");
                   }
                   //strLblTbl.Append("<td style='width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Action</td>");
                   strLblTbl.Append("</tr>");
                   strLblTbl.Append("</table>");

                   strLblTbl.Append("</td>");
                   strLblTbl.Append("</tr>");
                   //
                   #endregion

                   int strSrno = 0;
                   //Added by prince on 28/08/2016
                   string strHSNCode = string.Empty;
                   if (dtJobWork2 != null)
                   {
                       for (int i = 0; i < dtJobWork2.Rows.Count; i++)
                       {
                           if (dtJobWork2.Rows[i]["ItemId"] != null)
                           {
                               StrItemId = dtJobWork2.Rows[i]["ItemId"].ToString();
                           }
                           else
                           {
                               StrItemId = "0";
                           }

                           if (dtJobWork2.Rows[i]["ItemName"] != null)
                           {
                               StrItemName = dtJobWork2.Rows[i]["ItemName"].ToString();
                           }
                           else
                           {
                               StrItemName = "";
                           }
                           if (dtJobWork2.Rows[i]["ItemCode"] != null)
                           {
                               StrItemCode = dtJobWork2.Rows[i]["ItemCode"].ToString();
                           }
                           else
                           {
                               StrItemCode = "";
                           }
                           if (dtJobWork2.Rows[i]["HSNCode"] != null)
                           {
                               StrHSNCode = dtJobWork2.Rows[i]["HSNCode"].ToString();
                           }
                           else
                           {
                               StrHSNCode = "";
                           }

                           if (dtJobWork2.Rows[i]["UOM"] != null)
                           {
                               StrUOM = dtJobWork2.Rows[i]["UOM"].ToString();
                           }
                           else
                           {
                               StrUOM = "";
                           }

                           if (dtJobWork2.Rows[i]["Qty"] != null)
                           {
                               strQuantity = dtJobWork2.Rows[i]["Qty"].ToString();
                           }
                           else
                           {
                               strQuantity = "";
                           }

                           if (dtJobWork2.Rows[i]["Rate"] != null)
                           {
                               strRate = dtJobWork2.Rows[i]["Rate"].ToString();
                           }
                           else
                           {
                               strRate = "";
                           }

                           if (dtJobWork2.Rows[i]["Amount"] != null)
                           {
                               strTotalAmt = dtJobWork2.Rows[i]["Amount"].ToString();
                           }
                           else
                           {
                               strTotalAmt = "";
                           }


                           // Calculations

                           if (strQuantity.Trim() != "")
                           {
                               DecQty = Convert.ToDecimal(strQuantity);
                               DecSumQuantity = DecSumQuantity + DecQty;
                           }
                           else
                           {
                               DecSumQuantity = 0;
                           }


                           if (strRate.Trim() != "")
                           {
                               DecRate = decimal.Parse(strRate);
                           }
                           else
                           {
                               DecRate = 0;
                           }

                           if(strTotalAmt.Trim()!="")
                           {
                               DecAmount = decimal.Parse(strTotalAmt);
                               DecSumAmt = DecSumAmt + DecAmount;


                           }


                           strSrno = i + 1;


                           #region -- table 4th Row to show Item details Table
                           // Item Details Table Data
                           strLblTbl.Append("<tr style='background-color:White;font-size:12px;'>");
                           strLblTbl.Append("<td>");

                           strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='width:800px'>");
                           strLblTbl.Append("<tr>");
                           if (strIsForex == "1")
                           {
                               strLblTbl.Append("<td style= 'height:18px;width:30px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'left'>" + strSrno + "</td>");
                               //strLblTbl.Append("<td style=' height:18px;width:145px; border-bottom:solid 1px silver;  border-right:solid 1px silver;' align = 'left'><div style='margin-left:5px;'>" + StrItemName + "</div></td>");
                               strLblTbl.Append("<td style=' height:18px;width:130px; border-bottom:solid 1px silver;  border-right:solid 1px silver;' align = 'left'><div style='margin-left:5px;'>" + StrItemName + "</div></td>");

                               strLblTbl.Append("<td style=' height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; text-indent:5px;' align = 'left'>" + StrItemCode + "</td>");
                               if (countryCode.Equals("1"))
                               {
                                   //strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; '>" + StrHSNCode + "  </td>");
                                   strLblTbl.Append("<td style=' height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; '>" + StrHSNCode + "  </td>");
                               }
                               else
                               {
                                   //strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; '>" + strGST + "  </td>");
                                   strLblTbl.Append("<td style=' height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; '>" + strGST + "  </td>");

                               }

                               strLblTbl.Append("<td style=' height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; '>" + StrUOM + "  </td>");
                               //strLblTbl.Append("<td style='height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'right'>UMO   </td>");


                               //strLblTbl.Append("<td style=' height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecQty)) + "  </td>");
                               strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecQty)) + "  </td>");
                               //strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'right'>" + currencyConverter(GetTwoDecimalValue(Math.Abs(DecRate))) + "  </td>");
                               strLblTbl.Append("<td style=' height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'right'>" + currencyConverter(GetTwoDecimalValue(Math.Abs(DecRate))) + "  </td>");

                               strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;border-right:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecAmount)) + "  </td>");
                               //strLblTbl.Append("<td class='base' style=' height:18;width:110px; border-bottom:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(TotAmtB)) + "  </td>");
                           }
                           else
                           {
                               strLblTbl.Append("<td style= 'height:18px;width:30px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'left'>" + strSrno + "</td>");
                               strLblTbl.Append("<td style=' height:18px;width:180px; border-bottom:solid 1px silver;  border-right:solid 1px silver;' align = 'left'><div style='margin-left:5px;'>" + StrItemName + "</div></td>");
                               strLblTbl.Append("<td style=' height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; text-indent:5px;' align = 'left'>" + StrItemCode + "</td>");

                               strLblTbl.Append("<td style=' height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; text-indent:5px;' align = 'left'>" + StrUOM + "</td>");

                               //strLblTbl.Append("<td style='height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>UMO</td>");


                               if (countryCode.Equals("1"))
                               {
                                   //strLblTbl.Append("<td style=' height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' >" + StrHSNCode + "  </td>");
                                   strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' >" + StrHSNCode + "  </td>");
                               }
                               else
                               {
                                   //strLblTbl.Append("<td style=' height:18px;width:90px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' >" + strGST + "  </td>");
                                   strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' >" + strGST + "  </td>");

                               }
                               //strLblTbl.Append("<td style=' height:18px;width:110px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecQty)) + "  </td>");
                               strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecQty)) + "  </td>");

                               //strLblTbl.Append("<td style=' height:18px;width:110px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'right'>" + currencyConverter(GetTwoDecimalValue(Math.Abs(DecRate))) + "  </td>");

                               strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver;text-indent:5px; ' align = 'right'>" + currencyConverter(GetTwoDecimalValue(Math.Abs(DecRate))) + "  </td>");

                               //strLblTbl.Append("<td style=' height:18px;width:110px; border-bottom:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecAmount)) + "  </td>");
                               strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;text-indent:5px;' align = 'right'>" + currencyConverter(Math.Abs(DecAmount)) + "  </td>");

                           }

                           // strLblTbl.Append("<td style='width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver; border-top:solid 1px silver; ' align = 'left'>Action</td>");
                           strLblTbl.Append("</tr>");

                           strLblTbl.Append("</table>");

                           strLblTbl.Append("</td>");
                           strLblTbl.Append("</tr>");
                           #endregion
                       } //for close
                   }//if close

                   strIT = strSumTotalAmt;
                   strIB = strSumTotalAmtB;
                   #region -- table 4th Row to show Item details Table footer ROw Total

                   strLblTbl.Append("<tr style='background-color:White;font-size:12px;'>");
                   strLblTbl.Append("<td>");

                   strLblTbl.Append("<table cellspacing='0' cellpadding='0' style='width:800px; text-indent:5px;'>");
                   strLblTbl.Append("<tr style='background-color:WhiteSmoke;font-weight:Bold;'>");
                   if (strIsForex == "1")
                   {
                       strLblTbl.Append("<td style= 'height:18px;width:30px; border-bottom:solid 1px silver; border-right:solid 1px silver;  ' align = 'left'> </td>");


                       strLblTbl.Append("<td style=' height:18px;width:130px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'>Total</td>");

                       strLblTbl.Append("<td style=' height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");


                       strLblTbl.Append("<td style=' height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");

                       strLblTbl.Append("<td style=' height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");

                       strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver;' align = 'right'>  </td>");
                       strLblTbl.Append("<td style=' height:18px;width:70px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'>  </td>");
                       strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;border-right:solid 1px silver;' align = 'right'>" + currencyConverter(Math.Abs(DecSumAmt)) + "  </td>");
                       strLblTbl.Append("<td class='base' style=' height:18px;width:100px; border-bottom:solid 1px silver;' align = 'right'>" + currencyConverter(Math.Abs(strSumTotalAmtB)) + "  </td>");
                   }
                   else
                   {
                       strLblTbl.Append("<td style= 'height:18px;width:30px; border-bottom:solid 1px silver;border-right:solid 1px silver;   ' align = 'left'> </td>");
                       strLblTbl.Append("<td style=' height:18px;width:180px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'>Total</td>");
                       strLblTbl.Append("<td style=' height:18px;width:60px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");

                       strLblTbl.Append("<td style=' height:18px;width:55px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");

                       strLblTbl.Append("<td style=' height:18px;width:80px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'> </td>");

                       strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;' align = 'right'>" + currencyConverter(Math.Abs(DecSumQuantity)) + "  </td>");
                       //strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver;' align = 'right'>  </td>");
                       strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;  border-right:solid 1px silver; ' align = 'left'>  </td>");
                       strLblTbl.Append("<td style=' height:18px;width:100px; border-bottom:solid 1px silver;' align = 'right'>" + currencyConverter(Math.Abs(DecSumAmt)) + "  </td>");
                   }

                   strLblTbl.Append("</tr>");
                   strLblTbl.Append("</table>");

                   strLblTbl.Append("</td>");
                   strLblTbl.Append("</tr>");
                   #endregion
               }
           }

推荐答案

If it were me, I’d create a class that represents your row data, and that has properties defined with appropriate types that match the column definitions in the datatable:



If it were me, I'd create a class that represents your row data, and that has properties defined with appropriate types that match the column definitions in the datatable:

public class MyItem
{
    public string OType { get; set; }
    ....
    public DateTime DueDate { get; set; }
}





The constructor for that class would be something like this:



The constructor for that class would be something like this:

public MyItem(DataRow row)
{
    this.OType = this.GetColumnValue("oType", "UNK");
    ... 
    this.DueDate = this.GetColumnValue("DueDate", new DateTime(0));
}

public T GetColumnValue<t>(DataRow row, string colName, T defaultValue)
{
    object value = row[colName];
    T result = (value != null) ? (T)value : defaultValue;
    return result;
}



And add a property that returns the html table row already formatted.


And add a property that returns the html table row already formatted.

public string AsHtmlTableRow
{
    get
    {
        StringBuilder html = new StringBuilder();
        html.AppendLine("    <tr>");
        html.AppendFormat("        <td (with cell styles)>{0}</td>", this.OType).AppendLine();
        ...
        html.AppendFormat("        <td (with cell styles)>{0}</td>", this.DueDate.ToString("dd/MM/yyyy")).AppendLine();
        html.AppendLine("    </tr>");
        return html.ToString();
    }
}





At that point, all you have to do is this





At that point, all you have to do is this

List<MyItem> items = new List<MyItem>();
foreach(DataRow row in dataTable.Rows)
{
    items.Add(new MyItem(row));
}

StringBuilder table = new StringBuilder();

table.AppendLine("<table (with table styles)>");

foreach(MyItem item in items)
{
    table.Append(item.AsHtmlDataRow);
}
table.AppendLine("</table>");





Keep in mind that the code above is just generalized pseudo code, so you you’ll need to tweak it here and there, and mold it to your specific needs. I don’t feel the need to do that myself since it’s your code, and I feel a strong need right now to take a righteous dump.



BTW, a minor nit - create some CSS style classes so that your html isn’t fulll of repetitive CSS elements. That just trashes up the html and makes it hard to read.



Keep in mind that the code above is just generalized pseudo code, so you you'll need to tweak it here and there, and mold it to your specific needs. I don't feel the need to do that myself since it's your code, and I feel a strong need right now to take a righteous dump.

BTW, a minor nit - create some CSS style classes so that your html isn't fulll of repetitive CSS elements. That just trashes up the html and makes it hard to read.


这篇关于我想在C#中使用字符串构建器动态创建表,并为其分配动态值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆