思维导图
1、分页
初始化设置:设置每页大小;设置当前页号为1;
载入数据表:将SQL命令连接属性指向SQL连接,SQL命令查询表的内容,使用数据适配器将查询命令属性指向SQL命令,声明并实例化数据表用以保存数据,以用作数据网格视图的数据源,用数据适配器填充数据表;指定SQL命令的命令文本;该命令查询所有学生成绩,但只返回空表,即只获取架构;声明并实例化SQL数据适配器;SQL数据适配器读取数据,并只将架构填充至医生信息数据表;声明并实例化数据列,用于保存行编号;设置数据列的名称、类型,类型需借助typeof获取;设置数据列为自增列;自增种子为1,自增步长也为1;
this.PageSize = 10; //设置每页大小为10(行记录); this.CurrentPageNo = 1; //设置当前页号为1; SqlConnection sqlConnection = new SqlConnection(); //声明并实例化SQL连接; sqlConnection.ConnectionString = "Server=(local);Database=医院门诊预约管理系统;Integrated Security=sspi"; //在字符串变量中,描述连接字符串所需的服务器地址、数据库名称、集成安全性(即是否使用Windows验证); SqlCommand sqlCommand = new SqlCommand(); //声明并实例化SQL命令; SqlCommand sqlCommand1 = new SqlCommand(); SqlCommand sqlCommand2 = new SqlCommand(); //声明并实例化SQL命令; sqlCommand.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand1.Connection = sqlConnection; sqlCommand2.Connection = sqlConnection; //将SQL命令的连接属性指向SQL连接; sqlCommand.CommandText = "SELECT * FROM tb_Department;"; //指定SQL命令的命令文本;该命令查询所有班级,以用作下拉框数据源; sqlCommand1.CommandText = "SELECT * FROM tb_doctorTitle"; sqlCommand2.CommandText = "SELECT No_hospital,No_doctor,Name_doctor,BirthDate_doctor,employDate,Graduate_doctor,Remain_doctor,Department_No,TitleNo,Gender_doctor FROM tb_doctor WHERE 1=0;"; //指定SQL命令的命令文本;该命令查询所有学生; SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(); //声明并实例化SQL数据适配器; sqlDataAdapter.SelectCommand = sqlCommand; //将SQL数据适配器的查询命令属性指向SQL命令; DataTable DepartmentTable = new DataTable(); //声明并实例化数据表,用于保存所有班级,以用作下拉框数据源; SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(); //声明并实例化SQL数据适配器; sqlDataAdapter1.SelectCommand = sqlCommand1; //将SQL数据适配器的查询命令属性指向SQL命令; DataTable TitleTable = new DataTable(); SqlDataAdapter sqlDataAdapter2 = new SqlDataAdapter(); //声明并实例化SQL数据适配器; sqlDataAdapter2.SelectCommand = sqlCommand2; //将SQL数据适配器的查询命令属性指向SQL命令; sqlDataAdapter2.MissingSchemaAction = MissingSchemaAction.AddWithKey; this.DoctorTable = new DataTable(); //声明并实例化数据表,用于保存所有学生,以用作数据网格视图的数据源; sqlConnection.Open(); //打开SQL连接; sqlDataAdapter.Fill(DepartmentTable); //SQL数据适配器读取数据,并填充班级数据表; sqlDataAdapter1.Fill(TitleTable); sqlDataAdapter2.Fill(this.DoctorTable); //SQL数据适配器读取数据,并填充学生数据表; DataColumn rowIdColumn = new DataColumn(); rowIdColumn.ColumnName = "RowID"; //设置数据列的名称; rowIdColumn.DataType = typeof(int); //设置数据列的类型;类型需借助typeof获取; rowIdColumn.AutoIncrement = true; //设置数据列为自增列; rowIdColumn.AutoIncrementSeed = 1; //设置数据列的自增种子为1; rowIdColumn.AutoIncrementStep = 1; //设置数据列的自增步长为1; DoctorTable.Columns.Add(rowIdColumn); sqlCommand2.CommandText = "SELECT No_hospital,No_doctor,Name_doctor,BirthDate_doctor,employDate,Graduate_doctor,Remain_doctor,Department_No,TitleNo, Gender_doctor FROM tb_doctor"; sqlDataAdapter2.Fill(this.DoctorTable); sqlConnection.Close(); //关闭SQL连接; this.MaxPageNo = DoctorTable.Rows.Count / this.PageSize + 1; //根据课程数据表的行集合的计数,计算最大页号; this.CurrentPageView = new DataView(); //实例化本窗体的课程数据视图,用于筛选当前页的记录; this.CurrentPageView.Table = this.DoctorTable ; //设置课程数据视图对应的数据表; this.CurrentPageView.Sort = "RowID ASC"; //设置课程数据视图的排序条件,即行编号; this.CurrentPageView.RowFilter = //设置课程数据视图的行筛选条件,即筛选当前页的记录; "RowID >" + (this.CurrentPageNo - 1) * this.PageSize + " AND RowID <=" + this.CurrentPageNo * this.PageSize; //根据当前页号、每页大小,计算相应的行编号范围,并作为行筛选条件; this.dgv_doctor .Columns.Clear(); //数据网格视图的列集合清空; this.dgv_doctor .DataSource = DoctorTable; //将数据网格视图的数据源设为学生数据表; this.dgv_doctor.Columns["No_hospital"].HeaderText = "医院编号"; //将数据网格视图的指定列的表头文本设为中文; this.dgv_doctor.Columns["No_doctor"].HeaderText = "医生编号"; this.dgv_doctor.Columns["Name_doctor"].HeaderText = "姓名"; this.dgv_doctor.Columns["BirthDate_doctor"].HeaderText = "生日"; this.dgv_doctor.Columns["employDate"].HeaderText = "就职日期"; this.dgv_doctor.Columns["Gender_doctor"].HeaderText = "性别"; this.dgv_doctor.Columns["Graduate_doctor"].HeaderText = "毕业院校"; this.dgv_doctor.Columns["Remain_doctor"].HeaderText = "可预约数"; this.dgv_doctor.Columns["Department_No"].Visible = false; //将数据网格视图的指定列设为不可见; this.dgv_doctor.Columns["TitleNo"].Visible = false; this.dgv_doctor.Columns["RowID"].Visible = false; this.dgv_doctor.Columns[this.dgv_doctor.Columns.Count - 1].AutoSizeMode = //数据网格视图的最后一列的自动调整列宽模式设为填充(至数据网格视图右侧边缘); DataGridViewAutoSizeColumnMode.Fill; DataGridViewComboBoxColumn DepartmentColumn = new DataGridViewComboBoxColumn(); //声明并实例化数据网格视图下拉框列,用于设置学生的班级; DepartmentColumn.Name = "Department"; //设置下拉框列的名称; DepartmentColumn.HeaderText = "科室"; //设置下拉框列的表头文本; DepartmentColumn.DataSource = DepartmentTable; //设置下拉框列的数据源为班级数据表; DepartmentColumn.DisplayMember = "Name"; //设置下拉框列的显示成员为(班级数据表的)名称(列); DepartmentColumn.ValueMember = "No"; //设置下拉框列的值成员为(班级数据表的)编号(列); DepartmentColumn.DataPropertyName = "Department_No"; //设置下拉框列的数据属性名称为(学生数据表的)班级编号(列); DepartmentColumn.DisplayIndex = 4; //设置下拉框列的显示顺序; DepartmentColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //设置下拉框列的自动调整列宽模式为填充; this.dgv_doctor.Columns.Add(DepartmentColumn); DataGridViewComboBoxColumn TitleColumn = new DataGridViewComboBoxColumn(); //声明并实例化数据网格视图下拉框列,用于设置学生的班级; TitleColumn.Name = "Title"; //设置下拉框列的名称; TitleColumn.HeaderText = "职称"; //设置下拉框列的表头文本; TitleColumn.DataSource = TitleTable; //设置下拉框列的数据源为班级数据表; TitleColumn.DisplayMember = "Title"; //设置下拉框列的显示成员为(班级数据表的)名称(列); TitleColumn.ValueMember = "No_doctorTitle"; //设置下拉框列的值成员为(班级数据表的)编号(列); TitleColumn.DataPropertyName = "Title_No"; //设置下拉框列的数据属性名称为(学生数据表的)班级编号(列); TitleColumn.DisplayIndex = 5; //设置下拉框列的显示顺序; TitleColumn.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill; //设置下拉框列的自动调整列宽模式为填充; this.dgv_doctor.Columns.Add(TitleColumn); }
“上一页”:若当前页号大于1;则当前页号递减;设置医生信息视图的行筛选条件,即筛选当前页的记录;
if (this.CurrentPageNo > 1) //若当前页号大于1; { this.CurrentPageNo--; //则当前页号递减; } this.CurrentPageView.RowFilter = //设置课程数据视图的行筛选条件,即筛选当前页的记录; "RowID >" + (this.CurrentPageNo - 1) * this.PageSize + " AND RowID <=" + this.CurrentPageNo * this.PageSize; //根据当前页号、每页大小,计算相应的行编
“下一页”:若当前页号尚未超出最大页号;则当前页号递增;设置医生信息视图的行筛选条件,即筛选当前页的记录;根据当前页号、每页大小,计算相应的行编号范围,并作为行筛选条件;
if (this.CurrentPageNo < this.MaxPageNo) //若当前页号尚未超出最大页号; { this.CurrentPageNo++; //则当前页号递增; } this.CurrentPageView.RowFilter = //设置课程数据视图的行筛选条件,即筛选当前页的记录; "RowID >" + (this.CurrentPageNo - 1) * this.PageSize + " AND RowID <=" + this.CurrentPageNo * this.PageSize; //根据当前页号、每页大小,计算相应的行编
示例: