简体   繁体   English

将选定的DataGridView行以另一种形式传输到新的DataGridView

[英]Transfer selected DataGridView row to a new DataGridView in another form

I have exhaustively researched (googled) a solution to this problem, but can find none. 我已经对这个问题的解决方案进行了详尽的研究(搜索),但找不到任何解决方案。 My problem is that upon selecting a row in Form1 (Called that for clarity) it does not transfer the values to the DataGridView in Form2. 我的问题是,在Form1中选择了一行(为清楚起见,称为行)时,它不会将值传输到Form2中的DataGridView Mind you, all the column headers are transferred. 请注意,所有列标题均已传输。

Here is a snippet of Form1: 这是Form1的代码段:

        private void customersDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        OrderSearchForm orderform = new OrderSearchForm();

        orderform.Row = this.customersDataGridView.CurrentRow;

        orderform.MdiParent = this.MdiParent;

        orderform.Show();
    }

And Form2: 和Form2:

        private DataGridViewRow row;

    public DataGridViewRow Row
    {
        get { return row; }
        set { row = value; }
    }

    private void OrderSearchForm_Load(object sender, EventArgs e)
    {
        NorthwindDataClassesDataContext db = new NorthwindDataClassesDataContext();

        DataGridViewRow r = row.Clone() as DataGridViewRow;

        foreach (DataGridViewCell cell in row.Cells)
        {
            this.OrderSearchgridview.Columns.Add(cell.OwningColumn.Name,
                cell.OwningColumn.HeaderText);

            r.Cells[cell.ColumnIndex].Value = cell.Value;
        }

        OrderSearchgridview.Rows.Add(r);
    }

So basically, my question is, how do I show the row values from Form1's DataGridView in Form2's DataGridView ? 所以基本上,我的问题是,如何在Form2的DataGridView显示Form1的DataGridView中的行值?

I feel I am missing a step to add the actual values after inserting the columns, but I am stuck at this point. 我觉得我在插入列后错过了添加实际值的步骤,但是我被困在这一点上。

Better to have one more Contrucutor for OrderSearchForm to hold the Values for Row. 最好再为OrderSearchForm拥有一个构造器来保存Row的值。

Consider Row is having 3 values like ID,Name, Age 考虑Row具有3个值,例如ID,Name,Age

Create Object for OrderSearchForm with these Pramaeteter. 使用这些Pramaeteter为OrderSearchForm创建对象。

 OrderSearchForm orderform = new OrderSearchForm(New User(customersDataGridView.SelectedRows[0].Cells[0].Value.ToString(),SelectedRows[0].Cells[1].Value.ToString(),SelectedRows[0].Cells[2].Value.ToString()));

  orderform.MdiParent = this.MdiParent;

 orderform.Show();

And Form2: 和Form2:

   private DataGridViewRow row;
   private User userObj;

public OrderSearchForm(User user)
{
    this.userObj=user;

} 


    public DataGridViewRow Row
    {
        get { return row; }
        set { row = value; }
    }

    private void OrderSearchForm_Load(object sender, EventArgs e)
    {
       //Do Operation with User's Properties.as you Like

        OrderSearchgridview.Rows.Add(r);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM