我正在尝试实现一个方法,它将接受带有StringTemplate的DataTable并返回数据的字符串表示形式。
我在这里找到了如何做到这一点here和here,但对我来说不起作用。
示例代码:
我在这里找到了如何做到这一点here和here,但对我来说不起作用。
示例代码:
// Here can be any table with any number of columns.
var table = new DataTable();
table.Columns.Add("StrCol", typeof(string));
table.Columns.Add("IntCol", typeof(int));
table.Rows.Add(new object[] { "Row1String", 1 });
table.Rows.Add(new object[] { "Row2String", 2 });
var data = from dataRow in table.AsEnumerable()
select dataRow;
var st = new StringTemplate("$items:{$it.StrCol$ $it.IntCol$}$");
st.SetAttribute("items", data);
Console.Write(st.ToString());
结果:
Class DataRow has no such attribute: StrCol in template context [anonymous anonymous]
Class DataRow has no such attribute: IntCol in template context [anonymous anonymous]
Class DataRow has no such attribute: StrCol in template context [anonymous anonymous]
Class DataRow has no such attribute: IntCol in template context [anonymous anonymous]
更新:
我必须使用StringTemplate
的版本3.1.0,需要使用Antlr3.StringTemplate.dll。我决定尝试另一个版本并下载了Antlr3.StringTemplate.dll版本3.3.0。一切正常运行。那么,有没有办法在旧的库中应用DataTable
上的模板?
DataTable
。 - kyrylomyr