【问题标题】:How to requery info in listbox in windows form from another form如何从另一个窗体重新查询 Windows 窗体中列表框中的信息
【发布时间】:2015-12-29 03:07:02
【问题描述】:

我的主 Windows 窗体带有一个在程序启动时填充的 ListBox。我在主窗体上有一个按钮来打开“添加页面”。在这个新窗口中,我可以添加要添加到 ListBox 的新条目的所有信息。但是,当单击“添加”并关闭辅助表单时,我不执行任何操作来更新 ListBox。

有没有办法从辅助窗口调用主窗体的方法 populateCollectionList?我遇到的所有解决方案似乎都使​​解决方案过于复杂。

这是我第一次涉足 Windows 窗体,如有必要,我可以提供任何额外信息。

主窗体:

public partial class frmMain : Form
{
    String connectionString;
    SqlConnection connection;

    public frmMain()
    {
        InitializeComponent();
        connectionString = ConfigurationManager.ConnectionStrings["CollectionsManager.Properties.Settings.CollectionsConnectionString"].ConnectionString;
    }

    private void frmMain_Load(object sender, EventArgs e)
    {
        populateCollectionList();
    }

    private void populateCollectionList()
    {
        using (connection = new SqlConnection(connectionString))
        using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM Bottlecaps", connection))
        {
            DataTable collectionTable = new DataTable();
            adapter.Fill(collectionTable);

            currentItems.ValueMember = "Id";
            currentItems.DisplayMember = "Maker";
            currentItems.DataSource = collectionTable;
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frmAddItem itemAddPage = new frmAddItem();
        itemAddPage.ShowDialog();
    }
}

辅助表格(“添加表格”):

public partial class frmAddItem : Form
{
    String connectionString;
    SqlCommand command;
    SqlConnection connection;

    public frmAddItem()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\mdfPath;Integrated Security=True");
        connection.Open();
        insertTextFields();
        connection.Close();
        this.Close();
        // THIS IS WHERE I NEED TO MAKE THE CALL TO REPOPULATE LIST
    }

    private void insertTextFields()
    {
        command = new SqlCommand("INSERT INTO Table (SomeParams) VALUES (@someParams)", connection);
        command.Parameters.AddWithValue("@param1", txtField1.Text);
        //....
        command.Parameters.AddWithValue("@param2", txtField2.Text);
        command.ExecuteNonQuery();
    }
}

【问题讨论】:

    标签: c# sql-server winforms


    【解决方案1】:

    您可以将公共属性添加到具有对主窗体的引用的 frmAddItem 窗体: public frmMain MainForm;

    然后在 frmMain button1_Click 中将此属性设置为“this”: itemAddPage.MainForm = this;

    然后在frmAddItem button1_Click 中可以调用populateCollectionList: MainForm.populateCollectionList();

    您需要公开 populateCollectionList 函数,以便 frmAddItem 可以访问该方法

    【讨论】:

    • 非常感谢!但是我认为 populateCollectionList 可能不是再次调用的正确方法。因为我尝试了您的解决方案和 Szymon 的解决方案,但它们都不起作用。我是否必须在再次填写之前清除collectionTable?
    【解决方案2】:

    最简单的方法是修改button1_Click 并在对话框关闭时从中调用populateCollectionList。您可以这样做并检查用户是否在对话框中单击了确定:

    private void button1_Click(object sender, EventArgs e)
    {
        frmAddItem itemAddPage = new frmAddItem();
        if (itemAddPage.ShowDialog() == DialogResult.OK) 
        {
            populateCollectionList();
        }
        itemAddPage.Dispose();
    }
    

    您可以在 MSDN 上的 Form.ShowDialog 方法页面上找到类似的示例。


    您还需要修改您的populateCollectionList 以刷新列表框。在设置数据源之前添加以下内容:

    currentItems.DataSource = null;
    

    没有这个数据源将不会刷新。

    【讨论】:

    • 嗯,但是当 button1_Click 运行时,在用户有时间做任何事情之前不会进行检查吗?检查会持续运行吗?我试过了,但没有用。但话又说回来,pcowan 的建议也没有奏效。所以也许它与其他东西有关。
    • @user3750325 ShowDialog 会将对话框显示为模态,因此它将阻止其他所有内容。
    • 你把button1的DialogResult设置为OK了吗?
    • 不,它被设置为无。但是,将其更改为 OK 并不能解决问题。
    • 当你说它不起作用时,究竟是什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多