实例介绍
【实例简介】SQLite 轻型数据库学习
【实例截图】
【核心代码】
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace 学生成绩管理 { public partial class MainForm : Form { #region 公用成员变量 studinfo stud_info = new studinfo(); private string commandText = ""; private static string DBName = "jianxuxie.db"; private static string datasource = System.AppDomain.CurrentDomain.BaseDirectory DBName; private static string connectionString = @"Data Source=" datasource; DBHelper dbHelper = new DBHelper(connectionString); #endregion public MainForm() { InitializeComponent(); this.InitializeDataBase(); this.LoadTable(dataGridView1, "table_stud_info"); } #region 自定义函数 private void InitializeDataBase() { //创建数据库 dbHelper.CreateNewDatabase(DBName); // 数据库中创建表 commandText = "create table if not exists table_stud_info(学号 varchar(10) primary key,姓名 varchar(6),高数 float,物理 float,C语言 float,外语 float,德育 float,平均 float,及格 varchar(1))"; dbHelper.CreateTable(commandText); } private string Get_Column_Max_add_one(string ColumnName) { int k = 0; int j = 0; string result = ""; for (int i = 0; i < dataGridView1.Rows.Count; i ) { if (i == 0) k = Convert.ToInt32(dataGridView1.Rows[i].Cells[ColumnName].Value); if (k < Convert.ToInt32(dataGridView1.Rows[i].Cells[ColumnName].Value)) { k = Convert.ToInt32(dataGridView1.Rows[i].Cells[ColumnName].Value); j = i; } } if (j == 0) { result = "1"; } else { result = (Convert.ToInt32(dataGridView1.Rows[j].Cells[ColumnName].Value) 1).ToString(); for (int i = result.Length; i < dataGridView1.Rows[j].Cells[ColumnName].Value.ToString().Length; i ) result = "0" result; } return result; } private void Delete_Row() { DialogResult dr = MessageBox.Show("确定要删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dr == DialogResult.OK) { commandText = "DELETE FROM table_stud_info WHERE 学号 = '" tb_del_xuehao.Text.Trim() "'"; dbHelper.DeleteFromTable(commandText); } else return; this.LoadTable(dataGridView1, "table_stud_info"); } private void set_tabpage_from_datagridview() { if (dataGridView1.CurrentCellAddress.X >= 0 && dataGridView1.CurrentCellAddress.Y >= 0) { int i = dataGridView1.CurrentRow.Index; stud_info.xuehao = dataGridView1.Rows[i].Cells[0].Value.ToString(); stud_info.name = dataGridView1.Rows[i].Cells[1].Value.ToString(); stud_info.gaoshu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[2].Value); stud_info.wuli = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[3].Value); stud_info.cyuyan = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[4].Value); stud_info.waiyu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[5].Value); stud_info.deyu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[6].Value); stud_info.pingjun = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells[7].Value); if (this.tabControl_caozuo.SelectedTab == tab_tianjia) { this.add_set_studinfo_to_tb(); this.btn_add_sure.Focus(); } else if (this.tabControl_caozuo.SelectedTab == tab_shanchu) { this.del_set_studinfo_to_tb(); this.btn_del_del.Focus(); } else if (this.tabControl_caozuo.SelectedTab == tab_xiugai) { this.edit_set_studinfo_to_tb(); this.btn_edit_sure.Focus(); } else if (this.tabControl_caozuo.SelectedTab == tab_chaxun) { this.query_set_studinfo_to_tb(); this.btn_query_sure.Focus(); } } } private bool check_exist_or_not(string ColumnName, string Target_String) { for (int i = 0; i < dataGridView1.Rows.Count; i ) { if (dataGridView1.Rows[i].Cells[ColumnName].Value.ToString() == Target_String || Convert.ToInt32(dataGridView1.Rows[i].Cells[ColumnName].Value) == Convert.ToInt32(Target_String)) return true; } return false; } private bool query_from_column(string Column_Name, string match_text) { int i = 0; for (i = 0; i < dataGridView1.RowCount; i ) { if (dataGridView1.Rows[i].Cells[Column_Name].Value.ToString() == match_text) { stud_info.xuehao = dataGridView1.Rows[i].Cells["学号"].Value.ToString(); stud_info.name = dataGridView1.Rows[i].Cells["姓名"].Value.ToString(); stud_info.gaoshu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["高数"].Value); stud_info.wuli = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["物理"].Value); stud_info.cyuyan = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["C语言"].Value); stud_info.waiyu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["外语"].Value); stud_info.deyu = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["德育"].Value); stud_info.pingjun = (float)Convert.ToDouble(dataGridView1.Rows[i].Cells["平均"].Value); break; } } if (i == dataGridView1.RowCount) { return false; } else { dataGridView1.CurrentCell.Selected = false;//取消当前单元格的选中状态 dataGridView1.Rows[i].Selected = true; //设置某行为选中状态 dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];//重设当前单元格 dataGridView1.FirstDisplayedScrollingRowIndex = i; //设置在当前区域的第一行显示 return true; } } #endregion #region 其他事件函数 private void dataGridView1_CurrentCellChanged(object sender, EventArgs e) { this.set_tabpage_from_datagridview(); } private void tabControl_caozuo_SelectedIndexChanged(object sender, EventArgs e) { this.set_tabpage_from_datagridview(); } #endregion #region 加载DataGridView private void LoadTable(DataGridView DGV, string TableName) { commandText = @"SELECT * FROM " TableName " order by 学号 asc"; //asc 升序排列,可省略,desc降序排列,不可省略 if (dbHelper.Load(commandText) == true) { // Show the data in the datagridview DGV.DataSource = dbHelper.DataSet.Tables[0]; //要在datagridview的属性中设置RowHeadersVisible = true,否则达不到理想效果 FillDatagridview(DGV); } } //既填满datagridview的宽度,又自适应单元格的内容宽度 //DataGridViewAutoSizeColumnsMode.Fill && DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader private void FillDatagridview(DataGridView DGV) { int ScrollBars_Width = 10; int[] AllCells_ColumnsWidth = new int[DGV.Columns.Count]; int[] Fill_ColumnsWidth = new int[DGV.Columns.Count]; int[] Final_ColumnsWidth = new int[DGV.Columns.Count]; int j = DGV.Columns.Count; int width_left = DGV.Width; for (int i = 0; i < DGV.Columns.Count; i ) { //设置第i列的宽度为 自适应内容的宽度 DGV.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.AllCellsExceptHeader); //获取第i列的列宽 AllCells_ColumnsWidth[i] = DGV.Columns[i].Width; } for (int i = 0; i < DGV.Columns.Count; i ) { Fill_ColumnsWidth[i] = DGV.Size.Width / DGV.Columns.Count; //自适应宽度和平均列宽比较 if (AllCells_ColumnsWidth[i] >= Fill_ColumnsWidth[i]) { Final_ColumnsWidth[i] = AllCells_ColumnsWidth[i]; width_left -= AllCells_ColumnsWidth[i]; j--; } else { Final_ColumnsWidth[i] = 0; } } for (int i = 0; i < DGV.Columns.Count; i ) { if (Final_ColumnsWidth[i] == 0) { if (DGV.ColumnHeadersHeight DGV.Rows.Count * dataGridView1.RowTemplate.Height > DGV.Height) { Final_ColumnsWidth[i] = (width_left - ScrollBars_Width - DGV.RowHeadersWidth) / j; } else { Final_ColumnsWidth[i] = (width_left - DGV.RowHeadersWidth) / j; } } DGV.Columns[i].Width = Final_ColumnsWidth[i]; } /* if (width < DGV.Size.Width) { DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; } else { for (int i = 0; i < DGV.Columns.Count; i ) { //在填充的模式下,自动调整第i列的列宽 DGV.AutoResizeColumn(i, DataGridViewAutoSizeColumnMode.Fill); //记录在填充的模式下的每列宽度和总宽度 Fill_ColumnsWidth[i] = DGV.Columns[i].Width; if (AllCells_ColumnsWidth[i] > Fill_ColumnsWidth[i]) { Fanal_ColumnsWidth[i] = AllCells_ColumnsWidth[i]; j = DGV.Columns.Count - 1; width_left -= AllCells_ColumnsWidth[i]; if (width_left < 0) { DGV.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; break; } } } }*/ } #endregion #region 输入合法性判断 private bool add_judge_legality() { try { this.add_get_studinfo_from_tb(); if (check_exist_or_not("学号", stud_info.xuehao)) { MessageBox.Show("此学号已存在,请重新输入学号!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } if (tb_add_xuehao.Text.Trim().Length == 0) { MessageBox.Show("学号输入不能为空,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else { for (int i = 0; i < tb_add_xuehao.Text.Trim().Length; i ) { if (!char.IsNumber(tb_add_xuehao.Text.Trim()[i])) { MessageBox.Show("学号格式输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } } if (tb_add_name.Text.Trim().Length == 0) { MessageBox.Show("姓名不能为空,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_add_gaoshu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_add_gaoshu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_add_gaoshu.Text.Trim()) < 0) { MessageBox.Show("高数成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_add_wuli.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_add_wuli.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_add_wuli.Text.Trim()) < 0) { MessageBox.Show("物理成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_add_waiyu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_add_waiyu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_add_waiyu.Text.Trim()) < 0) { MessageBox.Show("外语成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_add_cyuyan.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_add_cyuyan.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_add_cyuyan.Text.Trim()) < 0) { MessageBox.Show("C语言成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_add_deyu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_add_deyu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_add_deyu.Text.Trim()) < 0) { MessageBox.Show("德育成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else { return true; } } catch (Exception e) { MessageBox.Show("您的输入有误,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } //依据任意其中一个条件进行查找后删除 private bool del_judge_legality() { if (tb_del_xuehao.Text.Trim().Length == 0) { MessageBox.Show("学号输入不能为空,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else { for (int i = 0; i < tb_del_xuehao.Text.Trim().Length; i ) { if (!char.IsNumber(tb_del_xuehao.Text.Trim()[i])) { MessageBox.Show("学号格式输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } return true; } } private bool edit_judge_legality() { if (tb_edit_xuehao.Text.Trim().Length == 0) { MessageBox.Show("学号输入不能为空,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else { for (int i = 0; i < tb_edit_xuehao.Text.Trim().Length; i ) { if (!char.IsNumber(tb_edit_xuehao.Text.Trim()[i])) { MessageBox.Show("学号格式输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } } } if (tb_edit_name.Text.Trim().Length == 0) { MessageBox.Show("姓名不能为空,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_edit_gaoshu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_edit_gaoshu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_edit_gaoshu.Text.Trim()) < 0) { MessageBox.Show("高数成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_edit_wuli.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_edit_wuli.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_edit_wuli.Text.Trim()) < 0) { MessageBox.Show("物理成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_edit_waiyu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_edit_waiyu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_edit_waiyu.Text.Trim()) < 0) { MessageBox.Show("外语成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_edit_cyuyan.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_edit_cyuyan.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_edit_cyuyan.Text.Trim()) < 0) { MessageBox.Show("C语言成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else if (tb_edit_deyu.Text.Trim().Length == 0 || (float)Convert.ToDouble(tb_edit_deyu.Text.Trim()) > 100 || (float)Convert.ToDouble(tb_edit_deyu.Text.Trim()) < 0) { MessageBox.Show("德育成绩输入不合法,请重新输入!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return false; } else { return true; } } #endregion #region 清空文本框 private void add_tb_setnull() { tb_add_xuehao.Text = null; tb_add_name.Text = null; tb_add_gaoshu.Text = null; tb_add_deyu.Text = null; tb_add_cyuyan.Text = null; tb_add_pingjun.Text = null; tb_add_waiyu.Text = null; tb_add_wuli.Text = null; } private void del_tb_setnull() { tb_del_xuehao.Text = null; tb_del_name.Text = null; tb_del_gaoshu.Text = null; tb_del_deyu.Text = null; tb_del_cyuyan.Text = null; tb_del_pingjun.Text = null; tb_del_waiyu.Text = null; tb_del_wuli.Text = null; } private void edit_tb_setnull() { tb_edit_xuehao.Text = null; tb_edit_name.Text = null; tb_edit_gaoshu.Text = null; tb_edit_deyu.Text = null; tb_edit_cyuyan.Text = null; tb_edit_pingjun.Text = null; tb_edit_waiyu.Text = null; tb_edit_wuli.Text = null; } private void query_tb_setnull() { tb_query_xuehao.Text = null; tb_query_name.Text = null; tb_query_gaoshu.Text = null; tb_query_deyu.Text = null; tb_query_cyuyan.Text = null; tb_query_pingjun.Text = null; tb_query_waiyu.Text = null; tb_query_wuli.Text = null; } #endregion #region 文本框输入输出 private void add_get_studinfo_from_tb() { stud_info.xuehao = tb_add_xuehao.Text.Trim(); stud_info.name = tb_add_name.Text.Trim(); stud_info.gaoshu = (float)Convert.ToDouble(tb_add_gaoshu.Text.Trim()); stud_info.wuli = (float)Convert.ToDouble(tb_add_wuli.Text.Trim()); stud_info.waiyu = (float)Convert.ToDouble(tb_add_waiyu.Text.Trim()); stud_info.cyuyan = (float)Convert.ToDouble(tb_add_cyuyan.Text.Trim()); stud_info.deyu = (float)Convert.ToDouble(tb_add_deyu.Text.Trim()); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_add_pingjun.Text = stud_info.pingjun.ToString(); stud_info.jige = (stud_info.pingjun >= 60) ? "是" : "否"; } private void add_set_studinfo_to_tb() { tb_add_xuehao.Text = stud_info.xuehao; tb_add_name.Text = stud_info.name; tb_add_gaoshu.Text = stud_info.gaoshu.ToString(); tb_add_wuli.Text = stud_info.wuli.ToString(); tb_add_cyuyan.Text = stud_info.cyuyan.ToString(); tb_add_waiyu.Text = stud_info.waiyu.ToString(); tb_add_deyu.Text = stud_info.deyu.ToString(); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_add_pingjun.Text = stud_info.pingjun.ToString(); } private void del_set_studinfo_to_tb() { tb_del_xuehao.Text = stud_info.xuehao; tb_del_name.Text = stud_info.name; tb_del_gaoshu.Text = stud_info.gaoshu.ToString(); tb_del_wuli.Text = stud_info.wuli.ToString(); tb_del_cyuyan.Text = stud_info.cyuyan.ToString(); tb_del_waiyu.Text = stud_info.waiyu.ToString(); tb_del_deyu.Text = stud_info.deyu.ToString(); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_del_pingjun.Text = stud_info.pingjun.ToString(); } private void edit_get_studinfo_from_tb() { stud_info.xuehao = tb_edit_xuehao.Text.Trim(); stud_info.name = tb_edit_name.Text.Trim(); stud_info.gaoshu = (float)Convert.ToDouble(tb_edit_gaoshu.Text.Trim()); stud_info.wuli = (float)Convert.ToDouble(tb_edit_wuli.Text.Trim()); stud_info.waiyu = (float)Convert.ToDouble(tb_edit_waiyu.Text.Trim()); stud_info.cyuyan = (float)Convert.ToDouble(tb_edit_cyuyan.Text.Trim()); stud_info.deyu = (float)Convert.ToDouble(tb_edit_deyu.Text.Trim()); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_edit_pingjun.Text = stud_info.pingjun.ToString(); stud_info.jige = (stud_info.pingjun >= 60) ? "是" : "否"; } private void edit_set_studinfo_to_tb() { tb_edit_xuehao.Text = stud_info.xuehao; tb_edit_name.Text = stud_info.name; tb_edit_gaoshu.Text = stud_info.gaoshu.ToString(); tb_edit_wuli.Text = stud_info.wuli.ToString(); tb_edit_cyuyan.Text = stud_info.cyuyan.ToString(); tb_edit_waiyu.Text = stud_info.waiyu.ToString(); tb_edit_deyu.Text = stud_info.deyu.ToString(); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_edit_pingjun.Text = stud_info.pingjun.ToString(); } private void query_set_studinfo_to_tb() { tb_query_xuehao.Text = stud_info.xuehao; tb_query_name.Text = stud_info.name; tb_query_gaoshu.Text = stud_info.gaoshu.ToString(); tb_query_wuli.Text = stud_info.wuli.ToString(); tb_query_cyuyan.Text = stud_info.cyuyan.ToString(); tb_query_waiyu.Text = stud_info.waiyu.ToString(); tb_query_deyu.Text = stud_info.deyu.ToString(); stud_info.pingjun = (stud_info.gaoshu stud_info.wuli stud_info.waiyu stud_info.cyuyan stud_info.deyu) / 5; tb_query_pingjun.Text = stud_info.pingjun.ToString(); } #endregion #region 工具栏按钮点击事件 private void tianjia_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_tianjia; } private void shanchu_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_shanchu; } private void xiugai_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_xiugai; } private void chaxun_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_chaxun; } #endregion #region 各选项卡中的按钮点击事件 private void btn_add_sure_Click(object sender, EventArgs e) { if (this.add_judge_legality()) { //下面添加数据库操作 // 插入数据 //string commandText = "INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1, value2, value3,...valueN)"; commandText = "INSERT INTO table_stud_info (学号,姓名,高数,物理,C语言,外语,德育,平均,及格) VALUES ('" stud_info.xuehao "','" stud_info.name "'," stud_info.gaoshu "," stud_info.wuli "," stud_info.cyuyan "," stud_info.waiyu "," stud_info.deyu "," stud_info.pingjun ",'" stud_info.jige "')"; dbHelper.InsertToTable(commandText); string text = stud_info.xuehao; this.LoadTable(dataGridView1, "table_stud_info"); for (int i = 0; i < dataGridView1.Rows.Count; i ) { if (dataGridView1.Rows[i].Cells["学号"].Value.ToString() == text) { //dataGridView1.CurrentCell.Selected = false;//取消当前单元格的选中状态 // dataGridView1.Rows[i].Selected = true; //设置某行为选中状态 dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0];//重设当前单元格 dataGridView1.CurrentRow.Selected = true; dataGridView1.FirstDisplayedScrollingRowIndex = i; //设置在当前区域的第一行显示 break; } } this.add_tb_setnull(); this.tb_add_xuehao.Text = Get_Column_Max_add_one("学号"); } } private void btn_add_qingkong_Click(object sender, EventArgs e) { this.add_tb_setnull(); this.tb_add_xuehao.Text = Get_Column_Max_add_one("学号"); } private void btn_del_del_Click(object sender, EventArgs e) { //从datagridview中获取数据并显示到文本框中 this.del_set_studinfo_to_tb(); this.Delete_Row(); } private void btn_del_find_Click(object sender, EventArgs e) { bool a = false; bool b = false; //按条件查找 if (tb_del_xuehao.Text == "" && tb_del_name.Text == "") { MessageBox.Show("请输入学号或姓名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (tb_del_xuehao.Text != "") { a = this.query_from_column("学号", tb_del_xuehao.Text); } else { b = this.query_from_column("姓名", tb_del_name.Text); } //是否找到 if (a == true || b == true) { this.del_set_studinfo_to_tb(); } else { MessageBox.Show("未找到此人信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void btn_edit_sure_Click(object sender, EventArgs e) { if (this.edit_judge_legality()) { int i = dataGridView1.CurrentRow.Index; //下面添加数据库操作 this.edit_get_studinfo_from_tb(); commandText = string.Format("UPDATE table_stud_info SET 姓名 = '{0}',高数 ={1},物理 ={2},C语言 ={3},外语 ={4},德育 ={5},平均 ={6},及格 ='{7}' WHERE 学号 = '{8}'", stud_info.name, stud_info.gaoshu, stud_info.wuli, stud_info.cyuyan, stud_info.waiyu, stud_info.deyu, stud_info.pingjun, stud_info.jige, stud_info.xuehao); dbHelper.UpdateTable(commandText); this.LoadTable(dataGridView1, "table_stud_info"); dataGridView1.CurrentCell = dataGridView1.Rows[i].Cells[0]; dataGridView1.Rows[i].Selected = true; //设置某行为选中状态 dataGridView1.FirstDisplayedScrollingRowIndex = i; //设置在当前区域的第一行显示 } } private void btn_edit_cancel_Click(object sender, EventArgs e) { //this.edit_tb_setnull(); this.edit_set_studinfo_to_tb(); } private void btn_query_sure_Click(object sender, EventArgs e) { bool a = false; bool b = false; //按条件查找 if (tb_query_xuehao.Text.Trim() == "" && tb_query_name.Text.Trim() == "") { MessageBox.Show("请输入学号或姓名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); return; } else if (tb_query_xuehao.Text.Trim() != "") { a = this.query_from_column("学号", tb_query_xuehao.Text.Trim()); } else { b = this.query_from_column("姓名", tb_query_name.Text.Trim()); } //是否找到 if (a == true || b == true) { this.query_set_studinfo_to_tb(); } else { MessageBox.Show("未找到此人信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } private void btn_query_qingkong_Click(object sender, EventArgs e) { this.query_tb_setnull(); } #endregion #region DataGridView的右键事件函数 private void 添加ToolStripMenuItem_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_tianjia; this.set_tabpage_from_datagridview(); } private void 修改ToolStripMenuItem_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_xiugai; this.set_tabpage_from_datagridview(); } private void 查询ToolStripMenuItem_Click(object sender, EventArgs e) { this.tabControl_caozuo.SelectedTab = tab_chaxun; this.set_tabpage_from_datagridview(); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) { this.del_set_studinfo_to_tb(); this.Delete_Row(); } #endregion } }
好例子网口号:伸出你的我的手 — 分享!
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)