Jacklovely

导航

 

说明(2017-5-27 16:34:39):

1. 注意事项:

(0)设计窗体,添加一个dgv,添加5个列名,修改名字和绑定数据。

 

(1)添加引用,system.data.sqlclient

(2)新建student类,添加id,name,chinese,math,flag字段

(3)窗体代码:loadStudent(0)调用方法,加载学生信息,参数为0,就是flag的值,0未删,1删除

(4)新建loadStudent方法,传一个参数p。

(5)数据库操作:连接字符串,using三个方法(sqlconnection,sqlcommand,sqldatareader),在sqldatareader里,实例化一个student类st,每读取一行数据,存入st,外面新建一个list集合,类型是student,把每个st存入list集合。

(6)dgv(datagridview缩写),dgv.datasource=list,把list绑定到dgv表格中。

(7)还有一些其他禁止自动生成列,禁止默认选中,点击选中一行一些破事,无关紧要,主要是上面的,将数据库数据显示出来。

2. 运行结果:

 

数据库:

myclass

代码:

  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 
 11 namespace _08餐桌大项目
 12 {
 13     public partial class Form1 : Form
 14     {
 15         public Form1()
 16         {
 17             InitializeComponent();
 18         }
 19         /// <summary>
 20         /// 学生类
 21         /// </summary>
 22         public class Student
 23         {
 24             private int _id;
 25             public int Id
 26             {
 27                 get { return _id; }
 28                 set { _id = value; }
 29             }
 30             private string _name;
 31             public string Name
 32             {
 33                 get { return _name; }
 34                 set { _name = value; }
 35             }
 36             private string _chinese;
 37             public string Chinese
 38             {
 39                 get { return _chinese; }
 40                 set { _chinese = value; }
 41             }
 42             private string _math;
 43             public string Math
 44             {
 45                 get { return _math; }
 46                 set { _math = value; }
 47             }
 48             private int _flag;
 49             public int Flag
 50             {
 51                 get { return _flag; }
 52                 set { _flag = value; }
 53             }
 54         }
 55         /// <summary>
 56         /// 窗体加载
 57         /// </summary>
 58         /// <param name="sender"></param>
 59         /// <param name="e"></param>
 60         private void Form1_Load(object sender, EventArgs e)
 61         {
 62             loadStudent(0);
 63         }
 64         /// <summary>
 65         /// 加载学生信息
 66         /// </summary>
 67         /// <param name="p">flag=0未删除,flag=1删除</param>
 68         private void loadStudent(int p)
 69         {
 70             //集合,把reader到的数据添加到student各字段
 71             List<Student> list = new List<Student>(); 
 72             string str = "Data Source=.; Initial Catalog=jjwdb; Integrated Security=True";
 73             using (SqlConnection con = new SqlConnection(str))
 74             {
 75                 con.Open();
 76                 string sql = "select id,name,chinese,math,flag from myclass where flag=" + p;
 77                 //string sql = "insert into myclass(name,chinese,math) values('zhao','77','77')";
 78                 using (SqlCommand cmd = new SqlCommand(sql, con))
 79                 {
 80                     using (SqlDataReader reader = cmd.ExecuteReader())
 81                     {
 82                         if (reader.HasRows)
 83                         {
 84                             //Console.WriteLine(123);
 85                             while (reader.Read())
 86                             {
 87                                 Student st = new Student();
 88                                 st.Id = Convert.ToInt32(reader["id"]);
 89                                 st.Name = Convert.ToString(reader["name"]);
 90                                 st.Chinese = Convert.ToString(reader["chinese"]);
 91                                 st.Math = Convert.ToString(reader["math"]);
 92                                 st.Flag = Convert.ToInt32(reader["flag"]);
 93                                 list.Add(st);
 94                             }
 95                         }
 96                     }
 97                 }
 98                 
 99             }
100             dgv.AutoGenerateColumns = false;//禁止自动生成列
101             dgv.DataSource = list;//绑定数据
102             dgv.SelectedRows[0].Selected = false;//禁止被选中,这一行代码要放在绑定数据后面,不然会报错!;属性里selection mode改为full row select,一次选一行
103         }
104     }
105 }

 

posted on 2017-05-27 17:11  Jacklovely  阅读(610)  评论(3编辑  收藏  举报