简体   繁体   中英

how to automatically refresh data from database with datagridview in c#

I have a program that has two forms, Form A is accessible by an admin and Form B is for random users.

Form A is used for searching for the list of registered users. Form B is used for registering.

Form A has datagridview that has data from database, I want the datagridview to refresh the data inside it automatically, after someone registered from Form B...

I want to get the data from the database and put it into datagridview automatically without closing the form and opening it again...

sorry i'm still new at this.. Can you give me some advice and examples please... Thank you...

Without any code is difficult to answer your problem. But usually this kind of problem could be resolved implementing a simple form of event handling.

In your case FormB publishes the information that a new user has been registered. When a new user register itself the FormB checks if anyone has registered itself to be notified of the event. The FormA registers itself to receive the notification of the fact.

In code you have this in FormB

public class FormB : Form
{
     public delegate void OnNewUserRegistered(string username)
     public OnNewUserRegistered NewUserRegistered;

     ....

     protected void cmdRegister_Click(object sender, EventArgs e)
     {
         // Code to register the user
         string username = txtUserName.Text;
         ..... etc .....

         // If someone has registered a subscription then call that subscription handler
         if(NewUserRegistered != null)
              NewUserRegistered(username); 

     }
}

Now the biggest problem, according to your explanation, is the mode in which the FormA subscribes the event in FormB. Suppose that, when the FormA loads, the FormB is already opened. You need to search for the instance of FormB

FormA.cs code

Public FormB RegistrationForm {get; set;}

private void Form_Load(object sender, EventArgs e)
{
   // If FormB is open then get its instance
   this.RegistrationForm = Application.OpenForms.Cast<Form>().OfType<FormB>().FirstOrDefault ();
   if(this.RegistrationForm != null)
   {
       // Subscribe to the registration event in FormB
       RegistrationForm.NewUserRegistered += ANewUserHasBeenRegistered;

       // Subscribe to the closed event of FormB
       RegistrationForm.Closed += FormBHasBeenClosed;
   }

   // Now load the current user list in your grid view.
   LoadUserList(); // this contains the code that initializes the gridView
}    

// this is the subscription method that will be called by the FormB instance
// every time a new user registers with that form
private void ANewUserHasBeenRegistered(string userName)
{
    // Here you have two options.
    // Reload the grid I.E. call LoadUserList();
    // Try to manually add the new user in the current gridview.
    .....
}

// this will be called by the framework when FormB instance closes. 
// It is important to avoid any possibility to reference a destroyed form 
// using the RegistrationForm variable.
private void FormBHasBeenClosed(object sender, FormClosedEventArgs e) 
{
    this.RegistrationForm = null;
}

The last point to address is the fact that FormB could be opened AFTER the FormA has been opened. This happens probably from some kind of menuItem code that you need to adjust with:

FormB f = new FormB();

// Search if there is an instance of FormA around
// If found then pass the instance of the FormB to the RegistrationForm public property of FormA
FormA currentFormA = Application.OpenForms.Cast<Form>().OfType<FormA>().FirstOrDefault ();
if(currentFormA != null)
    currentFormA.RegistrationForm = f;

// Allow users to register.....
f.ShowDialog();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM