0

I have a jagged array of strings in C#.

How do I bind it to a DataGrid such that I can see the contents of the array?

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.

My code:

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

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

dataGridView1.DataSource = jagged;  
MrPatterns
  • 4,184
  • 27
  • 65
  • 85
  • This answer is for a WPF Grid but conceptually should be about the same for what your trying to do. http://stackoverflow.com/a/8326875/261997 – RThomas Sep 28 '12 at 18:05

2 Answers2

1

Here is an example that you can try following I didn't do this with String[] but you can get the Idea

//
// 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[][] 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

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

string[][] stringRepresentation = ds.Tables[0].Rows  
    .OfType<DataRow>()  
    .Select(r => ds.Tables[0].Columns  
        .OfType<DataColumn>()  
        .Select(c => r[c.ColumnName].ToString())  
        .ToArray())  
    .ToArray();
MethodMan
  • 18,625
  • 6
  • 34
  • 52
  • This ArrayDataView class seems to be the key to what I'm looking for. But due to my poor understanding I have no idea how to implement what the comments are suggesting that I do. Do you have the code for this class you speak of? – MrPatterns Sep 28 '12 at 18:12
  • 1
    @phan: the `ArrayDataView` is an open source library you can find here, http://code.google.com/p/accord/source/browse/trunk/Sources/Accord.Controls.Statistics/ArrayDataView/ArrayDataView.cs?r=11&spec=svn11. – Mike Perrenoud Sep 28 '12 at 18:14
  • Are you familiar with LINQ..? if so I can paste a simple example using linq – MethodMan Sep 28 '12 at 18:16
  • @DJ KRAZE hi DJ, i'm looking at your LINQ example but that is only a simple, one-dimensional list. how would you implement your LINQ solution on the list-equivalent of a jagged array? (a list of lists?) – MrPatterns Oct 01 '12 at 14:03
  • Take a look at the example I just appended to my list of examples and change it to fit your UseCase.. – MethodMan Oct 01 '12 at 14:07
0

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. 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.

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.

Cesar
  • 2,059
  • 25
  • 30