2017-01-31 2 views
0

여기에 많은 질문이 있으며 여기에는 수많은 정보가 있습니다. 웬일인지, 이것을 작동시킬 수는 없습니다. 여기 내 출발점 중 하나입니다 ... Add entire row to DataTable at once using listWPF Datagrid C#

이것은 목록 목록입니다. 첫 번째 목록은 열 머리글이어야합니다.

dat처럼 보이는 List<List<string>>입니다 :

{"index", "filename0", "filename1"}, 
{"A-100", "yes", "no"}, 
{"A-200", "no", "yes"} 
etc... 

코드 :

|  |  |  | 
| index | file1 | file2 | 
------------------------- 
| A-100 | yes | no | 
------------------------- 
| A-200 | no | yes | 
------------------------- 
| A-300 | yes | yes | 

대신 내가 얻을 :

/// 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 |  |  | 
------------------------- 
| A-200 |  |  | 
------------------------- 
| A-300 |  |  | 

List of Lists는 필자가 기대하는 것, 명확하게 열의 정의를위한 작업입니다. 왜 첫 번째 열 뒤에 DataGrid에 아무 것도 들어 가지 않을지 모르겠다.

다음은 dat의 출력입니다. 그것은 내가 생각하는 Im입니다. 모든 행과 열이 차지했습니다. 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

+0

목록의 목록을 올바르게 초기화 했습니까? 방금 코드를 테스트하고 나에게 맞춰 졌기 때문입니다. –

+0

감사합니다. 나는 잘 모르겠다. 나는 일주일 전에 C#을 시작했지만 아직도 그걸 알아 듣고있다. 전체 수업을 추가했습니다. –

+0

문제는 목록 초기화 목록에서 비롯된 것 같습니다. 나는 AllDrawingLists에 무엇이 포함되어 있는지 모르겠습니다. 그것을 확인해보십시오. 'foreach (string strDat in listDat) { Console.Write (strDat + "");와 같이 목록의 모든 항목을'Console.Write()'로 시도 할 수 있습니다. } Console.WriteLine(); }' –

답변

0

헤더 이름에 점을 유지하려는 경우 대괄호로 묶인 머리글의 이름으로 머리글 열 바인딩 경로를 설정하여 특수 문자를 만들 수 있습니다 (이 경우 점 표기법) 도망 쳤다.

DataGridAutoGeneratingColumn 이벤트를 구독하는 이벤트 처리기에서 처리 할 수 ​​있습니다.

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; 
    } 
}