Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C-将实体属性与字符串值匹配_C#_Html - Fatal编程技术网

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

C# C-将实体属性与字符串值匹配,c#,html,C#,Html,我试图生成一个HTML表,其中列数和宽度都可以自定义。我还希望能够将表中的特定列绑定到相应的列 模式示例: [发票][数量] [发票][项目名称] [发票][总金额] 最初,我试图将一个具有两个属性的对象(两个属性都是字典)传入,这一过程非常混乱 大概是这样的: public class CustomObject { public Dictionary<string, int> THTitle_Width { get; set; } public Dictionary&l

我试图生成一个HTML表,其中列数和宽度都可以自定义。我还希望能够将表中的特定列绑定到相应的列

模式示例: [发票][数量] [发票][项目名称] [发票][总金额]

最初,我试图将一个具有两个属性的对象(两个属性都是字典)传入,这一过程非常混乱

大概是这样的:

public class CustomObject {
   public Dictionary<string, int> THTitle_Width { get; set; }
   public Dictionary<string, string> InvoiceColumn_CorrespondingTHTitle { get; set; }
}
这个想法是,[Invoice].[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>
等等

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


谢谢你的帮助

最后使用了如下内容:感谢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; }
    }
}

出于好奇,您是在ASP.NET还是MVC中这样做的?这实际上是一个控制台应用程序,生成HTML,然后转换为PDF。所有转换功能都存在。只需添加轻松自定义显示/隐藏列和控制列宽的功能。
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; }
    }
}