?? courseinfolistform.cs
字號:
?using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MySchoolPrj.DAO;
using MySchoolPrj.Entity;
namespace MySchoolPrj.CourseInfo
{
public partial class CourseInfoListForm : Form
{
private course cour;
public course Cour
{
get { return cour; }
set { cour = value; }
}
public CourseInfoListForm()
{
InitializeComponent();
}
//連接數據庫獲取信息
public void getCourse()
{
try
{
//打開數據庫
DBHelper.con.Open();
//創建Command對象
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBHelper.con;
cmd.CommandType = CommandType.Text;
//讀取數據庫Sql語句
string sql = "select * from course";
cmd.CommandText = sql;
//數據填充
SqlDataAdapter dap = new SqlDataAdapter();
dap.SelectCommand = cmd;
DataTable dt = new DataTable();
dap.Fill(dt);
//釋放數據
//把dt數據放到DateSource中
dgvCourse.DataSource = dt;
}
catch (Exception ex)
{
//操作錯誤
MessageBox.Show(ex.Message);
}
finally
{
//關閉數據庫
DBHelper.con.Close();
}
}
//初始化DataGridView的顯示
public void initDg()
{ //如何選擇datGradeView單元格
//整行選擇
dgvCourse.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
//是否顯示行標題的列
dgvCourse.RowHeadersVisible = false;
//單元格只讀
dgvCourse.ReadOnly = true;
//不允許用戶增加行
dgvCourse.AllowUserToAddRows = false;
//不允許用戶選擇多行
dgvCourse.MultiSelect = false;
for (int i = 0; i < dgvCourse.Columns.Count; i++)
{
//排序時 不包含列表頭
dgvCourse.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
string headerText = dgvCourse.Columns[i].HeaderText;
switch (headerText)
{
case "courseId":
//把列標題命名為:課程編號
dgvCourse.Columns[i].HeaderText = "課程編號";
//列的寬度
dgvCourse.Columns[i].Width = 170;
break;
case "courseName":
// 把列標題命名為:課程名稱
dgvCourse.Columns[i].HeaderText = "課程名稱";
//列的寬度
dgvCourse.Columns[i].Width = 170;
break;
case "Memo":
//把列標題命名為:課程備注
dgvCourse.Columns[i].HeaderText = "課程備注";
//列的寬度
dgvCourse.Columns[i].Width = 170;
break;
}
}
}
//刪除信息
public void delCourse(string courseId)
{
try
{ //從數據庫中獲取數據
DBHelper.con.Open();
//創建Command對象
SqlCommand cmd = new SqlCommand();
cmd.Connection = DBHelper.con;
cmd.CommandType = CommandType.Text;
//刪除Sql語句
string sql = string.Format("delete from course where CourseId= '{0}'", courseId);
cmd.CommandText = sql;
cmd.ExecuteNonQuery();//執行命令
}
catch (Exception)
{
//錯誤操作
MessageBox.Show("該Id已被其他表引用,受主外鍵約束,無法刪除");
}
finally
{//關閉數據庫
DBHelper.con.Close();
}
}
//刪除按鈕事件
//提示是否刪除
private void btnCancel_Click(object sender, EventArgs e)
{
int curRow;
DialogResult res = MessageBox.Show("你確定刪除嗎?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
//選擇否時就返回
if (res == DialogResult.Cancel)
return;
//獲取選擇行的索引
if (dgvCourse.Rows.Count > 0)
{
curRow = dgvCourse.CurrentRow.Index;
}
else {
MessageBox.Show("沒有信息無法刪除!");
return;
}
DataTable dt = (DataTable)dgvCourse.DataSource;
string CourseId = dt.Rows[curRow]["CourseId"].ToString();
//調用刪除方法
delCourse(CourseId);
//刷新界面
getCourse();
}
//Load事件
private void CourseInfoListForm_Load(object sender, EventArgs e)
{
//從數據庫獲取數據
getCourse();
//初始化DataGridView的顯示
initDg();
}
//增加事件
private void btnAdd_Click(object sender, EventArgs e)
{
//創建CourseEditForm對象
CourseEditForm addcourseinfofrom = new CourseEditForm();
//調用窗體(模態)
addcourseinfofrom.ShowDialog();
//獲取當前的索引
if (dgvCourse.Rows.Count>0)
{
//當前選擇行的索引
int curRow = dgvCourse.CurrentRow.Index;
//增加的行顯示出來
dgvCourse.Rows[curRow].Selected = true;
}
//刷新界面
getCourse();
}
//退出事件
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
//修改事件
private void button1_Click(object sender, EventArgs e)
{
//創建 CourseEditForm窗體的對象
CourseEditForm addForm = new CourseEditForm();
//獲取當前單元格行的索引
int curRow = dgvCourse.CurrentRow.Index;
//從DataGradeView中的數據放到Dt中
DataTable dt = (DataTable)dgvCourse.DataSource;
//創建臨時對象
course temp = new course();
//把當前選擇的數據放到臨時temp中
temp.CourseId = dt.Rows[curRow]["courseId"].ToString();
temp.CourseName = dt.Rows[curRow]["courseName"].ToString();
temp.Memo = dt.Rows[curRow]["Memo"].ToString();
//從枚舉中選擇dsUpdate界面
addForm.State = State.dsUpdate;
//把數據放到Cour類中
addForm.Cour = temp;
//調用窗體
addForm.ShowDialog();
//刷新界面
getCourse();
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -