目录
在一些软件,比如,进销存管理系统中添加销售单信息时,每个销售单都可能对应多种商品,而且在向销售单中添加商品时,一般都是在新弹出的窗体中选择商品,这时就涉及通过子窗体刷新父窗体的问题。
1、实现方法
实现通过子窗体刷新父窗体时,主要通过在自定义事件中执行数据绑定来对主窗体进行刷新,即当子窗体产生更新操作时,通过子窗体的一个方法触发主窗体中对应的事件,这个过程主要用到了EventHandler事件。
EventHandler事件主要是通过EventHandler委托来实现的,该委托表示处理不包含事件数据的事件的方法。语法格式如下:
public delegate void EventHandler(Object sender,EventArgs e)
参数说明
Sender:事件源。
e:不包含任何事件数据的EventArgs事件数据。
例如,通过使用EventHandler事件为子窗体添加一个事件处理程序,以便能够刷新父窗体中的数据。
在子窗体里:
/// <summary>
/// 某一个事件,比如,添加
/// </summary>
private void Button1_Click(object? sender, EventArgs e)
{
GlobalFlag = true; //设定标识的值为true
if (!(comboBox1.Items.Equals(idContent))) //当Combobox控件中不存在将添加的信息时
{
comboBox1.Items.Add(idContent!); //在Combobox控件中添加一条记录
}
UpdateData();
}
// 其它
/// <summary>
/// 更新DataGridView控件中的内容
/// </summary>
protected void UpdateData()
{
UpdateDataGridView?.Invoke(this, EventArgs.Empty);
}
在主窗体里:
// 添加子窗体的事件
public void CreateFrmChild()
{
Frm_Child BabyWindow = new()
{
MdiParent = this,
};
dataGridView1.Controls.Add(BabyWindow);
BabyWindow.UpdateDataGridView += new EventHandler(BabyWindow_UpdateDataGridView!);
BabyWindow.Show();
}
// 其它
/// <summary>
/// 实现事件委托
/// </summary>
void BabyWindow_UpdateDataGridView(object sender, EventArgs e)
{
// 其它
}
2.SqlCommand类
本实例中使用了SQL,主窗体的数据读取自SQL,在子窗体里更新,然后回写SQL,其结果再次更新到主窗体。
在C#中执行SQL语句时,可以使用SqlCommand类,该类主要用于向SQL Server数据库发送SQL语句。
本实例的数据库连接字符串:
//数据库连接字符串
readonly string? ConnString = "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;";
还有,在.NET 8.0中,要使用命名空间:using Microsoft.Data.SqlClient;因为 using System.Data.SqlClient; 已经废弃。
3.实例的主窗体Frm_Main:
(1)Frm_Main.Designer.cs
namespace _197
{
partial class Frm_Main
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
menuStrip1 = new MenuStrip();
toolStripMenuItem1 = new ToolStripMenuItem();
toolStripMenuItem2 = new ToolStripMenuItem();
toolStripMenuItem3 = new ToolStripMenuItem();
dataGridView1 = new DataGridView();
menuStrip1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
SuspendLayout();
//
// menuStrip1
//
menuStrip1.BackColor = SystemColors.ControlDark;
menuStrip1.Items.AddRange(new ToolStripItem[] { toolStripMenuItem1 });
menuStrip1.Location = new Point(0, 0);
menuStrip1.Name = "menuStrip1";
menuStrip1.Size = new Size(335, 25);
menuStrip1.TabIndex = 0;
menuStrip1.Text = "menuStrip1";
//
// toolStripMenuItem1
//
toolStripMenuItem1.DropDownItems.AddRange(new ToolStripItem[] { toolStripMenuItem2, toolStripMenuItem3 });
toolStripMenuItem1.Name = "toolStripMenuItem1";
toolStripMenuItem1.Size = new Size(44, 21);
toolStripMenuItem1.Text = "操作";
//
// toolStripMenuItem2
//
toolStripMenuItem2.Name = "toolStripMenuItem2";
toolStripMenuItem2.Size = new Size(136, 22);
toolStripMenuItem2.Text = "添加或删除";
toolStripMenuItem2.Click += ToolStripMenuItem2_Click;
//
// toolStripMenuItem3
//
toolStripMenuItem3.Name = "toolStripMenuItem3";
toolStripMenuItem3.Size = new Size(136, 22);
toolStripMenuItem3.Text = "退出";
toolStripMenuItem3.Click += ToolStripMenuItem3_Click;
//
// dataGridView1
//
dataGridView1.AllowUserToAddRows = false;
dataGridView1.ColumnHeadersHeightSizeMod

最低0.47元/天 解锁文章
366

被折叠的 条评论
为什么被折叠?



