乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      WinForm之通過事件傳值來完成添加修改窗體賦值

       北方的白樺林 2017-08-20

              廢話不多說,直接上實例,對學生表進行事件傳值來完成對添加修改窗體的賦值。

      (1)創(chuàng)建Student類

      [csharp] view plain copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.   
      6. namespace _05事件傳值  
      7. {  
      8.    public class Student  
      9.     {  
      10.         //TSId, TSName, TSGender, TSAddress, TSAge,   
      11.         private int _tSId;  
      12.   
      13.         public int TSId  
      14.         {  
      15.             get { return _tSId; }  
      16.             set { _tSId = value; }  
      17.         }  
      18.        private string _tSName;  
      19.   
      20.        public string TSName  
      21.        {  
      22.            get { return _tSName; }  
      23.            set { _tSName = value; }  
      24.        }  
      25.        private char _tSGender;  
      26.   
      27.        public char TSGender  
      28.        {  
      29.            get { return _tSGender; }  
      30.            set { _tSGender = value; }  
      31.        }  
      32.        private string _tSAddress;  
      33.   
      34.        public string TSAddress  
      35.        {  
      36.            get { return _tSAddress; }  
      37.            set { _tSAddress = value; }  
      38.        }  
      39.        private int _tSAge;  
      40.   
      41.        public int TSAge  
      42.        {  
      43.            get { return _tSAge; }  
      44.            set { _tSAge = value; }  
      45.        }  
      46.     }  
      47. }  

      (2)SQLHelper類

      [csharp] view plain copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5. using System.Data.SqlClient;  
      6. using System.Configuration;  
      7. namespace _05事件傳值  
      8. {  
      9.    public class SqlHelper  
      10.     {  
      11.        private static readonly string str = ConfigurationManager.ConnectionStrings["strCon"].ConnectionString;  
      12.        //連接字符串  
      13.   
      14.        public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)  
      15.        {  
      16.            using (SqlConnection con=new SqlConnection(str))  
      17.            {  
      18.                using (SqlCommand cmd=new SqlCommand(sql,con))  
      19.                {  
      20.                    con.Open();  
      21.                    cmd.Parameters.AddRange(ps);  
      22.                    return cmd.ExecuteNonQuery();  
      23.                }  
      24.            }  
      25.        }  
      26.        public static object ExecuteScalar(string sql, params SqlParameter[] ps)  
      27.        {  
      28.            using (SqlConnection con = new SqlConnection(str))  
      29.            {  
      30.                using (SqlCommand cmd = new SqlCommand(sql, con))  
      31.                {  
      32.                    con.Open();  
      33.                    cmd.Parameters.AddRange(ps);  
      34.                    return cmd.ExecuteScalar();  
      35.                }  
      36.            }  
      37.        }  
      38.        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)  
      39.        {  
      40.            SqlConnection con = new SqlConnection(str);  
      41.            using (SqlCommand cmd=new SqlCommand(sql,con))  
      42.            {  
      43.                cmd.Parameters.AddRange(ps);  
      44.                try  
      45.                {  
      46.                    con.Open();  
      47.                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);  
      48.                }  
      49.                catch (Exception ex)  
      50.                {  
      51.                    con.Close();  
      52.                    con.Dispose();  
      53.                    throw ex;  
      54.                }  
      55.            }  
      56.        }  
      57.     }  
      58. }  
      (3)新建兩個窗體Form1和StudentAddAndUpdate

      (4)創(chuàng)建個事件參數類MyEventArgs繼承至EventArgs,用于事件傳值

      [csharp] view plain copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.Linq;  
      4. using System.Text;  
      5.   
      6. namespace _05事件傳值  
      7. {  
      8.    public class MyEventArgs:EventArgs  
      9.     {  
      10.        /// <summary>  
      11.        /// 標識  
      12.        /// </summary>  
      13.        public int Temp { getset; }  
      14.   
      15.        /// <summary>  
      16.        /// 傳對象  
      17.        /// </summary>  
      18.        public object Obj { getset; }  
      19.     }  
      20. }  
      (5)Form1窗體代碼

      [csharp] view plain copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.ComponentModel;  
      4. using System.Data;  
      5. using System.Drawing;  
      6. using System.Linq;  
      7. using System.Text;  
      8. using System.Windows.Forms;  
      9. using System.Data.SqlClient;  
      10. namespace _05事件傳值  
      11. {  
      12.     public partial class Form1 : Form  
      13.     {  
      14.         public Form1()  
      15.         {  
      16.             InitializeComponent();  
      17.         }  
      18.   
      19.         private void Form1_Load(object sender, EventArgs e)  
      20.         {  
      21.             LoadStudent();  
      22.         }  
      23.   
      24.         private void LoadStudent()  
      25.         {  
      26.   
      27.             List<Student> list = new List<Student>();  
      28.             string sql = "select tsid,tsname,tsgender,tsaddress,tsage from student";  
      29.             using (SqlDataReader reader=SqlHelper.ExecuteReader(sql))  
      30.             {  
      31.                 if (reader.HasRows)  
      32.                 {  
      33.                     while (reader.Read())  
      34.                     {  
      35.                         Student stu = new Student();  
      36.                         stu.TSAddress = reader["TSAddress"].ToString();  
      37.                         stu.TSAge = Convert.ToInt32(reader["TSAge"]);  
      38.                         stu.TSId = Convert.ToInt32(reader["TSId"]);  
      39.                         stu.TSName = reader["TSName"].ToString();  
      40.                         stu.TSGender = Convert.ToBoolean(reader["TSGender"])?'男':'女';  
      41.                         list.Add(stu);  
      42.                     }  
      43.                 }  
      44.             }  
      45.             dgvStudent.AutoGenerateColumns = false;  
      46.             dgvStudent.DataSource = list;  
      47.             dgvStudent.SelectedRows[0].Selected = false;  
      48.         }  
      49.         //新增  
      50.         private void btnAdd_Click(object sender, EventArgs e)  
      51.         {  
      52.             ShowStudentAddAndUpdate(1);  
      53.         }  
      54.         //修改  
      55.         private void btnUpdate_Click(object sender, EventArgs e)  
      56.         {  
      57.             if (dgvStudent.SelectedRows.Count > 0)  
      58.             {  
      59.                 Student stu = new Student();  
      60.                 stu.TSAddress = dgvStudent.SelectedRows[0].Cells[4].Value.ToString();//地址  
      61.                 stu.TSName = dgvStudent.SelectedRows[0].Cells[1].Value.ToString();//姓名  
      62.                 stu.TSAge = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[3].Value);//年齡  
      63.                 stu.TSGender = Convert.ToChar(dgvStudent.SelectedRows[0].Cells[2].Value);//性別  
      64.                 stu.TSId = Convert.ToInt32(dgvStudent.SelectedRows[0].Cells[0].Value);  
      65.                 mea.Obj = stu;//對象存起來了--傳值  
      66.                   
      67.                 ShowStudentAddAndUpdate(2);//修改  
      68.             }  
      69.             else  
      70.             {  
      71.                 MessageBox.Show("如果要修改請選中修改的行");  
      72.             }  
      73.              
      74.         }  
      75.         //1---新增,2=====修改  
      76.         public event EventHandler evt;  
      77.         MyEventArgs mea = new MyEventArgs();  
      78.         public void ShowStudentAddAndUpdate(int p)  
      79.         {  
      80.             
      81.             StudentAddAndUpdate sau = new StudentAddAndUpdate();  
      82.            //把方法作為參數進行傳遞  
      83.             //注冊事件  
      84.             this.evt+=new EventHandler(sau.SetText);//注冊事件  
      85.             //傳值  
      86.             mea.Temp = p;//標識存起來  
      87.             if (this.evt!=null)  
      88.             {  
      89.                 this.evt(this, mea);  
      90.                 //刷新  
      91.                 sau.FormClosed += new FormClosedEventHandler(sau_FormClosed);  
      92.                 sau.ShowDialog();  
      93.             }  
      94.   
      95.              
      96.         }  
      97.   
      98.         void sau_FormClosed(object sender, FormClosedEventArgs e)  
      99.         {  
      100.             LoadStudent();  
      101.         }  
      102.     }  
      103. }  
      (6)StudentAddAndUpdate窗體代碼
      [csharp] view plain copy
      1. using System;  
      2. using System.Collections.Generic;  
      3. using System.ComponentModel;  
      4. using System.Data;  
      5. using System.Drawing;  
      6. using System.Linq;  
      7. using System.Text;  
      8. using System.Windows.Forms;  
      9.   
      10. namespace _05事件傳值  
      11. {  
      12.     public partial class StudentAddAndUpdate : Form  
      13.     {  
      14.         public StudentAddAndUpdate()  
      15.         {  
      16.             InitializeComponent();  
      17.         }  
      18.         private int TP { getset; }  
      19.         public void SetText(object sender, EventArgs e)  
      20.         {  
      21.             MyEventArgs mea = e as MyEventArgs;  
      22.             this.TP = mea.Temp;//標識存起來  
      23.             //清空所有文本框的值  
      24.             foreach (Control item in this.Controls)  
      25.             {  
      26.                 if (item is TextBox)  
      27.                 {  
      28.                     TextBox tb = item as TextBox;  
      29.                     tb.Text = "";  
      30.                 }  
      31.             }  
      32.   
      33.             if (this.TP==1)//新增  
      34.             {  
      35.                   
      36.             }  
      37.             else if (this.TP==2)//修改  
      38.             {  
      39.                 Student stu = mea.Obj as Student;  
      40.                 txtAddress.Text = stu.TSAddress;//地址  
      41.                 txtAge.Text = stu.TSAge.ToString();//年齡  
      42.                 txtName.Text = stu.TSName;  
      43.                 //性別  
      44.                 rdoMan.Checked= stu.TSGender == '男' ? true : false;  
      45.                 rdoWoman.Checked = stu.TSGender == '女' ? true : false;  
      46.                 //id   
      47.                 labId.Text = stu.TSId.ToString();  
      48.             }  
      49.         }  
      50.         //  
      51.         private void btnOk_Click(object sender, EventArgs e)  
      52.         {  
      53.             //是新增還是修改  
      54.             if (this.TP==1)//新增  
      55.             {  
      56.                 //寫sql語句  調用sqlhelper  
      57.             }  
      58.             else if (this.TP==2)//修改  
      59.             {  
      60.   
      61.             }  
      62.         }  
      63.     }  
      64. }  

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多