小编Jay*_*yte的帖子

如何遍历列表,以便最后一个迭代步骤返回到第一个对象?

我有一个组成飞机的点列表.我想在连续点之间创建边缘并将它们添加到另一个列表中.

这是我目前的代码:

// Get points forming the plate
ArrayList points = part.points;

// Number of points forming the plate
int pointCount = points.Count;

// Create edges
List<LineSegment> edges = new List<LineSegment>();
for (int i = 0; i < pointCount - 1; i++)
{
    // Get start and end points
    Point start = points[i];
    Point end = points[i+1];

    // Create edge
    LineSegment edge = new LineSegment(start, end);

    // Add edge to the list
    edges.Add(edge);
}
Run Code Online (Sandbox Code Playgroud)

它不太起作用,因为它不会在列表的最后一个点和第一个点之间创建最后一个边.纠正它的方法是什么?我可以使用if这样的if语句:

for (int i = 0; …
Run Code Online (Sandbox Code Playgroud)

c# list

1
推荐指数
1
解决办法
69
查看次数

使用 ComboBox 将 DataGridView 绑定到 DataTable 不起作用

我正在尝试创建一个绑定到 DataTable 的 DataGridView,其中一列是 ComboBox。代码运行,但在绑定后(不是在绑定数据时)出现以下错误:System.ArgumentException:DataGridViewComboBoxCell 值无效。

在 DataGridView 中,其中一列是 DataGridViewComboBoxColumn,它使用枚举(名为 StructureType)作为其源:

// ColumnStructure
// 
this.ColumnStructure.ValueType = typeof(structureType);
this.ColumnStructure.DataSource = Enum.GetValues(typeof(structureType));
this.ColumnStructure.HeaderText = "Structure";
this.ColumnStructure.Name = "ColumnStructure";
this.ColumnStructure.DataPropertyName = "Structure";
//
Run Code Online (Sandbox Code Playgroud)

当我在不使用 DataTable 的情况下填充 DataGridView 时,它工作得很好:

structureType? structure = GetStructure(part);
dgvObjectTypes.Rows.Add(name, type, structure, count);
Run Code Online (Sandbox Code Playgroud)

现在我想使用 DataTable 代替,但无法让它工作。数据表创建如下:

DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Type", typeof(string));
table.Columns.Add("Structure", typeof(DataGridViewComboBoxCell));
table.Columns.Add("Count", typeof(int));
Run Code Online (Sandbox Code Playgroud)

其他列工作得很好,但我无法让“结构”列工作。这是我尝试创建组合框的方法:

var cb = new DataGridViewComboBoxCell();
cb.ValueType = typeof(structureType);
cb.DataSource = Enum.GetValues(typeof(structureType));
cb.Value = (structureType)structure;
Run Code Online (Sandbox Code Playgroud)

之后,我只需为表创建行并将表设置为 DataGridView 的数据源:

table.Rows.Add(name, type, cb, …
Run Code Online (Sandbox Code Playgroud)

.net c# datatable datagridview datagridviewcombobox

1
推荐指数
1
解决办法
4235
查看次数

标签 统计

c# ×2

.net ×1

datagridview ×1

datagridviewcombobox ×1

datatable ×1

list ×1