我现在做了两个窗体,form1和form2,在form1中用模式窗口打开form2,form1中有个datagridview1,在from2中点击上一条或者下一条按钮,可以操作form1里的datagridview1光标上下移动,我现在在form1中可以实现上下移动的功能,但是想从form2中调用form1中的方法总是不行,哪位高手帮我看看吧,第一次做winform的程序 不太懂form1:
 public void sy() //上一条记录
        {            if (dataGridView1.CurrentCell.RowIndex != 0)
            {
                if (dataGridView1.CurrentCell.RowIndex != dataGridView1.Rows.Count)
                { 
                    dataGridView1.CurrentCell = this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex - 1];
                }
            }
            else
            {
                dataGridView1.CurrentCell = this.dataGridView1[0, dataGridView1.CurrentCell.RowIndex];
            }
        }form2: private void button1_Click(object sender, EventArgs e)//按钮
        {
             form1 ys = new form1();
             ys.sy();
        }

解决方案 »

  1.   

    form1 ys = new form1();这是一个新窗口,调用当然没用
      

  2.   

    给构造方法加个参数
    public class form2
    {
    form1 frm1=null;
    public form2(form2 f1)
            {
    frm1=f1;
                InitializeComponent();
            }
    //......
     private void button1_Click(object sender, EventArgs e)//按钮
      {
      frm1.sy();
      }
    }
      

  3.   

    太乱 上面写错了
    public class form2
    {
        form1 frm1=null;
        public form2(form1 f1)
        {
            frm1=f1;
            InitializeComponent();
        }
    //......
        private void button1_Click(object sender, EventArgs e)//按钮
        {
             frm1.sy();
        }
    }
      

  4.   

    form2: private void button1_Click(object sender, EventArgs e)//按钮
      {
          ((Form1)this.ParentForm).sy();  }
      

  5.   

    public class form2
    {
      form1 frm1=null;
      public form2(form1 f1)
      {
      frm1=f1;
      InitializeComponent();
      }
    //......
      private void button1_Click(object sender, EventArgs e)//按钮
      {
      frm1.sy();
      }
    }
    打开form2是将form1的对象作为参数传递给form2