据从一种形式的文本框传递到Windows表单中另一种形式的数据网

据从一种形式的文本框传递到Windows表单中另一种形式的数据网

本文介绍了如何使用C#.net将数据从一种形式的文本框传递到Windows表单中另一种形式的数据网格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我有一个更新datagrid的问题.我的申请表中可能有表格.第一种形式是登录形式.在第二种形式中,我有一个datagrid控件以及一个添加新用户的按钮.在第二个表单中单击新用户按钮后,将显示一个新表单.这个新的用户表单将包含用于输入用户名和密码以及确定"按钮的测试框.单击确定按钮后,输入的数据应显示在数据网格中.我没有使用任何数据库.我刚用过Windows窗体.

请帮助我解决这个问题.



问候,

Chaitra

Hi all,

I have a prob updating the datagrid. i have may forms in my application. first form is login form. in second form i have a datagrid control along with a button add new user. once the new user button is clicked in the second form, a new form will be displayed. this new user form will have test boxes to enter the username and the password along with ok button. once the ok button is clicked, the data entered should be displayed in the datagrid. i have not used any database. i have just used windows forms.

pls help me to solve this prob.



with regards,

Chaitra

推荐答案

public string Username
   {
   get { return tbUserName.Text; }
   set { tbUserName.Text = value; }
   }


因此,formDisplayUsers:


So, formDisplayUsers:

...
formCreateUser fcu = new formCreateUser();
fcu.UserName = "Type your name here";
fcu.ShowDialog();
string myUserName = fcu.UserName;

针对所需的所有用户值重复此操作,然后可以将它们照常插入到数据网格中.

建议使用public properties,因为这意味着formDisplayUsers不需要了解fromCreateUser的工作原理:只要它继续返回用户名字符串,就可以随意更改它.

Repeat this for all the user values you need, and you can then insert them into the datagrid as normal.

Using public properties is recommended, because it means that formDisplayUsers does not need to know anthing about how fromCreateUser works: You can change it as you like as long as it continues to return a string for the user name.


 private ArrayList arruser = new ArrayList();
private NewUser nuser;


您以第二种形式在新用户按钮单击事件中编写以下代码


You write the following code in new user button click event in the second form

private void btnNew_Click(object sender, EventArgs e)
        {
            arruser = (ArrayList)dataGridView1.DataSource;
            nuser = new NewUser(arruser);
            nuser.ShowDialog();
            foreach (string user in nuser.User)
            {
                dataGridView1.Rows.Add(1);
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = user;
            }
        }





在新用户形式下,您必须使用以下代码





In new user form, you must be use following code

public partial class NewUser : Form
   {
       private ArrayList user = new ArrayList();
       public ArrayList User
       {
           get { return user; }
           set { user = value; }
       }
       public NewUser()
       {
           InitializeComponent();
       }
       public NewUser(ArrayList old)
       {
           InitializeComponent();
           user = old;
       }
       private void btnAdd_Click(object sender, EventArgs e)
       {
           if (user == null)
               user = new ArrayList();
           this.user.Add(txtUser.Text.Trim());
           this.Close();
       }
   }




现在可以测试了! :)

最好的问候,

Theingi Win




Now you can test! :)

Best Regard,

Theingi Win


这篇关于如何使用C#.net将数据从一种形式的文本框传递到Windows表单中另一种形式的数据网格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:08