简体   繁体   English

如何在C#中使用datagridview从数据库自动刷新数据

[英]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. 我有一个具有两个表单的程序,管理员可以访问表单A,而随机用户则可以使用表单B。

Form A is used for searching for the list of registered users. 表格A用于搜索注册用户列表。 Form B is used for registering. 表格B用于注册。

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... 表单A具有从数据库中获取数据的datagridview,我希望datagridview在有人从表单B注册后自动刷新其中的数据...

I want to get the data from the database and put it into datagridview automatically without closing the form and opening it again... 我想从数据库中获取数据并将其自动放入datagridview中,而无需关闭表单并再次打开它...

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. 在您的情况下, FormB发布有关新用户已注册的信息。 When a new user register itself the FormB checks if anyone has registered itself to be notified of the event. 当新用户注册自己时,FormB将检查是否有人注册了自己以通知该事件。 The FormA registers itself to receive the notification of the fact. FormA进行注册以接收事实通知。

In code you have this in FormB 在代码中,您在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. 现在,根据您的解释,最大的问题是FormA在FormB中订阅事件的方式。 Suppose that, when the FormA loads, the FormB is already opened. 假设在加载FormA时,FormB已经打开。 You need to search for the instance of FormB 您需要搜索FormB的实例

FormA.cs code FormA.cs代码

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. 最后要解决的事实是,可以在打开FormA之后打开FormB。 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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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