通过单击按钮刷新另一个表单 DataGridView

Posted

技术标签:

【中文标题】通过单击按钮刷新另一个表单 DataGridView【英文标题】:Refresh another forms DataGridView from button click 【发布时间】:2014-02-17 21:47:20 【问题描述】:

我有一个表单 (customersForm) 显示带有客户信息的 datagridview。第二个表单 (viewForm) 允许用户查看、编辑、删除和更新选定的 datagridview 行。当我单击更新或删除时,我希望 customerForm 上的 datagridview 刷新显示更新的数据。如何通过单击按钮执行此操作?

这是viewForms的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;



namespace Lewis_Warby_Airbrushing

    public partial class viewForm : Form
    
        DataRowView Data = null;
        public viewForm(DataRowView dr)
        
            InitializeComponent();
            Data = dr;
            

        private void closeBTN_Click(object sender, EventArgs e)
        

            this.Close();
        

        private void viewForm_Load(object sender, EventArgs e)
        
            refTxt.Text = Data["Reference"].ToString().Trim();
            firstTxt.Text = Data["First Name"].ToString().Trim();
            surenameTxt.Text = Data["Surename"].ToString().Trim();
            address1Txt.Text = Data["Address Line 1"].ToString().Trim();
            address2Txt.Text = Data["Address Line 2"].ToString().Trim();
            countyTxt.Text = Data["County"].ToString().Trim();
            postTxt.Text = Data["Post Code"].ToString().Trim();
            contactTxt.Text = Data["Contact Number"].ToString().Trim();
            emailTxt.Text = Data["Email Address"].ToString().Trim();



        

        private void deleteBTN_Click(object sender, EventArgs e)
        
            if (MessageBox.Show("Customer information will be perminantly deteled. Do you with to continue? ", "Confirm Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            
                string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
                string Query = "delete from customersTBL where Reference ='" + this.refTxt.Text + "';";
                SqlCeConnection conDataBase = new SqlCeConnection(constring);
                SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase);
                SqlCeDataReader myReader;
                try
                
                    conDataBase.Open();
                    myReader = cmdDataBase.ExecuteReader();
                    MessageBox.Show("Customer information has been deleted", "Deleted Sucessfully");
                    while (myReader.Read())
                    

                    
                    MessageBox.Show("Please exit the Customers window and re-open to update the table");
                    this.Close();
                    //displays a system error message if a problem is found
                
                catch (Exception ex)
                
                    MessageBox.Show(ex.Message);

               



            

        

        private void editBTN_Click(object sender, EventArgs e)

            
                bool notEditable = true;
                if (editBTN.Text == "Update")
                
                    int UserID = Convert.ToInt32(refTxt.Text);
                    UpdateDataBase( UserID );

                    editBTN.Text = "Edit";
                    deleteBTN.Visible = true;
                    notEditable = true;
                
                else
                
                    deleteBTN.Visible = false;
                    editBTN.Text = "Update";
                    deleteBTN.Visible = false;
                    notEditable = false;
                
                firstTxt.ReadOnly = notEditable;
                surenameTxt.ReadOnly = notEditable;
                address1Txt.ReadOnly = notEditable;
                address2Txt.ReadOnly = notEditable;
                countyTxt.ReadOnly = notEditable;
                contactTxt.ReadOnly = notEditable;
                emailTxt.ReadOnly = notEditable;
                postTxt.ReadOnly = notEditable;

        

private void UpdateDataBase(int customerID)
         
             if (MessageBox.Show("Customer information will be updated. This change cannot be undone. Are you sure you want to continue? ", "Confirm Edit", MessageBoxButtons.YesNo) == DialogResult.Yes)
             
                 string constring = @"Data Source=|DataDirectory|\LWADataBase.sdf";
                 string Query = @"update customersTBL set [First Name] = @fname,
                  surename = @sur, [Address Line 1] = @addr1,
                  [Address Line 2] = @addr2, County = @county,
                  [Post Code] = @pcode, [Email Address] = @mail, [Contact Number] = @ctNo
                  WHERE Reference = @id";
                 using (SqlCeConnection conDataBase = new SqlCeConnection(constring))
                 using (SqlCeCommand cmdDataBase = new SqlCeCommand(Query, conDataBase))
                 
                     try
                     
                         conDataBase.Open();
                         cmdDataBase.Parameters.AddWithValue("@fname", this.firstTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@sur", this.surenameTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@addr1", this.address1Txt.Text);
                         cmdDataBase.Parameters.AddWithValue("@addr2", this.address2Txt.Text);
                         cmdDataBase.Parameters.AddWithValue("@county", this.countyTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@pcode", this.postTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@mail", this.emailTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@ctNo", this.contactTxt.Text);
                         cmdDataBase.Parameters.AddWithValue("@id", customerID);
                         int rowsUpdated = cmdDataBase.ExecuteNonQuery();
                         if (rowsUpdated == 0)
                             MessageBox.Show("No customer found to update");
                         MessageBox.Show("Customer information sucessfully updated", "Update Sucessfull");
                     
                     catch (Exception ex)
                     
                         MessageBox.Show(ex.Message);
                     
                 
             

customerForm的完整代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace Lewis_Warby_Airbrushing

    public partial class customerForm : Form
    
        public customerForm()
        
            InitializeComponent();
        




        public void customerForm_Load(object sender, EventArgs e)
        

            // TODO: This line of code loads data into the 'lWADataBaseDataSet.customersTBL' table. You can move, or remove it, as needed.
            timer1.Start();
            this.lWADataBaseDataSet.EnforceConstraints = false;
            this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);

        

        private void viewBTN_Click(object sender, EventArgs e)
        
            int selectedRowIndex = customersTBLDataGridView.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = customersTBLDataGridView.Rows[selectedRowIndex];


            viewForm frm2 = new viewForm((DataRowView)selectedRow.DataBoundItem);
            frm2.ShowDialog();


        
        addForm addForm = new addForm();
        private void addBTN_Click(object sender, EventArgs e)
        
  if (addForm == null || addForm.IsDisposed == true)
                addForm = new addForm();
  addForm.ShowDialog();
  this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);

        

        int count = 0;
        private void timer1_Tick(object sender, EventArgs e)
        
            count = customersTBLBindingSource.Count;
            statusLBL.Text = "You currently have "+count.ToString()+" customer(s) stored in this database";
        
        searchForm searchForm = new searchForm();
        private void searchBTN_Click(object sender, EventArgs e)
        
            if (searchForm == null || searchForm.IsDisposed == true);
            searchForm = new searchForm();
            searchForm.ShowDialog();
        














        


        

【问题讨论】:

网格绑定了吗?如果是,那是什么? 您的意思是您想让 viewForm 打开以供编辑,但是随着更改发生,您希望这些更改反映在 customerForm 中? 不,viewForm 关闭,我希望 customerForm datagridview 刷新显示更新的数据。 当你关闭 viewForm 时,是否已经将更改推送到数据库中? 是的,添加代码会帮助您回答问题吗? 【参考方案1】:

您没有在问题中指定很多内容,但让我们假设这种情况:

当显示 customerView 时,您调用数据库以使用客户详细信息填充 DataGridView。在某些时候,您要么在 DGV 行上单击鼠标右键,要么选择一行并单击一个按钮,然后显示 viewForm,传入当前选定行中的数据。同样,您没有指定这是如何完成的,但我们假设您传入了某种数据传输对象。

完成编辑后,单击一个按钮,将更改保存到数据库,然后关闭 viewForm。

根据您的 cmets,这是您现在应用程序中的一般工作流程。

在这种情况下,您可以简单地重新获取数据,就像您在 viewForm.ShowDialog() 方法返回时首次显示 customerView 时所做的那样。

除非有其他我没有得到的东西,否则可以像这样轻松完成:

//customerForm
private void button1_Click(object sender, EventArgs e)

        ViewFormClass viewForm = new ViewFormClass();
            viewForm.SetCustomerData(dataObject);
        viewForm.ShowDialog(); // will stop here waiting for viewForm to close
        this.FetchCustomerData();

FetchCustomerData() 与您在打开 customerView 时调用的方法相同。您通常会在那里获取数据并将其绑定到控件。

干杯

编辑:

根据自己的代码,简单修改一下:

        private void viewBTN_Click(object sender, EventArgs e)
        
            int selectedRowIndex = customersTBLDataGridView.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = customersTBLDataGridView.Rows[selectedRowIndex];


            viewForm frm2 = new viewForm((DataRowView)selectedRow.DataBoundItem);
            frm2.ShowDialog();
            this.customersTBLTableAdapter.Fill(this.lWADataBaseDataSet.customersTBL);


        

【讨论】:

我已经编辑了问题并为您添加了代码。 是的,工作流程和我描述的差不多,所以当你关闭 viewForm 时,会在 customerForm 中恢复执行,所以只需重新获取数据并将数据重新绑定到 DataGridView。跨度> 我需要如何调整我的代码以使其按我的意愿工作?我很难理解它。 显示你的代码来填充customerView DataGridView,我会调整我的。 我已经在问题中发布了它,它是完整的代码

以上是关于通过单击按钮刷新另一个表单 DataGridView的主要内容,如果未能解决你的问题,请参考以下文章

单击表单中的按钮会导致页面刷新

单击表单中的按钮会导致页面刷新

如何通过关闭当前的aspx来触发另一个aspx中的按钮?

访问VBA无法刷新表单

如何通过php中的按钮单击将AJAX信息传递给另一个用户?

当通过链接或后退按钮打开时,强制 JSF 刷新页面/视图/表单