C#窗体如何在点击"删除"按钮、成功删除数据后,重新加载窗体?
这是窗体界面图
下面是删除按钮里面的代码
private void del_fm_Click(object sender, EventArgs e)
{
int id = Convert.ToInt32(((Button)sender).Tag);
bool num = new BLL_NetworManagement.BLL_FMmodulator().Delete(id);
if (num)
{
this.Invalidate();
//this.Refresh();
MessageBox.Show("删除成功");
}
else
{
MessageBox.Show("删除失败");
}
}
用户控件
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
namespace Test
{
public partial class Controller : UserControl
{
private Label lblTime;
private Label lblSysStatus;
private RadioButton rbtnSysStatus;
private Button btnLookup;
private Button btnDelete;
public event EventHandler Lookup;
public event EventHandler Delete;
public string SysTime
{
get { return lblTime.Text; }
set { lblTime.Text = value; }
}
public string SysStatusMsg
{
get { return lblSysStatus.Text; }
set { lblSysStatus.Text = value; }
}
public bool SysStatus
{
get { return rbtnSysStatus.Checked; }
set { rbtnSysStatus.Checked = value; }
}
public Controller()
{
InitializeComponent();
SetupControl();
}
//可以直接在IDE布局这些控件,这里用于讲解
private void SetupControl()
{
this.SuspendLayout();
lblTime = new Label();
lblTime.Name = "lblTime";
lblTime.Location = new Point(10, 20);
lblSysStatus = new Label();
lblSysStatus.Name = "lblSysStatus";
lblSysStatus.Location = new Point(10, 50);
rbtnSysStatus = new RadioButton();
rbtnSysStatus.Name = "rbtnSysStatus";
rbtnSysStatus.Text = "";
rbtnSysStatus.Location = new Point(80, 45);
btnLookup = new Button();
btnLookup.Name = "btnLookup";
btnLookup.Text = "查看";
btnLookup.Width = 50;
btnLookup.Location = new Point(10, 90);
btnLookup.Click += new EventHandler(btnLookup_Click);
btnDelete = new Button();
btnDelete.Name = "btnDelete";
btnDelete.Text = "删除";
btnDelete.Width = 50;
btnDelete.Location = new Point(70, 90);
btnDelete.Click += new EventHandler(btnDelete_Click);
this.Controls.Add(lblTime);
this.Controls.Add(rbtnSysStatus);
this.Controls.Add(lblSysStatus);
this.Controls.Add(btnLookup);
this.Controls.Add(btnDelete);
this.BorderStyle = BorderStyle.FixedSingle;
this.BackColor = Color.LightSkyBlue;
this.ResumeLayout();
}
private void btnLookup_Click(object sender, EventArgs e)
{
OnLookup(e);
}
private void btnDelete_Click(object sender, EventArgs e)
{
OnDelete(e);
}
private void OnDelete(EventArgs e)
{
EventHandler temp = Delete;
if (temp != null)
temp(this,e);
}
private void OnLookup(EventArgs e)
{
EventHandler temp = Lookup;
if (temp != null)
temp(this, e);
}
}
}
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Test
{
public partial class Form1 : Form
{
FlowLayoutPanel controllers;
public Form1()
{
InitializeComponent();
SetupControl();
}
private void SetupControl()
{
controllers = new FlowLayoutPanel();
controllers.AutoScroll = true;
controllers.Dock = DockStyle.Fill;
controllers.BackColor = Color.LightSkyBlue;
groupBox1.Controls.Add(controllers);
LoadControllers();
}
//可以从数据库读取数据进行绑定
private void LoadControllers()
{
for (int i = 0; i < 15; i++)
{
Controller ctler = new Controller();
ctler.Name = i.ToString();
ctler.SysTime = DateTime.Now.ToString();
ctler.SysStatusMsg = "系统待机";
ctler.SysStatus = i % 2 == 0 ? true : false;
ctler.Lookup += new EventHandler(ctler_Lookup);
ctler.Delete += new EventHandler(ctler_Delete);
controllers.Controls.Add(ctler);
}
}
//删除,从容器移除它
private void ctler_Delete(object sender, EventArgs e)
{
Controller ctler = sender as Controller;
if (ctler != null)
{
if (controllers.Controls.Contains(ctler))
controllers.Controls.Remove(ctler);
}
}
//可以处理查看功能
private void ctler_Lookup(object sender, EventArgs e)
{
MessageBox.Show((sender as Controller).Name);
}
}
}