use*_*794 1 c# datagridview list custom-object winforms
我正在使用C#编写Windows窗体应用程序。
我发现有人对如何List<>
从DataGridView
控件创建对象的建议,但在如何提取单元格值方面我需要更多的帮助。
这是给出的代码;假设dataGridView1
are Name
和中的两列Address
。
如何建立List<ProjList>
物件?
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
ProjList = new List<ProjectMasterRec>();
foreach (DataGridViewCell dc in dr.Cells)
{
// build out MyItem
// based on DataGridViewCell.OwningColumn and DataGridViewCell.Value
// how do we code this?
}
ProjList.Add(item);
}
Run Code Online (Sandbox Code Playgroud)
这样尝试
创建您的班级类型列表
List<ProjectMasterRec>() ProjList = new List<ProjectMasterRec>();
Run Code Online (Sandbox Code Playgroud)
确保列表类型属于Datagridview中的数据类型
foreach (DataGridViewRow dr in dataGridView1.Rows)
{
//Create object of your list type pl
ProjectMasterRec pl = new ProjectMasterRec();
pl.Property1 = dr.Cells[1].Value;
pl.Property2 = dr.Cells[2].Value;
pl.Property3 = dr.Cells[3].Value;
//Add pl to your List
ProjList.Add(pl);
}
Run Code Online (Sandbox Code Playgroud)