将数据从另一个表单获取到dataGridView中

时间:2016-01-15 22:44:57

标签: c# forms datagridview

我正在开发一个帐户生成器,我需要帮助使我的帐户以另一种形式显示在dataGridView中。我的代码现在就是这样。我不知道如何将我的帐户放入dataGridView

这是我的表格:

private void button4_Click(object sender, EventArgs e)
    {
        string email = richTextBox2.Text;
        string password = richTextBox1.Text;
        for (var currentAccount = 0; currentAccount < (Int32.TryParse(out)).richTextBox4.Text; currentAccount++)
        {
        }

        List<string> Firstnames = new List<string>();
        Firstnames.Add("James");
        Firstnames.Add("Jakob");
        Firstnames.Add("Jacob");
        Firstnames.Add("Jonathan");
        Firstnames.Add("Albert");
        Firstnames.Add("Calvin");
        Firstnames.Add("Kyle");
        Firstnames.Add("Christopher");
        Firstnames.Add("Jeremy");
        Firstnames.Add("Ari");
        Firstnames.Add("Maximus");
        Firstnames.Add("Jerry");
        Firstnames.Add("Eric");
        Firstnames.Add("Trey");
        Firstnames.Add("Brenden");
        Firstnames.Add("Sean");
        Firstnames.Add("Timmothy");
        Firstnames.Add("Harris");
        Firstnames.Add("Matthew");
        Firstnames.Add("Michael");
        Firstnames.Add("Ching");
        Firstnames.Add("Alexander");

        Random rand = new Random();
        int x = rand.Next(0, Firstnames.Count);

        List<string> Lastnames = new List<string>();
        Lastnames.Add("Hampton");
        Lastnames.Add("Green");
        Lastnames.Add("Doe");
        Lastnames.Add("Ching");
        Lastnames.Add("Chong");
        Lastnames.Add("Albertson");
        Lastnames.Add("Ringel");
        Lastnames.Add("Alerstein");
        Lastnames.Add("Zion");
        Lastnames.Add("Stein");
        Lastnames.Add("Stine");
        Lastnames.Add("Elzed");
        Lastnames.Add("Jackson");
        Lastnames.Add("Cooperberg");
        Lastnames.Add("Charman");
        Lastnames.Add("Calberg");
        Lastnames.Add("Dorfmanberg");

        int y = rand.Next(0, Lastnames.Count);

        List<string> passwords = new List<string>();
        passwords.Add("richTextBox1_TextChanged");

        List<string> screenName = new List<string>();
        screenName.Add("");

        List<string> emails = new List<string>();
        emails.Add("richTextBox2_TextChanged+1209943");
        emails.Add("richTextBox2_TextChanged+16329943");
        emails.Add("richTextBox2_TextChanged+56329943");
        emails.Add("richTextBox2_TextChanged+9943");
        emails.Add("richTextBox2_TextChanged+123456329943");
        emails.Add("richTextBox2_TextChanged+156329943");
        emails.Add("richTextBox2_TextChanged+1239943");
        emails.Add("richTextBox2_TextChanged+123456329943");
        emails.Add("richTextBox2_TextChanged+1234563292943");
        emails.Add("richTextBox2_TextChanged+12345603");
        emails.Add("richTextBox2_TextChanged+1234563299");
        emails.Add("richTextBox2_TextChanged+13001");
        emails.Add("richTextBox2_TextChanged+12990-3");
        emails.Add("richTextBox2_TextChanged+99103z");
        emails.Add("richTextBox2_TextChanged+1x3");
        emails.Add("richTextBox2_TextChanged+1623bdsjaw");
        emails.Add("richTextBox2_TextChanged+99jsd943");
        emails.Add("richTextBox2_TextChanged+12345938urfv3");
        emails.Add("richTextBox2_TextChanged+ejfj9943");
        emails.Add("richTextBox2_TextChanged+1234rmd43");
        emails.Add("richTextBox2_TextChanged+dmmd29943");
        emails.Add("richTextBox2_TextChanged+123456kiuj943");
        emails.Add("richTextBox2_TextChanged+12i8839291");
        emails.Add("richTextBox2_TextChanged+123bfbdba3");
        emails.Add("richTextBox2_TextChanged+123absna43");
        emails.Add("richTextBox2_TextChanged+1234ababsba9943");
        emails.Add("richTextBox2_TextChanged+1asa943");
        emails.Add("richTextBox2_TextChanged+989283");
        emails.Add("richTextBox2_TextChanged+1234jfn9943");
        emails.Add("richTextBox2_TextChanged+kd13jd23");
        emails.Add("richTextBox2_TextChanged+938478du");


        List<string> birthmonth = new List<string>();
        birthmonth.Add("1");
        birthmonth.Add("2");
        birthmonth.Add("3");
        birthmonth.Add("4");
        birthmonth.Add("5");
        birthmonth.Add("6");
        birthmonth.Add("7");
        birthmonth.Add("8");
        birthmonth.Add("9");
        birthmonth.Add("10");
        birthmonth.Add("11");
        birthmonth.Add("12");
        int z = rand.Next(0, birthmonth.Count);

        List<string> birthday = new List<string>();
        birthday.Add("13");
        birthday.Add("10");
        birthday.Add("9");
        birthday.Add("7");
        birthday.Add("30");
        birthday.Add("20");
        birthday.Add("21");
        birthday.Add("3");
        birthday.Add("2");
        birthday.Add("1");
        birthday.Add("19");
        birthday.Add("18");
        birthday.Add("15");
        birthday.Add("11");
        birthday.Add("22");
        birthday.Add("26");
        birthday.Add("27");
        birthday.Add("29");
        int a = rand.Next(0, birthday.Count);

        List<string> birthyear = new List<string>();
        birthyear.Add("1985");
        birthyear.Add("1986");
        birthyear.Add("1987");
        birthyear.Add("1988");
        birthyear.Add("1989");
        birthyear.Add("1990");
        birthyear.Add("1991");
        birthyear.Add("1992");
        birthyear.Add("1993");
        birthyear.Add("1994");
        birthyear.Add("1995");
        birthyear.Add("1984");
        birthyear.Add("1983");
        birthyear.Add("1982");
        birthyear.Add("1981");
        int b = rand.Next(0, birthyear.Count);

        List<string> postalcode = new List<string>();
        postalcode.Add("richTextBox5_TextChanged");

        List<string> gender = new List<string>();
        gender.Add("male");

        string first = Firstnames[x];
        String last = Lastnames[y];

        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-firstName").SetAttribute("value", "firstnames");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-lastName").SetAttribute("value", "lastnames");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-email").SetAttribute("value", "emails");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-password").SetAttribute("value", "passwords");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-passwordConfirm").SetAttribute("value", "passwords");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-screenName").SetAttribute("value", "screenName");
        comboBox1.FindStringExact("nike_US"); webBrowser1.Document.GetElementById("exp-register-dob-month").SetAttribute("value", "birthmonth");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-dob-day").SetAttribute("value", "birthday");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-dob-year").SetAttribute("value", "birthyear");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-postalCode").SetAttribute("value", "postalcode");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("exp-register-gender-male").SetAttribute("value", "gender");
        comboBox1.FindStringExact("nike_us"); webBrowser1.Document.GetElementById("sign_up").InvokeMember("click");
        for (int i = 1; i <= 5; i++)
        {
            Console.WriteLine(i);
        }

1 个答案:

答案 0 :(得分:0)

为子表单创建一个新的表单构造函数,并在子表单设置属性中为需要传递的内容创建。下面显示了一个带有一个参数的重载新构造函数,并根据需要重复此模式。

请注意还有其他方法,这只是其中之一。 第二个注释:我使用ListBox来保持简单,并意识到你有一个DataGridView。

主要表单

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        List<string> Firstnames = new List<string>();
        Firstnames.Add("James");
        Firstnames.Add("Jakob");
        Firstnames.Add("Jacob");
        Firstnames.Add("Jonathan");
        Firstnames.Add("Albert");
        Firstnames.Add("Calvin");
        Firstnames.Add("Kyle");
        Firstnames.Add("Christopher");
        Firstnames.Add("Jeremy");
        Firstnames.Add("Ari");
        Firstnames.Add("Maximus");
        Firstnames.Add("Jerry");
        Firstnames.Add("Eric");
        Firstnames.Add("Trey");
        Firstnames.Add("Brenden");
        Firstnames.Add("Sean");
        Firstnames.Add("Timmothy");
        Firstnames.Add("Harris");
        Firstnames.Add("Matthew");
        Firstnames.Add("Michael");
        Firstnames.Add("Ching");
        Firstnames.Add("Alexander");

        Form2 f = new Form2(Firstnames);

        try
        {
            f.ShowDialog();
        }
        finally
        {
            f.Dispose();
        }
    }

}
带有ListBox的

子表格,这是名字列表的属性。

public partial class Form2 : Form
{
    public List<string> FirstNames { get; set; }
    public Form2()
    {
        InitializeComponent();
    }
    public Form2(List<string> firstNames)
    {
        InitializeComponent();
        this.FirstNames = firstNames;
        this.Shown += Form2_Shown;
    }

    private void Form2_Shown(object sender, EventArgs e)
    {
        listBox1.DataSource = this.FirstNames;
    }
}