6

I created a 2 dimensional array of strings and populated it. I try to bind it to a DataGrid control like so:

string[][] Array = new string[100][];
dataGridView.DataSource = Array;

Instead of seeing the contents of the array I see the following columns: Length, LongLenth, Rank, SyncRoot, IsReadOnly, IsFixedSize, IsSyncrhonized.

So instead of displaying the contents of my array, it displays the properties of the array. What did I do wrong?

7
  • how do you expect to see that information into the GV? Mar 1, 2013 at 20:18
  • What does your XAML look like for your DataGrid control?
    – ryrich
    Mar 1, 2013 at 20:23
  • For [x][y] I was hoping each row would represent the [X]...and columns would represent the [Y], so that the contents of [2][5] would be displayed on the Datagrid in the 2nd row, 5 column.
    – MrPatterns
    Mar 1, 2013 at 20:23
  • @ryrich What is XAML?
    – MrPatterns
    Mar 1, 2013 at 20:24
  • For starters, I wouldn't call your array "Array." Besides that, run this through the debugger and after you've defined your Array, look at it in a Watch window. See if you can identify the property that will give you what you're looking for.
    – Melanie
    Mar 1, 2013 at 20:31

4 Answers 4

4

When you allow the grid control to auto-generate columns, it will basically enumerate through the properties of that object and create a column for each one. It has no way to know that you want to display this as a grid of array values.

You'll need to create a new object (such as an enumerable list of a class) out of the array with the properties you want to bind to as columns. A quick way to do this would be to use an anonymous type, built using a LINQ query. Something like:

string[][] Array = new string[100][];
for(int i = 0; i < 100; i++) // Set some values to test
   Array[i] = new string[2] { "Value 1", "Value 2" };

dataGridView.DataSource = (from arr in Array select new { Col1 = arr[0], Col2 = arr[1] });
Page.DataBind();

Here, we're iterating through all 100 elements of the array. Each element is an array of 2 strings. We're creating an anonymous type out of those two strings. This type has two properties: Col1 and Col2. Col1 will be set to array index 0, and Col2 will be set to array index 1. Then, we're building the grid to that enumeration of anonymous types. This will look something like:

enter image description here

You can of course define exactly how columns will be created by setting AutoGenerateColumns to False, and populated the Columns collection. This can be done declaratively as well within your ASPX file.

2

You need to convert your array to a datatable

string[][] Array = new string[100][];
DataTable dt= new DataTable();

int l= Array.length;

for(int i=0;i<l;i++) {
     dt.LoadDataRow(Array[i], true); //Pass array object to LoadDataRow method
}

dataGridView.DataSource = dt;
0

You could do something like this

string[][] Array = new string[100][];

ArrayList arrList = new ArrayList();
for(int i=0;i<100;i++)
{
    arrList.Add(new ListItem(Array[i, 0], Array[i, 1]));
}
Grid2D.DataSource = arrList;
Grid2D.DataBind(); 

See this link Binding Arrays to GridView in ASP.Net

0
using Linq;

var Found = (from arr in myArray2D select 
new { row1 = arr[0], row2 = arr[1], row3 = arr[2] })
.Where(y => (y.row1.ToUpper() + y.row2.ToUpper())
.Contains(sText.ToUpper()))
.OrderByDescending(y => Convert.ToInt32(y.row3)).ToList();
dataGridViewFind.DataSource = Found;
dataGridViewFind.AutoResizeColumns();
1
  • These commands search for a text in columns 1 and 2 of the array and return by bringing columns 1, 2, and 3 of the array and sorting them down by column 3 converted from string to number. Mar 22, 2017 at 0:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.