将DataGridView.SelectedRows分配给List

时间:2015-10-23 01:25:47

标签: c# winforms

如何完成从foreach到多个列表的for分配?

我一直在尝试使用forList<string> dTitle = new List<string>(); List<string> dSku = new List<string>(); List<string> dPrice = new List<string>(); List<string> dDesc = new List<string>(); List<string> dImage = new List<string>(); foreach (DataGridViewRow r in dgvProducts.SelectedRows) { for (int i = 0; i < dgvProducts.Rows.Count - 1; i++) { dSku.Add(dgvProducts.Rows[i].Cells[1].Value.ToString()); dTitle.Add(dgvProducts.Rows[i].Cells[2].Value.ToString()); dPrice.Add(dgvProducts.Rows[i].Cells[3].Value.ToString()); dDesc.Add(dgvProducts.Rows[i].Cells[4].Value.ToString()); dImage.Add(dgvProducts.Rows[i].Cells[5].Value.ToString()); } } 循环语句执行此操作,但它没有产生预期的结果。它只显示第一行,无论选择哪一行。

如何仅对选定的行进行const Value& obj2 = document["Obj1"] const Value& obj3 = obj2["Obj2"] 循环语句搜索?或者有更好的方法吗?

import turtle
def draw_shape(t, level,size):
    if level == 1: #draws an I
        t.down()
        t.forward(size/2)
        t.left(90)
        t.forward(size/2)
        t.back(size)
        t.forward(size/2)
        t.left(90)
        t.forward(size)
        t.left(90)
        t.forward(size/2)
        t.back(size)
        t.up()
        t.forward(size/2)
        t.left(90)
        t.forward(size/2)

    else:
        draw_shape(t,level - 1,size)
        t.back(size/2)
        t.right(90)
        t.forward(size/2)
        t.left(90)
        draw_shape(t,level - 1,size/2)
        t.left(90)
        t.forward(size)
        t.right(90)
        draw_shape(t,level-1,size/2)
        t.right(90)
        t.forward(size/2)
        t.left(90)
        t.forward(size)
        t.right(90)
        t.forward(size/2)
        t.left(90)
        draw_shape(t,level-1,size/2)
        t.left(90)
        t.forward(size)
        t.right(90)
        draw_shape(t,level-1,size/2)

def q1():
    my_win = turtle.Screen()
    my_turtle = turtle.Turtle()
    my_turtle.speed(0.006)
    my_turtle.left(90)
    my_turtle.up()
    my_turtle.back(200)
    n = int(input('Draw shape at level: '))
    draw_shape(my_turtle,n,200)
    my_win.exitonclick()

q1()

dgvProduct - the DataGridView

1 个答案:

答案 0 :(得分:2)

我认为您不需要使用嵌套循环来执行此操作。

List<string> dTitle = new List<string>();
List<string> dSku = new List<string>();
List<string> dPrice = new List<string>();
List<string> dDesc = new List<string>();
List<string> dImage = new List<string>();

foreach (DataGridViewRow r in dgvProducts.SelectedRows)
{
    dSku.Add(r.Cells[1].Value.ToString());
    dTitle.Add(r.Cells[2].Value.ToString());
    dPrice.Add(r.Cells[3].Value.ToString());
    dDesc.Add(r.Cells[4].Value.ToString());
    dImage.Add(r.Cells[5].Value.ToString());
}