简体   繁体   中英

How to bind two arrays to datagrid in WPF C#

I am new in programming. I have a question about data biding in WPF.

I have two double arrays and would like to bind them to datagrid with two columns like:

-double[] acce goes to a column in datagridview with a header of "Acceleration"

-double[] peri goes to a column in datagridview with a header of "Period"

I googled this issue but could not find out the right one. An example showing how will be greatly appreciated. Thanks,

You can't do that; you have to bind to one object. You could create aa double[,] array (ie a 2D array) and copy acce into one column ([0, x]) and peri into the other ([1, x]) and bind to this new array:

double[,] combined = new double[2, acce.Length];
for (int i = 0; i < acce.Length; i++)
{
    combined[0, i] = acce[i];
    combined[1, i] = peri[i];
}

..and then bind to combined. Or you get more ambitious (and possibly learn some more) and create a class as @FelixCastor suggested.

PS the code above assumes that peri and acce have the same length.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM