[英]WPF Datagrid C#
我知道这里有很多问题,其他地方也有很多信息。 由于某种原因,我无法使它正常工作。 这是我的起点之一... 使用列表一次将整行添加到DataTable
这是用于列表列表的。 第一个List应该是列标题。
dat
是一个List<List<string>>
,看起来像:
{"index", "filename0", "filename1"},
{"A-100", "yes", "no"},
{"A-200", "no", "yes"}
etc...
码:
/// Dictionary containing as Key => FileName
/// as Value => All drawing numbers found in FileName
Dictionary<string, List<string>> AllDrawingLists = new Dictionary<string, List<string>>();
private void processGrid()
{
List<string> index = new List<string>();
/// Build a comprehensive INDEX from the dictionary - A list
/// of all the drawing numbers found in all the FIlenames
foreach (KeyValuePair<string, List<string>> item in AllDrawingLists)
{
foreach (string dwg in item.Value)
{
if (index.Contains(dwg) == false)
{
index.Add(dwg); }
}
}
List<List<string>> dat = new List<List<string>>();
List<String> headers = new List<string>();
headers.Add("Index");
foreach (KeyValuePair<string, List<string>> item in AllDrawingLists)
{
headers.Add(item.Key);
}
dat.Add(headers);
foreach(string i in index)
{
List<string> row = new List<string>();
row.Add(i);
foreach(KeyValuePair<string, List<string>> item in AllDrawingLists)
{
string cell = "no";
if (item.Value.Contains(i))
{
cell = "yes";
}
row.Add(cell);
}
dat.Add(row);
}
dataGrid.Columns.Clear();
DataTable dt = new DataTable();
int ii = 0;
foreach (List<string> row in dat)
{
if (ii == 0)
{
foreach(string t in row)
{
dt.Columns.Add(t);
}
ii++;
} else
{
dt.Rows.Add(row.ToArray<string>());
}
}
dataGrid.ItemsSource = dt.AsDataView();
}
我的预期结果将是:
| | | |
| index | file1 | file2 |
-------------------------
| A-100 | yes | no |
-------------------------
| A-200 | no | yes |
-------------------------
| A-300 | yes | yes |
但是我得到了:
| | | |
| index | file1 | file2 |
-------------------------
| A-100 | | |
-------------------------
| A-200 | | |
-------------------------
| A-300 | | |
列表列表是我所期望的,显然它适用于列的定义。 我不确定为什么第一列之后什么都没有进入DataGrid
这是dat的输出。 这就是我认为我要寻找的。 所有行和列都占。 Index C:\\py\\narrver2\\lists.txt C:\\py\\narrver2\\list2.docx A-1001 yes yes A-1002 yes yes A-1003 yes yes A-1004 no yes A-1005 no yes A-1006 no yes A-1007 no yes
如果要在标题名称中保留点,可以设置标题列绑定路径,标题的名称用方括号括起来,以使特殊字符(在这种情况下为点符号)转义。
您可以在预订DataGrid
事件AutoGeneratingColumn
的事件处理程序中执行此操作。
private void dataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
if (e.PropertyName.Contains('.') && e.Column is DataGridBoundColumn)
{
DataGridBoundColumn dataGridBoundColumn = e.Column as DataGridBoundColumn;
dataGridBoundColumn.Binding = new Binding("[" + e.PropertyName + "]");
dataGridBoundColumn.SortMemberPath = e.PropertyName;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.