如何将值从多个ListView传递到Datagridview

时间:2013-07-07 07:48:09

标签: c# c#-4.0 listview datagridview

我有2 listviewsdatagridview

一个列表视图有3列ProductCode,ProductName,ProductPrice,第二个列表视图有两列Godown Name,Godown Code

当我点击datagridview的第一列product时,我想要listview visible,然后我希望将listview的所选值传递给{{1}在同一序列中。

当我点击datagridview中的datagridview列时,godown应该可见,然后将选定的godown listview值传递到godown listview中的godown列}。我怎么能这样做?

enter image description here

1 个答案:

答案 0 :(得分:0)

以下是一些可能有用的代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        for (int i = 1; i < listView1.Items.Count; i++)
        {
            dataGridView1.Rows.Add();
        }

    }

    private void dataGridView1_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        //Decide which column corresponds to the one clicked
        switch (e.ColumnIndex)
        {
            case 0:
                FillColumn(listView2, 0, e.ColumnIndex);
                break;
            case 1:
                FillColumn(listView1, 1, e.ColumnIndex);
                break;
            case 2:
                FillColumn(listView2, 1, e.ColumnIndex);
                break;
            case 3:
                FillColumn(listView1, 0, e.ColumnIndex);
                break;
            case 4:
                FillColumn(listView1, 2, e.ColumnIndex);
                break;
        }
    }
    //Take the corresponding listview, the listview column, and the dgv column
    //and populate the dgv with the listview data
    private void FillColumn(ListView lv, int lvcolumn, int dgvcolumn)
    {
        lv.Visible = true;            
        foreach (ListViewItem lvi in lv.Items)
        {
            dataGridView1.Rows[lvi.Index].Cells[dgvcolumn].Value = lvi.SubItems[lvcolumn].Text;
        }
    }
}