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

     2023-05-08     145

关键词:

【中文标题】通过单击按钮刷新另一个表单 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,我会调整我的。 我已经在问题中发布了它,它是完整的代码

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

】单击表单中的按钮会导致页面刷新【英文标题】:Clickingabuttonwithinaformcausespagerefresh【发布时间】:2012-09-0110:26:14【问题描述】:我在Angular中有一个表单,里面有两个按钮标签。一键在ng-click上提交表单。另一个按钮纯粹用于... 查看详情

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

】单击表单中的按钮会导致页面刷新【英文标题】:Clickingabuttonwithinaformcausespagerefresh【发布时间】:2021-09-3018:13:17【问题描述】:我在Angular中有一个表单,里面有两个按钮标签。一键在ng-click上提交表单。另一个按钮纯粹用于... 查看详情

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

我有一种情况是在先前打开的aspx页面中触发一个按钮并关闭当前的aspx页面当我开始申请时,表格1->表格2。注意:两个表单都打开执行某些操作后,表单2应该关闭,表单1应该刷新。为了刷新我有按钮,我想从表格2触发它答案... 查看详情

单击属于表单的另一个按钮时如何禁用提交表单

】单击属于表单的另一个按钮时如何禁用提交表单【英文标题】:Howtodisablesubmittheformwhenclickaanotherbuttonthatbelongstotheform【发布时间】:2021-11-2722:03:47【问题描述】:我编写了一个购物车页面并使用两个按钮来减少和增加数量,并... 查看详情

如何通过php中的按钮单击将ajax信息传递给另一个用户?(代码片段)

我想实现AJAX,以便在有新的通知传入给用户时自动刷新内容。我想要实现的目标示例:userA向userB发送了一个朋友请求,因此userB收到通知,但通知仅在页面刷新后显示。每当数据库更新/插入新值时,我想自动刷新通知。AJAX:$('... 查看详情

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

】当通过链接或后退按钮打开时,强制JSF刷新页面/视图/表单【英文标题】:ForceJSFtorefreshpage/view/formwhenopenedvialinkorbackbutton【发布时间】:2016-07-2617:36:23【问题描述】:我有一个将数据发布到外部页面的JSF页面。数据从JSF托管bean... 查看详情

单击另一个表单中的按钮提交时如何在c#中重新加载表单?

】单击另一个表单中的按钮提交时如何在c#中重新加载表单?【英文标题】:Howtoreloadforminc#whenbuttonsubmitinanotherformisclick?【发布时间】:2013-03-3112:58:16【问题描述】:我的C#中有一个组合框,它位于名为frmMain的表单中,当我在我... 查看详情

通过一个按钮根据变量“id”刷新多个 iframe

】通过一个按钮根据变量“id”刷新多个iframe【英文标题】:refreshmultipleiframesbasedonavariable"id"fromonebutton【发布时间】:2011-08-1502:25:21【问题描述】:我有一个页面上有多个iframe的php页面。如果您愿意,这些iframe会包含一... 查看详情

如何通过单击按钮转到另一个视图

】如何通过单击按钮转到另一个视图【英文标题】:Howtogotoanotherviewwithbuttonclick【发布时间】:2019-08-0115:01:20【问题描述】:我的代码中有一个按钮,我有一个名为LogindView.swift的文件单击按钮时,我无法获取打开另一个视图文件... 查看详情

页面刷新后保留值

...证按钮”)。我在我的项目中定义了一些全局变量。当我单击按钮时,会发生一些表单验证,并且另一个按钮(“保存按钮”)变得可见。问题是当我单击验证按钮时,当前页面刷新并且全局变量不包含值。它显示为空。即使在... 查看详情

如何通过单击按钮更改另一个对象的动画?

】如何通过单击按钮更改另一个对象的动画?【英文标题】:Howtochangeanotherobject\'sanimationfrombuttonclick?【发布时间】:2021-06-2911:54:39【问题描述】:我创建了这个按钮预制件,它使用内置的UnityButton组件、带有RaycastTarget的图像组... 查看详情

如何通过单击另一个列表视图 WPF 中的元素来刷新列表视图

】如何通过单击另一个列表视图WPF中的元素来刷新列表视图【英文标题】:howtorefreshalistviewbyclickingelementinanotherlistviewWPF【发布时间】:2021-12-2321:34:55【问题描述】:我正在尝试编写一个POS系统。我想要两个列表,一个带有文章... 查看详情

从另一个表单刷新所有子表单

...board,另一个是Edit。编辑后,我有一个重新打开仪表板的按钮。它可以打开仪表板,但我希望它刷新该仪表板中的子表单。【问题讨论】:为什么需要刷新子表单?仪表板打开时应该是“新鲜的”。您的意思是仪表板保持打开状... 查看详情

通过单击按钮将焦点设置到另一个控件

】通过单击按钮将焦点设置到另一个控件【英文标题】:Settingfocusontoanothercontolfrombuttonclick【发布时间】:2010-12-0101:06:09【问题描述】:我正在使用ASP.NET2.0和VB.NET(C#代码也会帮助我)在我的页面顶部,我有一个名为btnViewRecords... 查看详情

如何通过单击按钮跳转另一个选项卡?

】如何通过单击按钮跳转另一个选项卡?【英文标题】:Howcanjumpanothertabwithasingleclickonabutton?【发布时间】:2011-07-0712:59:46【问题描述】:-(void)SaveMainScreenContoller*main=[[MainScreenContolleralloc]initWithNibName:@"MainScreenContoller"bundle:nil];[sel 查看详情

从另一个表单刷新 dataGridView

...有Form1和Form2。Form1有一个dataGridView和一个用于打开Form2的按钮。我在Form1中编写了一个方法,如下所示,可以完美刷新dataGridView:publicvoidRefreshGrid()dataGridView 查看详情

如何通过单击一个按钮保存 asp.net 文本框值并将该数据用于另一个按钮单击?

】如何通过单击一个按钮保存asp.net文本框值并将该数据用于另一个按钮单击?【英文标题】:Howtosaveasp.nettextboxvalueswithonebuttonclickandusethatdataforanotherbuttonclick?【发布时间】:2017-11-2413:51:20【问题描述】:我有一个表格,其中有一... 查看详情

python在按钮单击时在一个表单中创建记录时自动创建另一个模型的记录(代码片段)

查看详情