Click here to Skip to main content
15,916,463 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

If I am reading you correctly, you have a form (lets call it "fromDisplayUsers") and when you press the "New User" button, you bring up another form to get the user data ("formCreateUser"). When the user presses the "Ok" button on "formCreateUser", it closes the form and you need the data to be available in "formDisplayUsers".

This is a normal thing to do, and the best way to handle it is to set up public properties in formCreateUser which access the information entered by the user. So, if the username is enetered into a TextBox called tbUserName:
public string Username
   {
   get { return tbUserName.Text; }
   set { tbUserName.Text = value; }
   }

So, formDisplayUsers:
...
formCreateUser fcu = new formCreateUser();
fcu.UserName = "Type your name here";
fcu.ShowDialog();
string myUserName = fcu.UserName;
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.
 
Share this answer
 
Comments
chaitravb 15-Jan-11 4:02am    
the entered data sud be displayed in the datagrid which ll be present in fromDisplayUsers. can u pls give the solution for this.
you need declare one arraylist and NewUser form in Second form
C#
 private ArrayList arruser = new ArrayList();
private NewUser nuser;

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

C#
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

C#
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();
       }
   }



Now you can test! :)

Best Regard,

Theingi Win
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900