【发布时间】:2018-04-09 08:02:06
【问题描述】:
我是 C# 新手,以前从未使用过 DataTable。
我想要一个具有特定名称的 DataGridView。
DataTable table = new DataTable();
List<string> bla = new List<string>();
XDocument config = XDocument.Load(configFile);
Dictionary<string, string> dict = config.Descendants("Columns").FirstOrDefault().Elements()
.GroupBy(x => (string)x.Attribute("XPath"), y => (string)y.Attribute("Name"))
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
//I dont know if I need this:
foreach (string key in dict.Keys)
{
table.Columns.Add(key, typeof(string));
}
foreach (XElement position in positions.Where(e => e.HasAttributes))
{
foreach (XAttribute attribute in position.Attributes().Where(a => dict.ContainsKey($"@{a.Name.LocalName}")))
{
string name = attribute.Name.LocalName;
string value = (string)attribute;
string xName = dict["@" + name];
bla.Add(xName);
}
列的名称应来自 xName。
我该怎么做?
我试过了:
foreach (var item in bla)
{
DataRow row = table.NewRow();
row.SetField<string>(item); //this didn't work.
//foreach (string key in dict.Keys)
//{
// row.SetField<string>(key, item[key]);
//}
}
只想将 xName 中的名称作为我的输出标题。
xName 示例:职位、状态、订单、编号... 作为我的标题。 而在这之下的价值观。
【问题讨论】: