Return 在 c# windows 应用程序中单击按钮的网格值

Return the value to a grid from a button click in c# windows application

在我的 C# windows 应用程序中,双击网格单元格我正在填充一个 Windows 表单,用于在付款后保存 payment.So 填充的表单应该传递 id到网格并刷新网格中的数据。 Nutshell:I 需要通过使用委托和事件来实现这一点,因为网格值从付款后的 db.Each 开始填充,网格需要 refreshed.ie,第二种形式的按钮应该触发父级表单根据点击返回值刷新数据网格。 Screenshot attached

Form1

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);
            
        }
    }

一种简单的方法是,您可以使用 public 属性和 Application.OpenForms Property 来实现它。

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

首先,我们需要定义一个属性来访问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();
    }
}