0

如何将选定的行从 Datagridview 传输到列表视图?(对不起,伙计们,我以前用错误的方式问这个问题!!)

private void button1_Click(object sender, EventArgs e)  
{  
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)  
    {  
    lvt = new ListViewItem();  
    lvt.SubItems.Add(row.Cells[1].Value.ToString());  
    lvt.SubItems.Add(row.Cells[2].Value.ToString());  
    lvt.SubItems.Add(row.Cells[3].Value.ToString());  
    lvt.SubItems.Add(row.Cells[4].Value.ToString());  
    listView1.Items.Add(lvt);  
    }  
}
4

1 回答 1

3

不是name,应该是SubItems

    ListViewItem lvItem;
    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {

        lvItem = new ListViewItem();
        lvItem.SubItems.Add(row.Cells["ID"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Item"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Cost"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Manufacturer"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Quanlity"].Value.ToString());
        // ...
    }

或者

    foreach (DataGridViewRow row in dataGridView1.SelectedRows)
    {

        lvItem = new ListViewItem(row.Cells["ID"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Item"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Cost"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Manufacturer"].Value.ToString());
        lvItem.SubItems.Add(row.Cells["Quanlity"].Value.ToString());
        // ...
    }
于 2012-08-01T12:36:15.963 回答