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.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; dataGridView1.Dock = DockStyle.Fill; dataGridView1.Location = new Point(0, 25); dataGridView1.Name = "dataGridView1"; dataGridView1.Size = new Size(335, 178); dataGridView1.TabIndex = 1; // // Frm_Main // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(335, 203); Controls.Add(dataGridView1); Controls.Add(menuStrip1); IsMdiContainer = true; MainMenuStrip = menuStrip1; Name = "Frm_Main"; Text = "主窗体"; Load += Frm_Main_Load; menuStrip1.ResumeLayout(false); menuStrip1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit(); ResumeLayout(false); PerformLayout(); } #endregion private MenuStrip menuStrip1; private ToolStripMenuItem toolStripMenuItem1; private ToolStripMenuItem toolStripMenuItem2; private ToolStripMenuItem toolStripMenuItem3; private DataGridView dataGridView1; } }
(2)Frm_Main.cs
// 通过子窗体刷新父窗体 // 设置主窗体的IsMdiContainer=true; using System.Data; using Microsoft.Data.SqlClient; namespace _197 { public partial class Frm_Main : Form { public Frm_Main() { InitializeComponent(); } #region 声明的变量 private static bool flag = false; //标识是否创建新的子窗体 readonly Frm_Child BabyWindow = new();//实例化一个子窗体 readonly DataSet PubsSet = new(); //定义一个数据集对象 private static string[]? iDArray; //声明一个一维字符串数组 public DataTable? IDTable; //声明一个数据表对象 SqlDataAdapter? IDAdapter; //声明一个数据读取器对象 SqlDataAdapter? PubsAdapter; //声明一个数据读取器对象 SqlConnection? ConnPubs; //声明一个数据库连接对象 SqlCommand? PersonalInformation; //声明一个执行SQL语句的对象 public static string[]? IDArray { get => iDArray; set => iDArray = value; } public static bool Flag { get => flag; set => flag = value; } readonly string? ConnString //数据库连接字符串 = "Data Source=DESKTOP-3LV13FS;DataBase=db_TomeOne;integrated security=SSPI;TrustServerCertificate=true;"; #endregion private void Frm_Main_Load(object? sender, EventArgs e) { string AdapterString = "select userID as 编号,userName as 姓名 ,phone as 电话,address as 住址 from tb_User";//用于查询的字符串 string IDString = "select userID from tb_User";//读取数据库中用户的ID编号字符串 ConnPubs = new SqlConnection(ConnString);//建立数据库连接 PubsAdapter = new SqlDataAdapter(AdapterString, ConnPubs);//创建PubsAdapter数据读取器 IDAdapter = new SqlDataAdapter(IDString, ConnPubs); //用于读取用户编号的读取器 PubsAdapter.Fill(PubsSet, "tb_User"); //填充PubsSet数据集 IDAdapter.Fill(PubsSet, "ID"); //填充PubsSet数据集 DataTable PubsTable = PubsSet.Tables["tb_User"]!;//将数据写入PubsTable表 IDTable = PubsSet.Tables["ID"]!; //将数据写入ID表 IDArray = new string[IDTable.Rows.Count]; //为数组定义最大长度 dataGridView1.DataSource = PubsTable.DefaultView;//设置dataGridView1的数据源 for (int i = 0; i < IDTable.Rows.Count; i++) //循环遍历数据表中的每一行数据 { for (int j = 0; j < IDTable.Columns.Count; j++)//循环遍历数据表中的每一列数据 { IDArray[i] = IDTable.Rows[i][j].ToString()!;//将数据表中的数据添加至一个一维数组 } } } #region 创建子窗体的CreateFrmChild方法 /// <summary> /// 设置子窗体的父窗体为当前窗体; /// 实例化一个子窗体; /// 在DataGridView控件中添加子窗体; /// 显示子窗体; /// </summary> 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) { if (Frm_Child.GlobalFlag == false) //当单击删除按钮时 { if (ConnPubs!.State == ConnectionState.Closed) //当数据库处于断开状态时 { ConnPubs.Open(); //打开数据库的连接 } string AfreshString = "delete tb_User where userID=" + Frm_Child.DeleteID!.Trim();//定义一个删除数据的字符串 PersonalInformation = new SqlCommand(AfreshString, ConnPubs); //执行删除数据库字段 PersonalInformation.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数 ConnPubs.Close(); //关闭数据库 DisplayData(); //显示数据库更新后的内容 MessageBox.Show("数据删除成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } else { if (ConnPubs!.State == ConnectionState.Closed) //当数据库处于关闭状态时 { ConnPubs.Open(); //打开数据库 } string InsertString = "insert into tb_User values('" + Frm_Child.IdContent + "','" + Frm_Child.NameContent + "','" + Frm_Child.PhoneContent + "','" + Frm_Child.AddressContent + "')";//定义一个插入数据的字符串变量 PersonalInformation = new SqlCommand(InsertString, ConnPubs);//执行插入数据库字段 PersonalInformation.ExecuteNonQuery(); //执行SQL语句并返回受影响的行数 ConnPubs.Close(); //关闭数据库 DisplayData(); //显示更新后的数据 MessageBox.Show("数据添加成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); } } #endregion #region 用于读取数据库的DisplayData方法 public void DisplayData() { PubsSet.Clear(); ConnPubs = new SqlConnection(ConnString); string DisplayString = "select userId as 编号,userName as 姓名 ,phone as 电话,address as 住址 from tb_User";//定义读取数据库的字段 SqlDataAdapter PersonalAdapter = new(DisplayString, ConnPubs); //定义一个读取数据库数据的读取器 PersonalAdapter.Fill(PubsSet, "tb_User"); //向表DisplayTable中填充数据 dataGridView1.DataSource = PubsSet.Tables["tb_User"]!.DefaultView; //设定DataGridView控件的数据源 } #endregion /// <summary> /// 添加或删除记录 /// 创建子窗体的CreateFrmChild方法 /// </summary> private void ToolStripMenuItem2_Click(object sender, EventArgs e) { if (flag == false) //判断标识的值决定是否创建窗体 { CreateFrmChild();//创建子窗体 } for (int i = 0; i < dataGridView1.Controls.Count; i++) //循环遍历DataGridView控件上的控件集 { if (dataGridView1.Controls[i].Name.Equals(BabyWindow.Name)) //当存在子窗体时 { flag = true; //改变标识Flag的值 break; //退出循环体 } } } /// <summary> /// 退出 /// </summary> private void ToolStripMenuItem3_Click(object sender, EventArgs e) { Application.Exit(); } } }
4.实例的子窗体Frm_Child:
(1) Frm_Child.Designer.cs
namespace _197 { partial class Frm_Child { /// <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() { groupBox1 = new GroupBox(); button2 = new Button(); button1 = new Button(); Record_Address = new TextBox(); Record_Phone = new TextBox(); Record_Name = new TextBox(); Record_ID = new TextBox(); label4 = new Label(); label3 = new Label(); label2 = new Label(); label1 = new Label(); groupBox2 = new GroupBox(); button3 = new Button(); comboBox1 = new ComboBox(); label5 = new Label(); groupBox1.SuspendLayout(); groupBox2.SuspendLayout(); SuspendLayout(); // // groupBox1 // groupBox1.Controls.Add(button2); groupBox1.Controls.Add(button1); groupBox1.Controls.Add(Record_Address); groupBox1.Controls.Add(Record_Phone); groupBox1.Controls.Add(Record_Name); groupBox1.Controls.Add(Record_ID); groupBox1.Controls.Add(label4); groupBox1.Controls.Add(label3); groupBox1.Controls.Add(label2); groupBox1.Controls.Add(label1); groupBox1.Location = new Point(12, 12); groupBox1.Name = "groupBox1"; groupBox1.Size = new Size(270, 157); groupBox1.TabIndex = 0; groupBox1.TabStop = false; groupBox1.Text = "添加信息"; // // button2 // button2.Location = new Point(187, 56); button2.Name = "button2"; button2.Size = new Size(75, 23); button2.TabIndex = 9; button2.Text = "清空"; button2.UseVisualStyleBackColor = true; button2.Click += Button2_Click; // // button1 // button1.Location = new Point(187, 25); button1.Name = "button1"; button1.Size = new Size(77, 23); button1.TabIndex = 8; button1.Text = "提交"; button1.UseVisualStyleBackColor = true; button1.Click += Button1_Click; // // Record_Address // Record_Address.Location = new Point(72, 118); Record_Address.Name = "Record_Address"; Record_Address.Size = new Size(100, 23); Record_Address.TabIndex = 7; Record_Address.TextChanged += Record_Address_TextChanged; // // Record_Phone // Record_Phone.Location = new Point(72, 87); Record_Phone.Name = "Record_Phone"; Record_Phone.Size = new Size(100, 23); Record_Phone.TabIndex = 6; Record_Phone.TextChanged += Record_Phone_TextChanged; // // Record_Name // Record_Name.Location = new Point(72, 56); Record_Name.Name = "Record_Name"; Record_Name.Size = new Size(100, 23); Record_Name.TabIndex = 5; Record_Name.TextChanged += Record_Name_TextChanged; // // Record_ID // Record_ID.Location = new Point(72, 25); Record_ID.Name = "Record_ID"; Record_ID.Size = new Size(100, 23); Record_ID.TabIndex = 4; Record_ID.TextChanged += Record_ID_TextChanged; // // label4 // label4.AutoSize = true; label4.Location = new Point(11, 118); label4.Name = "label4"; label4.Size = new Size(44, 17); label4.TabIndex = 3; label4.Text = "地址:"; // // label3 // label3.AutoSize = true; label3.Location = new Point(11, 87); label3.Name = "label3"; label3.Size = new Size(44, 17); label3.TabIndex = 2; label3.Text = "电话:"; // // label2 // label2.AutoSize = true; label2.Location = new Point(11, 56); label2.Name = "label2"; label2.Size = new Size(44, 17); label2.TabIndex = 1; label2.Text = "姓名:"; // // label1 // label1.AutoSize = true; label1.Location = new Point(11, 25); label1.Name = "label1"; label1.Size = new Size(44, 17); label1.TabIndex = 0; label1.Text = "编号:"; // // groupBox2 // groupBox2.Controls.Add(button3); groupBox2.Controls.Add(comboBox1); groupBox2.Controls.Add(label5); groupBox2.Location = new Point(12, 175); groupBox2.Name = "groupBox2"; groupBox2.Size = new Size(270, 54); groupBox2.TabIndex = 1; groupBox2.TabStop = false; groupBox2.Text = "删除信息"; // // button3 // button3.Location = new Point(187, 24); button3.Name = "button3"; button3.Size = new Size(75, 23); button3.TabIndex = 2; button3.Text = "删除"; button3.UseVisualStyleBackColor = true; button3.Click += Button3_Click; // // comboBox1 // comboBox1.FormattingEnabled = true; comboBox1.Location = new Point(72, 24); comboBox1.Name = "comboBox1"; comboBox1.Size = new Size(100, 25); comboBox1.TabIndex = 1; // // label5 // label5.AutoSize = true; label5.Location = new Point(11, 30); label5.Name = "label5"; label5.Size = new Size(44, 17); label5.TabIndex = 0; label5.Text = "编号:"; // // Frm_Child // AutoScaleDimensions = new SizeF(7F, 17F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(294, 241); Controls.Add(groupBox2); Controls.Add(groupBox1); MaximizeBox = false; //最大化按钮没有意义 Name = "Frm_Child"; StartPosition = FormStartPosition.CenterScreen; Text = "子窗体"; FormClosing += Frm_Child_FormClosing; Load += Frm_Child_Load; groupBox1.ResumeLayout(false); groupBox1.PerformLayout(); groupBox2.ResumeLayout(false); groupBox2.PerformLayout(); ResumeLayout(false); } #endregion private GroupBox groupBox1; private TextBox Record_Address; private TextBox Record_Phone; private TextBox Record_Name; private TextBox Record_ID; private Label label4; private Label label3; private Label label2; private Label label1; private GroupBox groupBox2; private Button button2; private Button button1; private Button button3; private ComboBox comboBox1; private Label label5; } }
(2)Frm_Child.cs
// 本实例仅可创建一个窗口: // 最大化后没有实际意义,因此关闭MaximizeBox属性值为False namespace _197 { public partial class Frm_Child : Form { #region 变量的声明 public event EventHandler? UpdateDataGridView = null;//定义一个处理更新DataGridView控件内容的方法 private static string? deleteID; //定义一个表示删除数据编号的字符串 private static string? idContent; //该变量用来存储数据编号 private static string? nameContent; //该变量用来存储姓名 private static string? phoneContent; //该变量用来存储电话 private static string? addressContent; //该变量用来存储住址 private static bool globalFlag; //该变量用来标识是否创建新的子窗体 public static string? DeleteID { get => deleteID; set => deleteID = value; } public static string? IdContent { get => idContent; set => idContent = value; } public static string? NameContent { get => nameContent; set => nameContent = value; } public static string? PhoneContent { get => phoneContent; set => phoneContent = value; } public static string? AddressContent { get => addressContent; set => addressContent = value; } public static bool GlobalFlag { get => globalFlag; set => globalFlag = value; } #endregion public Frm_Child() { InitializeComponent(); } /// <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> /// 清空 /// </summary> private void Button2_Click(object? sender, EventArgs e) { Record_ID.Clear(); //清空编号文本框中的内容 Record_Name.Clear(); //清空姓名文本框中的内容 Record_Phone.Clear(); //清空电话号码文本框中的内容 Record_Address.Clear(); //清空居住地址文本框中的内容 Record_ID.Focus(); //设定当前鼠标的焦点在编号文本框中 } /// <summary> /// 删除 /// </summary> private void Button3_Click(object? sender, EventArgs e) { GlobalFlag = false; //设定全局变量表示为false if (comboBox1.Items.Count > 1) //当ComboBox中剩不小于2条内容时 { DeleteID = comboBox1.SelectedItem!.ToString(); //将选中项转化为int型 if (comboBox1.Items.Count != 0) //当ComboBox中剩1条内容时 { comboBox1.Items.Remove(comboBox1.SelectedItem); comboBox1.SelectedIndex = 0; } } UpdateData(); } /// <summary> /// 保存新添加记录的编号 /// </summary> private void Record_ID_TextChanged(object? sender, EventArgs e) { idContent = Record_ID.Text; } /// <summary> /// 保存填入的姓名 /// </summary> private void Record_Name_TextChanged(object? sender, EventArgs e) { nameContent = Record_Name.Text; } /// <summary> /// 保存填入的电话号码 /// </summary> private void Record_Phone_TextChanged(object? sender, EventArgs e) { phoneContent = Record_Phone.Text; } /// <summary> /// 保存填入的地址信息 /// </summary> private void Record_Address_TextChanged(object? sender, EventArgs e) { addressContent = Record_Address.Text; } /// <summary> /// 循环遍历数组中的每一个元素,向Combobox控件中添加内容 /// 默认设定当前选中项的索引为0 /// </summary> private void Frm_Child_Load(object? sender, EventArgs e) { for (int i = 0; i < Frm_Main.IDArray!.Length; i++) { comboBox1.Items.Add(Frm_Main.IDArray[i].ToString()); comboBox1.SelectedIndex = 0; } } /// <summary> /// 设定该值为false表示可以创建新窗体 /// </summary> private void Frm_Child_FormClosing(object? sender, FormClosingEventArgs e) { Frm_Main.Flag = false; } /// <summary> /// 更新DataGridView控件中的内容 /// </summary> protected void UpdateData() { UpdateDataGridView?.Invoke(this, EventArgs.Empty); } } }
5.生成效果
(1)生成
(2)添加
(3)添加后
(4)清空
以上就是C#通过子窗体刷新父窗体的实现方法的详细内容,更多关于C#子窗体刷新父窗体的资料请关注其它相关文章!