简体   繁体   English

如何在C#中将锯齿状的字符串数组绑定到DataGrid?

[英]How do I bind a jagged array of strings to a DataGrid in C#?

I have a jagged array of strings in C#. 我在C#中有锯齿状的字符串数组。

How do I bind it to a DataGrid such that I can see the contents of the array? 如何将其绑定到DataGrid,以便可以看到数组的内容?

Currently in the DataGrid, instead of the array's contents, I see a column that says "Length", "Long Length", "Rank", "SyncRoot", etc...basically, properties of the array and not the contents of the array. 目前在DataGrid中,我看到的是列的长度,而不是数组的内容,而不是数组的内容:基本上是数组的属性,而不是数组的内容。“列”,“长”,“排”,“ SyncRoot”等数组。

My code: 我的代码:

string[][] jagged = new string [100][];

//...jagged array is populated...

dataGridView1.DataSource = jagged;  

Here is an example that you can try following I didn't do this with String[] but you can get the Idea 这是一个示例,您可以尝试遵循我没有使用String []进行的操作,但是您可以得到这个主意

//
// 1. Create two dimensional array
//

const int  dim = 1000;

double[,]  array = new double[dim,dim];

Random ran = new Random();
for(int r = 0; r < dim; r++)
{
    for(int c = 0; c < dim; c++)
    {
        array[r,c] = (ran.Next(dim)); // fill it with random numbers.
    }
}

// 2. Create ArrayDataView class in which 
// constructor you pass the array 
// and assign it to DataSource property of DataGrid. 

 dataGrid1.DataSource = new ArrayDataView(array);

For String[][] here is an example 对于String [] [],这是一个示例

string[][] arr = new string[2][];

arr[0] = new String[] {"a","b"};
arr[1] = new String[] {"c","d"};

DataGrid1.DataSource = arr[0];
DataGrid1.DataBind();//The result is: a,b in datagrid

using LinQ look at this 用LinQ看这个

List<string> names = new List<string>(new string[]
{
    "John",
    "Frank",
    "Bob"
});

var bindableNames =
    from name in names
    select new {Names=name};

dataGridView1.DataSource = bindableNames.ToList();

USING LINQ for Multi Denensional Array 将LINQ用于多密度阵列

string[][] stringRepresentation = ds.Tables[0].Rows  
    .OfType<DataRow>()  
    .Select(r => ds.Tables[0].Columns  
        .OfType<DataColumn>()  
        .Select(c => r[c.ColumnName].ToString())  
        .ToArray())  
    .ToArray();

As given by the current accepted answer and as mentioned by Michael Perrenoud in the comments, you can use Mihail Stefanov's ArrayDataView classes to achieve this binding. 正如当前接受的答案所给出的,以及迈克尔·佩伦努德(Michael Perrenoud)在评论中所提到的那样,您可以使用Mihail Stefanov的ArrayDataView类来实现此绑定。 His original code, however, was originally conceived to work only with multidimensional arrays. 但是,他的原始代码最初被认为仅适用于多维数组。 I have since modified his code to work with jagged arrays as well and made it available through the Accord.NET Framework. 此后,我修改了他的代码,使其也可以使用锯齿状数组,并且可以通过Accord.NET Framework使用它。

Now, you do not need to use the whole framework for doing that, you can simply use the updated classes available here . 现在,您无需使用整个框架来执行此操作,只需使用此处提供更新的类即可 After incorporating those classes in your project, all you have to do is 将这些类合并到项目中之后,您要做的就是

dataGridView.DataSource = new ArrayDataView( yourArray );

I hope this clarification helps. 我希望这种澄清有帮助。

As I mentioned, I am the author of Accord.NET, but the original credit really goes to Stefanov. 如前所述,我是Accord.NET的作者,但最初的荣誉归Stefanov所有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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