我有一个自动生成的Datagrid,它显示来自API的查询数据。结果以数组形式出现,我把它们放在DataTable中,显示在Datatgrid中。数组长度并不总是相同,列不是“偶数”。
|--DTColumn1---DTColumn2-----|
|---Data1----|---Data1-------|
|---Data2----|---Data3-------|
|---Data3----|---Data5-------|
|---Data4----|---emptycell---|
|---Data5----|---emptycell---|
“DTColumn”是包含数据的数组,它们是相似的,但部分可能会丢失而且不是错误。我不知道我将在运行时获得的数据,但我知道它将包含一些类似的数据。
任何建议如下:
|--DTColumn1---DTColumn2-----|
|---Data1----|---Data1-------|
|---Data2----|---emptycell---|
|---Data3----|---Data3-------|
|---Data4----|---emptycell---|
|---Data5----|---Data5-------|
修改
代码结构:
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)
我不知道DTColumn1
和DTColumn2
中的数据类型,但我想有一些东西让你知道,例如,DTColumn1[0]
是相对于DTColumn2[3]
。
您可以创建两个新数组并将其用作数据数组的容器,同时将新数组填充为空字段。
示例:
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
是较长的数组,它将被放置在表的第一列中。
我写了一个原始示例只是为了给你一个想法,请注意这是未经测试的代码