2016-08-30 12 views
1

私はAPIからクエリされたデータを表示する自動生成されたDatagridを持っています。結果は配列形式で取り込まれ、DataTableに入れられます。これらはDataTreeに表示されます。配列の長さは必ずしも同じではなく、列は「均等」ではありません。 |--DTColumn1---DTColumn2-----| |---Data1----|---Data1-------| |---Data2----|---Data3-------| |---Data3----|---Data5-------| |---Data4----|---emptycell---| |---Data5----|---emptycell---| WPFデータグリッドが不均一なデータでいっぱいになる

"DTColumn"は類似のデータを含む配列ですが、欠損している可能性がありエラーではありません。実行時に取得するデータはわかりませんが、同様のデータが含まれていることがわかります。

どれでも、それはこのように見えるようにするアドバイス: |--DTColumn1---DTColumn2-----| |---Data1----|---Data1-------| |---Data2----|---emptycell---| |---Data3----|---Data3-------| |---Data4----|---emptycell---| |---Data5----|---Data5-------|

EDIT:

コード構造:

private void buttonget_Click(object sender, RoutedEventArgs e) 
    { 
     sngrid.ItemsSource = null; 
     string dtnumber = DTBox.Text; 
     if (dtnumber == "") 
     { 
      MessageBox.Show("NO DATA!"); 
     } 
     else 
     { 
      string[] dtresvalues;// this is were the DTColumn's come from and the number a of loop's for the second apicall 
      int s = apicall.Check..; //<- check if input can be handled and get dtresvalues 
      if (s != 5) 
      { 
       //API error handling here 
      } 
      else 
      { 
      var list = dtresvalues.Where((value, index) => index % 2 == 0).ToArray();// get rid of excess/usles data 
      string[] predt = list.ToArray();// prepared data, actual DTColumn's in array 
      DataTable table = new DataTable(); 
      for (int i = 0; i < predt.Length; i++)//loop untill DTColumn run out 
      { 
      string kerdt = predt[i];// set current DTColumn for API 
      int si = apicall.Get...;// if no error, it returns the resoultvalues array (data1, data2,...) 
        if (si != 0) 
        { 
         //API error handling here 
        } 
        else 
        { 
        table.Columns.Add(kerdt);//set column name in table 
        if (i == 0)//this loop down, add's resoultvalues of API to current column of table(data1,data2, ..) 
         { 
          foreach (string sd in resoultvalues) 
          { 
           table.Rows.Add(sd); 
          } 
         } 
         else 
         { 
          while (table.Rows.Count < resoultvalues.Length) 
          { 
           table.Rows.Add(); 
          } 
          for (int j = 0; j < resoultvalues.Length; j++) 
          { 
           table.Rows[j].SetField(i, resoultvalues[j]); 
          } 

         } 
        } 

       } 
       // add finished table to datagrid 
       sngrid.ItemsSource = table.DefaultView; 

}

はまたresoultvalues配列です。

+0

偶数とはどういう意味ですか?文字列が中央に配置されているか、右の列の値が交互に並べられているセルの幅ですか? –

+0

でも私はスタイルの書式設定を意味するものではありません。私は、データ1がある場合は、次の列data1は、2番目の列にemptycellのdata2とdata2がない場合は隣にする必要がありますが、3番目の列に再びそれがある場合は、その行にする必要がありますデータを持たない可能性1。 – Tory

+0

もしそれがあまりにも多くの時間を要しなければ、配列からソースを取り出した後に配列を並べ替えるのはなぜですか?または、あなたは 'KeyValuePair のようなKeyValuesPairを使って新しいllist/arrayに配置することができます –

答えて

0

私はDTColumn1DTColumn2内にあるデータの種類を知りませんが、私はあなたが、たとえば、DTColumn1[0]DTColumn2[3]に相対的である、ということを知らせるために何かがあることを想像してみてください。

2つの新しい配列を作成し、データ配列のコンテナとして使用し、新しい配列に空のフィールドを埋め込むことができます。

例:

string[] DTColumn1 = { 
    "1-foo", 
    "2-bar", 
    "3-foobar" 
}; 
string[] DTColumn2 = { 
    "1-foo2", 
    "3-foobar2" 
}; 


//Find the longest array of the two 
string[] longestArray = DTColumn1; 
string[] shortestArray = DTColumn2; 

if (DTColumn2.Length > longestArray.Length) { 
    longestArray = DTColumn2; 
    shortestArray = DTColumn1; 
} 


//Instantiating new lists to show data 
List<string> col1 = new List<string>(); 
List<string> col2 = new List<string>(); 


//Filling "interface" lists with data 
foreach (void value_loopVariable in longestArray) { 
    value = value_loopVariable; 
    col1.Add(value); 
} 

//This can be tricky, but I really have no idea of how your data is structured 
foreach (void value1_loopVariable in col1) { 
    value1 = value1_loopVariable; 

    foreach (void value2_loopVariable in shortestArray) { 
     value2 = value2_loopVariable; 
     if (value1[0].Equals(value2[0])) { 
      col2.Add(value2); 
      break; 
     } 

     //When the program reaches this point means that there is no corrispondace of data, so we add an empty value to col2 
     col2.Add(""); 

    } 

} 
//Here you'll have col1 and col2 filled with data 

これは、このことになる。

|--DTColumn1---|--DTColumn2-----| 
|---1-foo------|---1-foo2-------| 
|---2-bar------|---emptycell----| 
|---3-foobar---|---3-foobar2----| 

あなたはDTColumn2長い配列である場合、それはテーブルの最初の列に配置されることに気づくことができます。

未解決のサンプルを書き留めましたが、これはテストされていないコードであることに注意してください。

+0

質問がC#でタグ付けされているときにVBコードを投稿する理由は何ですか? –

+0

私のせいで、私は不注意だった。答えを編集しました – theBugger

+0

構造を編集して追加しました。 DTcolumnが1から50の場合のこの問題。 – Tory

関連する問題