hi there
i have 2 forms that each one has a datagridview.
i want when i fill dgv1 in form1 and press a button the dgv1 data has been sent to the dgv2 in form2.But it doesn't work.

form1:

public string str;
private void button1_Click(object sender, EventArgs e)
        {
            str = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            Form2 f2 = new Form2();
            f2.Show();
        }

form2:

private void Form2_Load(object sender, EventArgs e)
        {
            Form1 f1 = new Form1();
            dataGridView1.Rows[0].Cells[2].Value = f1.str;
        }

To be honest I use a form entry point when calling other forms to ensure they are set up properly. I always call .ShowDialog() from within the forms own class and this also lets me have return types when using modal forms and I completely ignore the DialogResult of the win form :X

Here is code on the caller form trying to open up another form and pass information:

private void EditUser()
    {
      if (gridView1.GetActiveRow() != null)
      {
        int userId = (int)gridView1.GetActiveRow()["UserId"];

        using (frmUserEdit frm = new frmUserEdit())
        {
          userId = frm.Edit(userId);
          if (userId > 0)
            BuildSql(userId);
        }
      }
    }

I pass in a connector ID to edit and also store the int return value of frm.Edit() . If the return value is > 0 that means changes were saved and I need to refresh the grid.
One important note: I start all of my identity seeds at 1000. This is handy for a little trick.

Now on the callee form:

public sealed partial class frmUserEdit : DevExpress.XtraEditors.XtraForm
  {
    private int _result;
    private User _user;
    private EditType _editType;
    public enum EditType { Normal, New, View }
    public int Edit(int UserId)
    {
      return Edit(UserId, EditType.Normal);
    }
    public int Edit(int UserId, EditType editType)
    {
      if (UserId == 0)
      {
        _user.Active = true;
      }
      else
      {
        if (!_user.Fetch(UserId))
        {
          XtraMessageBox.Show("The selected user has been deleted from the system.", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
          return 1; //force a rebuild on the caller
        }
      }
      this.ShowDialog();

      return _result;
    }
    private void SaveDataAndClose()
    {
      _user.Save();
      _result = _user.UserId;
      this.Close()
    }

See on the callee I pass in the identifier to fetch the underlying row from the database I need. If the row isn't found that means someone deleted the row after the user populated the grid on the callee form but before they tried to edit. Since the return value is >0 and <1000 it forces a rebuild on the calling grid. I also set the row focus based on identifier but the # is below my seed base so it focuses to the top.

thx ddanabe.but my dgv has several rows and columns that i want to send all of these datas to anotherdgv in another form.i define accessor(get,set) for rowcount and columncount to use them.but it didin't work.

This sounds like a design issue. Please upload your project so we can review it. Click the "Go Advanced" button next to the quick reply, then select "Manage Attachments" and upload your zipped project archive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.