【问题标题】:How to reload the data in data grid view after closing another form?关闭另一个表单后如何在数据网格视图中重新加载数据?
【发布时间】:2015-11-03 15:51:24
【问题描述】:

我正在做一个简单的程序,在数据网格视图中显示某些客户端的数据,并且有一个添加新客户端选项。我通过为此打开新表格来做到这一点。问题是添加新客户端时,它不会以第一种形式出现在数据网格中。我制作了一个“刷新”按钮,它可以工作,但我希望这个任务由程序自动完成。到目前为止,这是我的代码:

private void Form1_Load(object sender, EventArgs e)
    {
        List<Client> list = new List<Client>();
        list = cnn.Select();
        dataGridClients.DataSource = list;
    }

private void btnRefresh_Click(object sender, EventArgs e)
    {
        List<Client> list = new List<Client>();
        list = cnn.Select();
        dataGridClients.DataSource = list;
    }

这是我用来显示数据的函数。这是添加客户端表单代码:

private void AddClientFunc(object sender, EventArgs e)
    {
        string date = dateSelected.Year +"-"+ dateSelected.Month +"-"+ dateSelected.Day;
        string cycLenght = txtBoxInsertCycle.Text;
        int len;
        if(txtBoxInsertName.Text != "" && txtBoxInsertDate.Text != "")
        {
            try
            {
                len= Convert.ToInt32(cycLenght);
                cnn.Insert(txtBoxInsertName.Text, date, len);
            }
            catch (FormatException ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            this.Close();
        }
        else
        {
            MessageBox.Show("Please fill all the fields", "Error");
        }
    }

这是单击按钮时调用的添加客户端函数。这是一张照片,你可以更好地了解我在做什么:

那么如何自动刷新呢?

【问题讨论】:

  • 什么是cnn?并且您将列表绑定到数据网格,但您修改了 cnn!
  • cnn 是来自我的类​​ DBconnect 的变量,它是与数据库类的连接。

标签: c# winforms datagridview


【解决方案1】:

可能有一种方法可以在窗口关闭时调用刷新。

但也许利用 bindingSource 而不是列表是处理网格数据的更好方法。这样,您就不会尝试使本地列表和数据网格数据源保持同步。 BindingSource 是网格/应用程序数据的单一权限。

我创建了一个新的 winform 项目。添加了一个客户端类,将其添加为打开 winform 设计器的对象数据源。然后将类拖放到表单上,它创建了一个带有绑定源的数据网格视图。

这是后面的表单代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            //var clientBindingSource = new BindingSource(); // Set in designer
            clientBindingSource.Add(new Client {Id = 1, Name = "Rob"});
            clientBindingSource.Add(new Client {Id = 2, Name = "Tim"});

            //clientDataGridView.DataSource = clientBindingSource; // Set in designer
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Updating the binding source updates the grid            
            clientBindingSource.Add(new Client {Id = 3, Name = "Kathy"});
            //clientBindingSource.EndEdit(); // May or may not have to end edit to see results
        }
    }
}

【讨论】:

  • 问题不是显示数据,而是在第二个窗体关闭时显示新数据。问题是第二种形式是其他类,数据网格视图和绑定源在第二种形式(添加客户端)上下文中不知道
【解决方案2】:

您可以使用委托创建事件。

您可以观看:https://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

【讨论】:

    【解决方案3】:

    因为您将list 绑定到您的数据网格。您只需将新客户添加到您的列表中。您只需将以下行附加到您的代码中。

    list = cnn.Select();
    

    cnn.Insert(txtBoxInsertName.Text, date, len);
    

    在您的尝试块中。似乎cnn.select() 只是选择了所有数据。你也可以写

    list.add(x);
    

    其中 x 是新添加的客户端。但我不知道这与您的数据库有何对应关系。我的第一个解决方案效率不高,因为您总是会覆盖您已有的大部分内存。

    只需将此添加到您的第二个表单中

    List<Client> list;
    

    并更改您的 2nd Form 的构造函数

    public Form2(List<Client> list)
    {
     this.list=list;
    ...
    }
    

    所以列表也会出现在您的第二个表单中。

    【讨论】:

    • 问题不是显示数据,而是在第二个窗体关闭时显示新数据。问题是第二种形式是其他类,数据网格视图和列表在第二种形式(添加客户端)上下文中是未知的
    • 然后在加载表单时将列表作为参数提交(更新我的帖子)。
    猜你喜欢
    • 2018-07-23
    • 1970-01-01
    • 1970-01-01
    • 2021-08-08
    • 1970-01-01
    • 2015-03-17
    • 2019-06-10
    • 1970-01-01
    • 2020-11-22
    相关资源
    最近更新 更多