从Form2向DataGridView(Form1)添加行 [英] Adding row to DataGridView (Form1) from Form2

查看:157
本文介绍了从Form2向DataGridView(Form1)添加行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多问题,很多答案...但我仍然在努力使这项工作。



OhMyVisitorsMW是我的Form1,FormAddWebsite是我的Form2。



这是Form1的代码:

 code> using System; 
使用System.Windows.Forms;

命名空间WindowsFormsApplication1
{
public partial class OhMyVisitorsMW:Form
{
private int nbroflinksadded = -1;
FormAddWebsite FormAddWebsite = new FormAddWebsite();

public OhMyVisitorsMW()
{
InitializeComponent();
}

private void OHMyVisitorsMW_Load(object sender,EventArgs e)
{
dataGridView1.Rows.Add(1);
}

private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
{
if(e.ColumnIndex == 3)
{
if(e.RowIndex == nbroflinksadded + 1)
{
FormAddWebsite.ShowDialog();
}
else
{
contextMenuStrip2.Show(MousePosition.X,MousePosition.Y);
}
}
}

public void AddSite(string nomsite,string urlsite)
{
dataGridView1.Rows.Add(nomsite, urlsite);
dataGridView1.Update();
dataGridView1.Refresh();
}
}
}

这里是我的Form2:

  using System; 
使用System.Windows.Forms;

命名空间WindowsFormsApplication1
{
public partial class FormAddWebsite:Form
{
public static string nomsite;
public static string urlsite;

public FormAddWebsite()
{
InitializeComponent();
}

private void button1_Click(object sender,EventArgs e)
{
OhMyVisitorsMW MainForm = new OhMyVisitorsMW();
nomsite = textBox1.Text;
urlsite = textBox2.Text;
MainForm.AddSite(nomsite,urlsite);

this.Close();
}

private void button2_Click(object sender,EventArgs e)
{
this.Close();
}
}
}

什么是错误?
感谢您的帮助。

解决方案

我更喜欢使用事件处理程序来做这样的事情。阅读以下样品。这不是真实的形式。我刚刚创建了示例类。

  public class ParentForm:Form 
{
Button openButton = new Button();

public ParentForm()
{
openButton.Click + = openButton_Click;
}

void openButton_Click(object sender,EventArgs e)
{
ChildForm childForm = new ChildForm();
childForm.OKButtonClick + = childForm_OKButtonClick;
childForm.ShowDialog();
}

void childForm_OKButtonClick(object sender,MyEventArgs e)
{
//使用事件参数中的属性,并以此形式设置数据
}

}

public class ChildForm:Form
{
按钮okButton = new Button();
文本框名称=新TextBox();
TextBox address = new TextBox();

public event EventHandler< MyEventArgs> OKButtonClick;

public ChildForm()
{
okButton.Click + = okButton_Click;


void okButton_Click(object sender,EventArgs e)
{
if(OKButtonClick!= null)
{
MyEventArgs myEventArgs =新的MyEventArgs();

myEventArgs.Name = name.Text;
myEventArgs.Address = address.Text;

OKButtonClick(sender,myEventArgs);
}
}
}

public class MyEventArgs:EventArgs
{
public string Name
{
get ;
设置;
}

public string Address
{
get;
设置;
}
}


I know there are a lot of questions about this, and a lot of answers... but I'm still trying to make this work.

OhMyVisitorsMW is my Form1, FormAddWebsite is my Form2.

Here is the code of my Form1 :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class OhMyVisitorsMW : Form
    {
        private int nbroflinksadded = -1;
        FormAddWebsite FormAddWebsite = new FormAddWebsite();

        public OhMyVisitorsMW()
        {
            InitializeComponent();
        }

        private void OHMyVisitorsMW_Load(object sender, EventArgs e)
        {
            dataGridView1.Rows.Add(1);
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                if (e.RowIndex == nbroflinksadded+1)
                {
                    FormAddWebsite.ShowDialog();
                }  
                else
                {
                    contextMenuStrip2.Show(MousePosition.X, MousePosition.Y); 
                }
            }
        }

        public void AddSite(string nomsite, string urlsite)
        {
            dataGridView1.Rows.Add(nomsite, urlsite);
            dataGridView1.Update();
            dataGridView1.Refresh();
        }
    }
}

And here is the code of my Form2 :

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FormAddWebsite : Form
    {
        public static string nomsite;
        public static string urlsite;

        public FormAddWebsite()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OhMyVisitorsMW MainForm = new OhMyVisitorsMW();
            nomsite = textBox1.Text;
            urlsite = textBox2.Text;
            MainForm.AddSite(nomsite, urlsite);

            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

What is wrong.. ? Thanks for your help.

解决方案

I prefer using event handlers to do something like this. Read following sample. This is not a real form. I just created sample classes to demonstrate.

public class ParentForm : Form
{
    Button openButton = new Button();

    public ParentForm()
    {
        openButton.Click += openButton_Click;
    }

    void openButton_Click(object sender, EventArgs e)
    {
        ChildForm childForm = new ChildForm();
        childForm.OKButtonClick += childForm_OKButtonClick;
        childForm.ShowDialog();
    }

    void childForm_OKButtonClick(object sender, MyEventArgs e)
    {
        // Use properties from event args and set data in this form
    }

}

public class ChildForm : Form
{
    Button okButton = new Button();
    TextBox name = new TextBox();
    TextBox address = new TextBox();

    public event EventHandler<MyEventArgs> OKButtonClick;

    public ChildForm()
    {
        okButton.Click += okButton_Click;
    }

    void okButton_Click(object sender, EventArgs e)
    {
        if (OKButtonClick != null)
        {
            MyEventArgs myEventArgs = new MyEventArgs();

            myEventArgs.Name = name.Text;
            myEventArgs.Address = address.Text;

            OKButtonClick(sender, myEventArgs);
        }
    }
}

public class MyEventArgs : EventArgs
{
    public string Name
    {
        get;
        set;
    }

    public string Address
    {
        get;
        set;
    }
}

这篇关于从Form2向DataGridView(Form1)添加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆