using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace 数据控件 { public class DataConnection { private static string connstr = "server=.; database=mydb; user=sa; pwd=ray;"; public static SqlConnection Conn { get { return new SqlConnection(connstr); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 数据控件 { public class Info { private string code; public string Code { get { return code; } set { code = value; } } private string name; public string Name { get { return name; } set { name = value; } } private bool sex; public bool Sex { get { return sex; } set { sex = value; } } private string nation; public string Nation { get { return nation; } set { nation = value; } } private DateTime birthday; public DateTime Birthday { get { return birthday; } set { birthday = value; } } public string SexName { get { return this.sex ? "男" : "女"; } } public string NationName { get { NationData data = new NationData(); return data.NationName(Nation); } } public string BirthdayStr { get { return this.Birthday.ToString("yyyy年MM月dd日"); } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 数据控件 { public class Nation { private string code; public string Code { get { return code; } set { code = value; } } private string name; public string Name { get { return name; } set { name = value; } } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace 数据控件 { public class InfoData { private SqlConnection _conn; private SqlCommand _cmd; private SqlDataReader _dr; public InfoData() { _conn = DataConnection.Conn; _cmd = _conn.CreateCommand(); } /// <summary> /// 查询Info表全部数据 /// </summary> /// <returns></returns> public List<Info> Select() { _cmd.CommandText = "select *from Info"; _conn.Open(); _dr = _cmd.ExecuteReader(); List<Info> list = new List<Info>(); if (_dr.HasRows) { while (_dr.Read()) { Info data = new Info(); data.Code = _dr[0].ToString(); data.Name = _dr[1].ToString(); data.Sex = Convert.ToBoolean(_dr[2]); data.Nation = _dr[3].ToString(); data.Birthday = Convert.ToDateTime(_dr[4]); list.Add(data); } } _conn.Close(); return list; } /// <summary> /// 删除Info表指定数据 /// </summary> /// <param name="code">代号</param> public void Delete(string code) { _cmd.CommandText = "delete from Info where Code=@code"; _cmd.Parameters.Clear(); _cmd.Parameters.AddWithValue("@code",code); _conn.Open(); _cmd.ExecuteNonQuery(); _conn.Close(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data.SqlClient; namespace 数据控件 { public class NationData { private SqlConnection _conn; private SqlCommand _cmd; private SqlDataReader _dr; public NationData() { _conn = DataConnection.Conn; _cmd = _conn.CreateCommand(); } public string NationName(string code) { _cmd.CommandText = "select Name from Nation where Code=@code"; _cmd.Parameters.AddWithValue("@code",code); _conn.Open(); _dr = _cmd.ExecuteReader(); if (_dr.HasRows) { _dr.Read(); return _dr[0].ToString(); } else { return null; } _conn.Close(); } } }
设计界面:
后台代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 数据控件 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { InfoData da=new InfoData(); //绑定数据源 dataGridView1.DataSource = da.Select(); //取消默认选中第一行 dataGridView1.ClearSelection(); } private void button1_Click(object sender, EventArgs e) { //取出选中项的值 if (dataGridView1.SelectedCells.Count > 0) { MessageBox.Show(dataGridView1.SelectedCells[0].Value.ToString()); } } private void button2_Click(object sender, EventArgs e) { //取出选中行绑定的对象 Info data = dataGridView1.SelectedRows[0].DataBoundItem as Info; MessageBox.Show(data.Code + " " + data.Name + " " + data.SexName + " " + data.NationName + " " + data.BirthdayStr); } private void button3_Click(object sender, EventArgs e) { //删除选中项 Info data = dataGridView1.SelectedRows[0].DataBoundItem as Info; InfoData da = new InfoData(); da.Delete(data.Code); //刷新数据 dataGridView1.DataSource = da.Select(); dataGridView1.ClearSelection(); } } }
运行界面:
获取选定项:
获取选定行绑定对象:
删除: 刷新: