C# - 将实体属性与字符串值匹配

时间:2014-10-08 22:27:34

标签: c# html

我正在尝试生成一个HTML表格,其中可以自定义列数和宽度。我也希望能够将表中的特定列绑定到相应的列...

示例架构
[发票]。[数量]
[发票] [ITEMNAME]
[发票]。[总金额]

最初,我试图传递一个带有两个属性(都是字典)的对象。

这样的事情:

public class CustomObject {
   public Dictionary<string, int> THTitle_Width { get; set; }
   public Dictionary<string, string> InvoiceColumn_CorrespondingTHTitle { get; set; }
}

我想要实现类似的目标:

THTitle_Width.Add("My Quantity Header", 100);
InvoiceColumn_CorrespondingTHTitle.Add("Quantity", "My Quantity Header");

我们知道[发票]。[数量]列中的数据属于表格列“我的数量标题”,而“我的数量标题”列的宽度为100(px)。

然后输出:

<tr><th style="width:100px;">My Quantity Header</th></tr>
<tr><td>1</td></tr>
<tr><td>15</td></tr>
<tr><td>3</td></tr>

等...

原因是,您可以传入此自定义对象,并处理除此之外的所有内容。这可能吗?也许不是这样,而是另一个?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

使用以下内容结束:(感谢Stefan Hoffmann)

namespace Samples
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;

    public class Sample
    {
        public static void Main()
        {
            DataTable data = GetTable();
            HtmlTable html = new HtmlTable()
                .BindTable(data)
                .BindColumn("ID", "Ident", 64)
                .BindColumn("Name", "First name", 128)
                .BindColumn("Description", "Things to know", 256);
            Console.WriteLine(html.BuildHtml());
            Console.ReadLine();
        }

        private static DataTable GetTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("ID", typeof(int));
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Description", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));            
            table.Rows.Add(1, "Alice", "Likes encryption.", DateTime.Now);
            table.Rows.Add(2, "Bob", "Has to decrypt a lot.", DateTime.Now);
            table.Rows.Add(3, "Charlie", "Not really funny.", DateTime.Now);
            table.Rows.Add(4, "David", "Knows how to use stones.", DateTime.Now);
            table.Rows.Add(5, "Elmer", "Don't mess with him, when wearing bunny ears.", DateTime.Now);
            return table;
        }
    }

    public class HtmlTable
    {
        private List<HtmlTableColumn> columns = new List<HtmlTableColumn>();
        private DataTable dataTable= null;

        public HtmlTable BindTable(DataTable dataTable)
        {
            this.dataTable = dataTable;
            return this;
        }

        public HtmlTable BindColumn(string columnName, string caption, int width)
        {
            this.columns.Add(new HtmlTableColumn() { Caption = caption, ColumnName = columnName, Width = width });
            return this;
        }

        public string BuildHtml()
        {
            StringBuilder result = new StringBuilder();
            // Build your HTML table.
            return result.ToString();
        }
    }

    public class HtmlTableColumn
    {
        public string Caption { get; set; }
        public string ColumnName { get; set; }
        public int Width { get; set; }
    }
}