废话不多说,直接上实例,对学生表进行事件传值来完成对添加修改窗体的赋值。
(1)创建Student类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace _05事件传值
- {
- public class Student
- {
- //TSId, TSName, TSGender, TSAddress, TSAge,
- private int _tSId;
-
- public int TSId
- {
- get { return _tSId; }
- set { _tSId = value; }
- }
- private string _tSName;
-
- public string TSName
- {
- get { return _tSName; }
- set { _tSName = value; }
- }
- private char _tSGender;
-
- public char TSGender
- {
- get { return _tSGender; }
- set { _tSGender = value; }
- }
- private string _tSAddress;
-
- public string TSAddress
- {
- get { return _tSAddress; }
- set { _tSAddress = value; }
- }
- private int _tSAge;
-
- public int TSAge
- {
- get { return _tSAge; }
- set { _tSAge = value; }
- }
- }
- }
(2)SQLHelper类
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Data.SqlClient;
- using System.Configuration;
- namespace _05事件传值
- {
- public class SqlHelper
- {
- private static readonly string str = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;
- //连接字符串
-
- public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
- {
- using (SqlConnection con=new SqlConnection(str))
- {
- using (SqlCommand cmd=new SqlCommand(sql,con))
- {
- con.Open();
- cmd.Parameters.AddRange(ps);
- return cmd.ExecuteNonQuery();
- }
- }
- }
- public static object ExecuteScalar(string sql, params SqlParameter[] ps)
- {
- using (SqlConnection con = new SqlConnection(str))
- {
- using (SqlCommand cmd = new SqlCommand(sql, con))
- {
- con.Open();
- cmd.Parameters.AddRange(ps);
- return cmd.ExecuteScalar();
- }
- }
- }
- public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
- {
- SqlConnection con = new SqlConnection(str);
- using (SqlCommand cmd=new SqlCommand(sql,con))
- {
- cmd.Parameters.AddRange(ps);
- try
- {
- con.Open();
- return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
- }
- catch (Exception ex)
- {
- con.Close();
- con.Dispose();
- throw ex;
- }
- }
- }
- }
- }
(3)新建两个窗体Form1和StudentAddAndUpdate
(4)创建个事件参数类MyEventArgs继承至EventArgs,用于事件传值
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
-
- namespace _05事件传值
- {
- public class MyEventArgs:EventArgs
- {
- /// <summary>
- /// 标识
- /// </summary>
- public int Temp { get; set; }
-
- /// <summary>
- /// 传对象
- /// </summary>
- public object Obj { get; set; }
- }
- }
(5)Form1窗体代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace _05事件传值
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- LoadStudent();
- }
-
- private void LoadStudent()
- {
-
- List<Student> list = new List<Student>();
- string sql = "select tsid,tsname,tsgender,tsaddress,tsage from student";
- using (SqlDataReader reader=SqlHelper.ExecuteReader(sql))
- {
- if (reader.HasRows)
- {
- while (reader.Read())
- {
- Student stu = new Student();
- stu.TSAddress = reader["TSAddress"].ToString();
- stu.TSAge = Convert.ToInt32(reader["TSAge"]);
- stu.TSId = Convert.ToInt32(reader["TSId"]);
- stu.TSName = reader["TSName"].ToString();
- stu.TSGender = Convert.ToBoolean(reader["TSGender"])?'男':'女';
- list.Add(stu);
- }
- }
- }
- dgvStudent.AutoGenerateColumns = false;
- dgvStudent.DataSource = list;
- dgvStudent.SelectedRows[0].Selected = false;
- }
- //新增
- private void btnAdd_Click(object sender, EventArgs e)
- {
- ShowStudentAddAndUpdate(1);
- }
- //修改
- private void btnUpdate_Click(object sender, EventArgs e)
- {
- if (dgvStudent.SelectedRows.Count > 0)
- {
- Student stu = new Student();
- stu.TSAddress = dgvStudent.SelectedRows[0].Cells[4].Value.ToString();//地址
- stu.TSName = dgvStudent.SelectedRows[0].Cells[1].Value.ToString();//姓名
- stu.TSAge = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[3].Value);//年龄
- stu.TSGender = Convert.ToChar(dgvStudent.SelectedRows[0].Cells[2].Value);//性别
- stu.TSId = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[0].Value);
- mea.Obj = stu;//对象存起来了--传值
-
- ShowStudentAddAndUpdate(2);//修改
- }
- else
- {
- MessageBox.Show("如果要修改请选中修改的行");
- }
-
- }
- //1---新增,2=====修改
- public event EventHandler evt;
- MyEventArgs mea = new MyEventArgs();
- public void ShowStudentAddAndUpdate(int p)
- {
-
- StudentAddAndUpdate sau = new StudentAddAndUpdate();
- //把方法作为参数进行传递
- //注册事件
- this.evt+=new EventHandler(sau.SetText);//注册事件
- //传值
- mea.Temp = p;//标识存起来
- if (this.evt!=null)
- {
- this.evt(this, mea);
- //刷新
- sau.FormClosed += new FormClosedEventHandler(sau_FormClosed);
- sau.ShowDialog();
- }
-
-
- }
-
- void sau_FormClosed(object sender, FormClosedEventArgs e)
- {
- LoadStudent();
- }
- }
- }
(6)StudentAddAndUpdate窗体代码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
-
- namespace _05事件传值
- {
- public partial class StudentAddAndUpdate : Form
- {
- public StudentAddAndUpdate()
- {
- InitializeComponent();
- }
- private int TP { get; set; }
- public void SetText(object sender, EventArgs e)
- {
- MyEventArgs mea = e as MyEventArgs;
- this.TP = mea.Temp;//标识存起来
- //清空所有文本框的值
- foreach (Control item in this.Controls)
- {
- if (item is TextBox)
- {
- TextBox tb = item as TextBox;
- tb.Text = "";
- }
- }
-
- if (this.TP==1)//新增
- {
-
- }
- else if (this.TP==2)//修改
- {
- Student stu = mea.Obj as Student;
- txtAddress.Text = stu.TSAddress;//地址
- txtAge.Text = stu.TSAge.ToString();//年龄
- txtName.Text = stu.TSName;
- //性别
- rdoMan.Checked= stu.TSGender == '男' ? true : false;
- rdoWoman.Checked = stu.TSGender == '女' ? true : false;
- //id
- labId.Text = stu.TSId.ToString();
- }
- }
- //
- private void btnOk_Click(object sender, EventArgs e)
- {
- //是新增还是修改
- if (this.TP==1)//新增
- {
- //写sql语句 调用sqlhelper
- }
- else if (this.TP==2)//修改
- {
-
- }
- }
- }
- }
|