【问题标题】:Return the value to a grid from a button click in c# windows application从 c# windows 应用程序中的按钮单击将值返回到网格
【发布时间】:2021-03-18 11:24:24
【问题描述】:

在我的 C# windows 应用程序中,从网格单元双击我正在填充一个用于保存付款的 Windows 窗体。因此,在付款后,填充的窗体应该将 id 传递给网格并刷新网格中的数据。 简而言之:我需要通过使用委托和事件来实现这一点,因为网格值是从数据库中填充的。每次付款后,网格都需要刷新。即,第二种形式中的按钮应该触发父表单刷新数据网格点击返回值。 Screenshot attached

表格1

private void dgvLoadBalance_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        long cellValue = long.Parse(dgvLoadBalance.Rows[e.RowIndex].Cells[2].Value.ToString());
        OPPaymentDatatable= commonDL.GetOpPaymentDetails(opRegId, cellValue);
        opDataTable = new DataTable();
        opDataTable.Columns.Add("PatientName", typeof(string));
        opDataTable.Columns.Add("DoctorName", typeof(string));
        opDataTable.Columns.Add("BillTypeName", typeof(string));
        opDataTable.Columns.Add("OPId", typeof(string));
        opDataTable.Columns.Add("OPNumber", typeof(string));
        opDataTable.Rows.Add(uctrlPatientSearch1.PatientName, uctrlPatientSearch1.DoctorName, uctrlPatientSearch1.BillTypeName, uctrlPatientSearch1.OPId,uctrlPatientSearch1.OPNumber);
        var settingsdt = commonBL.GetSettings();
        if (settingsdt.Rows.Count > 0 && settingsdt != null)
        {
            Address.Add(settingsdt.Rows[1][2]);
            Address.Add(settingsdt.Rows[2][2]);
            Address.Add(settingsdt.Rows[3][2]);
            Address.Add(settingsdt.Rows[4][2]);
            Address.Add(settingsdt.Rows[5][2]);
            Address.Add(settingsdt.Rows[6][2]);
            Address.Add(settingsdt.Rows[7][2]);
            Address.Add(settingsdt.Rows[8][2]);
        }
        frmPayOPBalance frmbalancepay = new frmPayOPBalance(OPPaymentDatatable, opDataTable, Address);
        frmbalancepay.ShowDialog();
    }

Form2

 private void btnPaynBill_Click(object sender, EventArgs e)
    {
        if (ValidateForm())
        {
            BindData();
            outVal = commonBL.InsertOpBalanceHistoryPayment(opPaymentModel);
            if (outVal > 0)
            {
                var dt = commonBL.GetOpBalanceBill(opPaymentModel);
                if (dt != null && dt.Rows.Count > 0)
                    opPaymentModel.BillNumber = long.Parse(dt.Rows[0]["BillId"].ToString());
                MessageBox.Show("Balance Payment made successfully", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                particularsDt = new DataTable();
                particularsDt.Columns.Add("Particulars", typeof(string));
                particularsDt.Columns.Add("Rate", typeof(string));
                particularsDt.Columns.Add("Amount", typeof(decimal));
                particularsDt.Rows.Add("OP Balance Payment for Reciept # " + opPaymentModel.OldBillId, string.Empty, opPaymentModel.BalAmount);
                BalancePaymentBillPrint objBalaPayPrint = new BalancePaymentBillPrint(opPaymentModel, Address, particularsDt);
                ClearBindings();
                this.Hide();
            }
            else
                MessageBox.Show("Error found in bill entry", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
            
        }
    }

【问题讨论】:

    标签: c# winforms events delegates


    【解决方案1】:

    一个简单的方法是你可以使用公共属性和Application.OpenForms Property来实现它。

    这是一个演示。在这个例子中,有一个 DataGridView 有两列“ID”和“Number”。

    首先,我们需要定义一个属性来访问Form1中的DataGridView实例。

    public DataGridView DGV
    {
        get { return dataGridView1; }
        set { dataGridView1 = value; }
    }
    

    然后定义属性获取Form2中的LabelID和TextBoxNumber。

    public Label LBID
    {
        get { return labelID; }
        set { labelID = value; }
    }
    
    public TextBox TBNum
    {
        get { return textBoxNumber; }
        set { textBoxNumber = value; }
    }
    

    然后通过相关属性将值传递给Form2。

    // DataGridView in Form1
    private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
        Form2 form2 = new Form2();
        form2.LBID.Text = dataGridView1.Rows[e.RowIndex].Cells["ID"].Value.ToString();
        form2.TBNum.Text = dataGridView1.Rows[e.RowIndex].Cells["Number"].Value.ToString();
        form2.Show();
    }
    

    最后,我们可以在Form2中使用Application.OpenForms Property来获取Form1实例来修改DataGridview。

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        Form1 form1 = (Form1)Application.OpenForms["Form1"];
        form1.DGV.Rows[Convert.ToInt32(labelID.Text) - 1].Cells["Number"].Value = textBoxNumber.Text;
        this.Close();
    }
    

    测试结果:


    更新:使用委托和事件

    Form1.cs

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        //event handle method
        void frm_TransfEvent(int rowindex, string value)
        {
            // set Number column value
            dataGridView1.Rows[rowindex].Cells["Number"].Value = value;
        }
    
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Form2 frm = new Form2();
            // pass rowindex to form2
            frm.ROWIndex = e.RowIndex;
            //subscribe to event
            frm.TransfEvent += frm_TransfEvent;
            frm.ShowDialog();
        }
    }
    

    Form2.cs

    public delegate void TransfDelegate(int rowindex, string value);
    
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
    
        public event TransfDelegate TransfEvent;
    
    
        public int ROWIndex { get; set; }
    
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //trigger event
            TransfEvent(ROWIndex, textBox1.Text);
            this.Close();
        }
    }
    

    【讨论】:

    • 嘿@Kyle。这个选项看起来不错,但我需要通过使用委托和事件来实现这一点,因为网格值是从数据库中填充的。每次付款后,网格都需要刷新。即,第二个表单中的按钮应该触发父表单根据点击返回值刷新数据网格。
    • @medpicz 已经用代表更新了答案,检查一下。
    • 嗨@kyle 使用委托和事件方法正在工作。在其他一些方法中也实现了相同的方法。感谢您的帮助
    猜你喜欢
    • 2016-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多