如何使用DataGridView显示此数组的内容?

时间:2022-06-24 07:37:33

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

我创建了一个二维字符串数组并填充它。我尝试将它绑定到DataGrid控件,如下所示:

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.

我没有看到数组的内容,而是看到以下列: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?

因此,它不显示我的数组的内容,而是显示数组的属性。我做错了什么?

4 个解决方案

#1


3  

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:

您需要使用要作为列绑定的属性从数组中创建一个新对象(例如类的可枚举列表)。一种快速的方法是使用匿名类型,使用LINQ查询构建。就像是:

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:

在这里,我们迭代遍历数组的所有100个元素。每个元素是一个包含2个字符串的数组。我们正在用这两个字符串创建一个匿名类型。此类型有两个属性:Col1和Col2。 Col1将设置为数组索引0,Col2将设置为数组索引1.然后,我们将网格构建为匿名类型的枚举。这看起来像是这样的:

如何使用DataGridView显示此数组的内容?

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.

您当然可以通过将AutoGenerateColumns设置为False来确定如何创建列,并填充Columns集合。这可以在ASPX文件中以声明方式完成。

#2


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;

#3


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

请参阅此链接将绑定数组绑定到ASP.Net中的GridView

#4


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


3  

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:

您需要使用要作为列绑定的属性从数组中创建一个新对象(例如类的可枚举列表)。一种快速的方法是使用匿名类型,使用LINQ查询构建。就像是:

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:

在这里,我们迭代遍历数组的所有100个元素。每个元素是一个包含2个字符串的数组。我们正在用这两个字符串创建一个匿名类型。此类型有两个属性:Col1和Col2。 Col1将设置为数组索引0,Col2将设置为数组索引1.然后,我们将网格构建为匿名类型的枚举。这看起来像是这样的:

如何使用DataGridView显示此数组的内容?

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.

您当然可以通过将AutoGenerateColumns设置为False来确定如何创建列,并填充Columns集合。这可以在ASPX文件中以声明方式完成。

#2


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;

#3


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

请参阅此链接将绑定数组绑定到ASP.Net中的GridView

#4


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();