سؤال

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. Mind you, all the column headers are transferred.

Here is a snippet of 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:

        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 DataGridViewin Form2's 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.

Consider Row is having 3 values like ID,Name, Age

Create Object for OrderSearchForm with these Pramaeteter.

 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:

   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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top