2017-01-31 5 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 |  |  | 

リストのリストは、私が期待していることです。明らかに、カラムの定義のために働いています。なぜ最初の列の後にも何も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

+0

リストのリストを正しく初期化しましたか?私はちょうどあなたのコードをテストし、それが私の上で働いたので。 –

+0

ありがとうございます。よく分かりません。私は1週間ほど前にC#を起動し、まだそれを取得しています。クラス全体を追加しました。 –

+0

私はリストの初期化のあなたのリストからの問題だと思う。私は 'AllDrawingLists'に何が含まれているのか分かりません。それが正しいかどうかを確認するためにチェックしてください。 'foreach(listDatの文字列strDat) { Console.Write(strDat +" ");のように、' Console.Write() 'でリスト内のすべての項目を試すことができます。 } Console.WriteLine(); } ' –

答えて

0

)エスケープ。

これは、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; 
    } 
}