C# 如何使用用户生成的整数数组填充 dataGridView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18820151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:18:07  来源:igfitidea点击:

How to populate a dataGridView with an array of user generated integers

c#arraysdatagridview

提问by Foemilod

With this:

有了这个:

dataGridView.DataSource = theData.Select((x, index) =>
    new { CreatureRoll = x, CreatureLabel = index}).OrderByDescending(x =>    x.CreatureRoll).ToList();

It produces the data correctly, But it produces all the rows in the array with extra data that will not be useful, empty data.

它正确地生成数据,但它生成数组中的所有行,其中包含额外的数据,这些数据不会有用,空数据。

img http://s21.postimg.org/t3f34aa43/list_Result.jpg?noCache=1379353500

img http://s21.postimg.org/t3f34aa43/list_Result.jpg?noCache=1379353500

Would like to remove unnecessary rows of data

想要删除不必要的数据行

采纳答案by Damith

You can use LINQ to generate the datasource from a array.

您可以使用 LINQ 从数组生成数据源。

int[] theData = new int[] { 14, 17, 5, 11, 2 };
dataGridView1.DataSource = theData.Where(x => x>0).Select((x, index) =>
    new { RecNo = index + 1, ColumnName = x }).OrderByDescending(x => x.ColumnName).ToList();

enter image description here

在此处输入图片说明

回答by Saeid Alizade

Try this:

尝试这个:

int[] ints = new int[] { 1, 2, 3, 4, 5 };
gvMain.DataSource = ints;
gvMain.DataBind();

gvMainis a GridView, You can Add user generated array instead of ints.

gvMain是 GridView,您可以添加用户生成的数组而不是ints.

回答by Jageen

You can have code like this also,

你也可以有这样的代码,

dataGridView1.Columns.Add("Index", "Index");
dataGridView1.Columns.Add("Value", "Dice Value");
int[] theData = new int[] { 5, 2, 1, 5, 4, 1, 3, 1};

for (int i = 0; i < theData.Length; i++)
{
     dataGridView1.Rows.Add(new object[] { i+1, theData[i] });
}

Output
enter image description here

输出
在此处输入图片说明

回答by carbo18

Take a look at this .NET / C# Binding IList<string> to a DataGridView

看看这个.NET / C# Binding IList<string> to a DataGridView

Setting the datasource to an array will not display anything. Basically the values need to have a named property and need to implement IList to be able to be used as a DataSource. Something like this should do your bidding.

将数据源设置为数组将不会显示任何内容。基本上,这些值需要有一个命名属性,并且需要实现 IList 才能用作数据源。像这样的事情应该让你出价。

var array = new int[] { 3, 4, 4, 5, 6, 7, 8 };
dataGridView1.AutoGenerateColumns = true;
dataGridView1.DataSource = array.Select(x => new { IntValue = x }).ToList(); 

回答by Hemant

how to assign the collection for below situation:

如何为以下情况分配集合:

foreach (DataRow dr in dropDT.Rows)
{
     values.Add(dr[0].ToString());
}

dataGridView1.Rows[e.RowIndex].Cells[4].Value = values;
dataGridView1.Rows[e.RowIndex].Cells[4].Value = values;