Datagridview包含单元格值作为对象,但我需要属性?

时间:2017-03-11 18:01:03

标签: c# datagridview

k是类Card的实例,datagridview中的每一行都包含其所有3个属性。我想阅读每个属性,并在列表中插入该实例。

public List<Card> insertInList()
{
     foreach (DataGridViewRow row in dataGridView1.Rows)
     {
          for (int i = 0; i < dataGridView1.Columns.Count - 1; i++)
            {
                //I considered 'row.Cells[i].Value' - this returns object,               
                //and I need int, or string
                Card k = new Card();
                //k.Id =?;
                //k.Word =?;
                //k.Description =?;
                list1.Add(k);
                return list1;
          }
     }
}

1 个答案:

答案 0 :(得分:0)

这应该是解决方案:

public List<Karta> insertInList()
{
     foreach (DataGridViewRow row in dataGridView1.Rows)
     {

                Card k = new Card();
                k.Id =(int)row.Cells[0].Value;
                k.Word =(string) row.Cells[1].Value;
                k.Description = (string)row.Cells[2].Value;
                List.Add(k);


     }
 return List;
}