1

私は、C# で記述された Windows フォーム アプリケーションに取り組んでいます。

List<>コントロールから作成する方法について誰かの提案を見つけましたがDataGridView、セル値を抽出する方法についてはもう少し助けが必要です。

与えられたコードは次のとおりです。と に 2 つの列がdataGridView1ある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);
}
4

3 に答える 3

6

この方法で試してみてください

クラスタイプのリストを作成する

List<ProjectMasterRec>() ProjList = new List<ProjectMasterRec>(); 

リストのタイプが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);     
}
于 2014-01-10T05:22:25.193 に答える
2

LINQ を使用できる場合は、次のようなことができます。

var projectList = (from row in dataGridView1.Rows.OfType<DataGridViewRow>()
                   select new ProjectMasterRec() 
                   { Name = row.Cells["Name"].Value.ToString(),
                     Address = row.Cells["Address"].Value.ToString() 
                   }).ToList();
于 2014-01-10T05:33:11.433 に答える