小隐的博客

人生在世,笑饮一生
随笔 - 304, 文章 - 0, 评论 - 349, 阅读 - 50万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

c#通过数据集生成浏览页面

Posted on   隐客  阅读(511)  评论(0编辑  收藏  举报

在做项目时,因为有时候要查看某一个订单的详细信息,不知道如何显示!

于是做了一个公共方法,生成一个html表格,如图:

 

 

 

 公共方法代码:

复制代码
    public class htmlTable
    {
        
public htmlTable ()
        {
            Title 
= "信息";
            Type 
= "0";
            MarkField 
= "";
            MarkRowSpan 
= "3";
            Width 
="100%";
            FieldMap 
= new DataTable("FieldMap");
            Data 
= new DataTable("Data");
        }
        
public string Title {get;set;}
        
public string  Type {get;set;} //0为主表 1为明细表
        public string MarkField { getset; }  //需要大框,比如备注
        public string MarkRowSpan { getset; }  //大框的高度,以行数计算
        public string Width { getset; }
        
        
public DataTable FieldMap { getset; }  //字段对照,如果没有,则以字段列表名为准
        public DataTable Data { getset; }   //数据

    }
复制代码

 

 

         //返回一个html表格

复制代码
        public static string GetHtmlTable(htmlTable ht)
        {
            
if (ht.Data == null || ht.FieldMap == null)
            {
                
return "";
            }
            
if (ht.Data.Rows.Count ==0)
            {
                
return "";
            }
            
//ht.FieldHide = ht.FieldHide.ToLower();

            
//处理字段对应
            
//string map="";
            Dictionary <string,string> dic=new Dictionary<string,string> ();
            
for (int i=0;i<ht.FieldMap.Rows.Count ;i++)
            {
                DataRow dr
=ht.FieldMap.Rows[i];
                
//map+=dr["columnname"].ToString ()  + "," + dr["ColumnDesc"].ToString () + "|"; 
                if (!string.IsNullOrEmpty(dr["columnname"].ToString()) && !string.IsNullOrEmpty(dr["ColumnDesc"].ToString()))
                {
                    dic.Add (dr[
"columnname"].ToString (),dr["ColumnDesc"].ToString ());
                }
                
            }
            StringBuilder sbd 
= new StringBuilder();
            sbd.AppendLine(
"<table width=\"" + ht.Width + "\" name=\"mytable\" cellspacing=\"0\" >");

            
#region 主体
            
if (ht.Type == "0")
            {
                
if (ht.Data.Rows.Count == 0)
                    
return "";

                
//生成标题
                sbd.AppendLine("<tr>");
                sbd.AppendLine(
"<th scope=\"row\" class=\"title\" colspan=2>" + ht.Title + "</th>");
                sbd.AppendLine(
"</tr>");

                
//循环字段
                DataRow dr = ht.Data.Rows[0];
                
for (int i = 0;i< ht.Data.Columns.Count; i++)
                {
                    
string colName = ht.Data.Columns[i].ColumnName;
                    
string colValue = dr[colName].ToString();
                    
string colMapValue = "";
                    
if (dic.Keys.Contains (colName) )
                    {
                        colMapValue 
= dic[colName];
                    }
                    
else
                    {
                        colMapValue 
= colName;
                    }
                    
////先判断是否要隐藏
                    //if (ht.FieldHide.IndexOf(colName.ToLower()) >= 0 || ht.FieldHide.IndexOf(colMapValue.ToLower()) >= 0)
                    
//{
                    
//    continue;
                    
//}
                    
//生成一行
                    sbd.AppendLine("<tr>");
                    sbd.AppendLine(
"<th scope=\"row\" width=100  class=\"specalt\">" + colMapValue + "</th>");
                    sbd.AppendLine(
"<td class=\"alt\" >" + colValue  +  "</td>");                    
                    sbd.AppendLine(
"</tr>");

                }
                
                

            }
            
#endregion 

            
#region 明细
            
else if (ht.Type == "1")
            {
 

                
//判断有多少列
                int cols = ht.Data.Columns.Count;

                
//生成标题
                sbd.AppendLine("<tr>");
                sbd.AppendLine(
"<th scope=\"row\"  class=\"title\" colspan=" + cols + ">" + ht.Title + "</th>");
                sbd.AppendLine(
"</tr>");

                
//生成列表头
                sbd.AppendLine("<tr>");
                
for (int i = 0; i < cols; i++)
                {
                    
string colName = ht.Data.Columns[i].ColumnName;
                    
string colMapValue = "";
                    
if (dic.Keys.Contains(colName))
                    {
                        colMapValue 
= dic[colName];
                    }
                    
else
                    {
                        colMapValue 
= colName;
                    }
                    sbd.AppendLine(
"<th scope=\"col\"  class=\"specalt\" >" + colMapValue + "</th>");                    
                }
                sbd.AppendLine(
"</tr>");

                
//生成数据                
                for (int i = 0; i < ht.Data.Rows.Count; i++)
                {
                    DataRow dr 
= ht.Data.Rows[i];

   
                    
//生成一行
                    sbd.AppendLine("<tr>");
                    
for (int j = 0; j < cols; j++)
                    {
                        
string colValue = dr[j].ToString();                        
                        sbd.AppendLine(
"<td class=\"alt\" >" + colValue + "</td>");

                    }
                    sbd.AppendLine(
"</tr>");
                }

    
            }
            
#endregion


            sbd.AppendLine(
"</table>");
            
return sbd.ToString ();
        }
        
        
        
/// <summary>返回多个表格
        
///  
        
/// </summary>
        
/// <param name="htl"></param>        
        
/// <returns></returns>
        public static string GetHtmlTable(List< htmlTable> htl)
        {
            
if (htl.Count == 0return "0";
            StringBuilder sb 
= new StringBuilder();
            
for (int i = 0; i < htl.Count; i++)
            {
                sb.AppendLine(GetHtmlTable(htl[i]));
            }
            
return sb.ToString();
        }
        
public static string GetHtmlTable(DataSet ds,string type)
        {
            List
<htmlTable> htl = new List<htmlTable>();
            
int count=ds.Tables.Count ;
            
string[] t = type.Split(new string[] { "|" }, StringSplitOptions.RemoveEmptyEntries);
            
for (int i = 0; i < t.Length; i++)
            {
                
string[] p = t[i].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                
if (p.Length != 4)
                    
continue;
                htmlTable ht 
= new htmlTable();
                ht.Type 
= p[0];
                
int t1=Convert.ToInt32(p[1]);
                
int t2=Convert.ToInt32(p[2]);
                
if (t1 > count || t2 > count)
                    
continue;
                ht.Data 
= ds.Tables[t1];
                ht.FieldMap 
= ds.Tables[t2];
                ht.Title 
= p[3];
                htl.Add(ht);

            }

            
return GetHtmlTable(htl);
        }
复制代码

 

 调用:

tb = CommonInfo.GetHtmlTable(ds, "0,0,1,运输信息|0,2,3,派车信息");

 

 

 

样式表:

复制代码
 
/* CSS Document */
#mytable 
{
padding
: 0;
margin
: 0;
}

th.title 
{
font
: bold 16px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color
: #4f6b72;
border-right
: 1px solid #C1DAD7;
border-bottom
: 1px solid #C1DAD7;
border-top
: 1px solid #C1DAD7;
border-left
: 0px;
letter-spacing
: 2px;
text-transform
: uppercase;
text-align
: left;
padding
: 6px 6px 6px 12px;
background
: #f5fada url(bg_heade1r.jpg) no-repeat;
}


td.alt 
{
border-right
: 1px solid #C1DAD7;
border-bottom
: 1px solid #C1DAD7;
font-size
:12px;
background
:#fff;
color
: #797268;
padding
: 6px 6px 6px 12px;
}

th.specalt 
{
border-right
: 1px solid #C1DAD7;
border-bottom
: 1px solid #C1DAD7;
border-top
: 0px solid #C1DAD7;
border-left
: 1px solid #C1DAD7;
 letter-spacing
: 0px;
text-transform
: uppercase;
text-align
: left;
padding
: 6px 6px 6px 12px;
background
: #f5fafa ;
font
: 12px "Trebuchet MS", Verdana, Arial, Helvetica, sans-serif;
color
: #797268;
}
/*---------for IE 5.x bug* #F5FADA/
html>body td{ font-size:11px;}
 
复制代码

 

 

编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
QQ交流
点击右上角即可分享
微信分享提示