C#數(shù)據(jù)表格(DataGridView)控件的應(yīng)用案例
我們通過一個(gè)完整的實(shí)例來實(shí)現(xiàn)課程信息管理功能的操作,包括查詢、修改、刪除課程信息的操作。
為了簡化實(shí)例,添加課程信息的操作直接在 SQL Server 數(shù)據(jù)庫中完成。
下面分幾個(gè)步驟完成。
1) 創(chuàng)建課程信息表創(chuàng)建課程信息表的 SQL 語句如下。
use test; create table course ( id int primary key identity(1,1), name varchar(20), credit numeric(3,1), remark varchar(50) );
向表中添加數(shù)據(jù)的語句如下。
insert into course (name, credit, remark) values ('計(jì)算機(jī)基石 ' , 2, '無'); insert into course (name, credit, remark) values ('C# 程序開發(fā)', 2.5 , '機(jī)房授課'); insert into course (name, credit, remark) values ('數(shù)據(jù)庫原理',1,'無'); insert into course (name, credit, remark) values ('體育',1,'無'); insert into course (name, credit, remark) values ('職業(yè)素養(yǎng)培訓(xùn)',0.5,'無');
在 SQL Server 中執(zhí)行上述 SQL 語句即可完成課程信息表(course)的創(chuàng)建和數(shù)據(jù)的添加。
2) 課程信息管理界面的設(shè)計(jì)在課程信息管理界面中提供了 DataGridView 控件用于顯示課程信息,并提供了根據(jù)課程名稱查找課程信息、修改以及刪除的功能。
具體的界面設(shè)計(jì)如下圖所示。
3) 在加載窗體時(shí)顯示所有課程信息本例中使用編寫代碼的方式實(shí)現(xiàn) DataGridView 控件的數(shù)據(jù)綁定,并在窗體的加載事件中加入數(shù)據(jù)綁定的代碼。
由于查詢所有課程信息的代碼將在后面的修改和刪除功能中重復(fù)使用,所以單獨(dú)定義一個(gè)方法來實(shí)現(xiàn)查詢所有課程信息。代碼如下。
//窗體加載事件 private void DataGridViewForm_Load(object sender, EventArgs e) { //調(diào)用查詢?nèi)空n程的方法 QueryAllCourse(); } //查詢?nèi)空n程 private void QueryAllCourse() { //數(shù)據(jù)庫連接串 string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //創(chuàng)建SqlConnection的實(shí)例 SqlConnection conn = null; try { conn = new SqlConnection(connStr); //打開數(shù)據(jù)庫 conn.Open(); string sql = "select * from course"; //創(chuàng)建SqlDataAdapter類的對象 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //創(chuàng)建DataSet類的對象 DataSet ds = new DataSet(); //使用SqlDataAdapter對象sda將查新結(jié)果填充到DataSet對象ds中 sda.Fill(ds); //設(shè)置表格控件的DataSource屬性 dataGridView1.DataSource = ds.Tables[0]; //設(shè)置數(shù)據(jù)表格上顯示的列標(biāo)題 dataGridView1.Columns[0].HeaderText = "編號"; dataGridView1.Columns[1].HeaderText = "課程名稱"; dataGridView1.Columns[2].HeaderText = "學(xué)分"; dataGridView1.Columns[3].HeaderText = "備注"; //設(shè)置數(shù)據(jù)表格為只讀 dataGridView1.ReadOnly = true; //不允許添加行 dataGridView1.AllowUserToAddRows = false; //背景為白色 dataGridView1.BackgroundColor = Color.White; //只允許選中單行 dataGridView1.MultiSelect = false; //整行選中 dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; } catch (Exception ex) { MessageBox.Show("查詢錯(cuò)誤!" + ex.Message); } finally { if (conn != null) { //關(guān)閉數(shù)據(jù)庫連接 conn.Close(); } } }
運(yùn)行該窗體,效果如下圖所示。
4) 完成課程名稱的模糊查詢在“查詢”按鈕的單擊事件中加入根據(jù)課程名稱模糊查詢的代碼,具體如下。
//查詢按鈕單擊事件 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text != "") { //數(shù)據(jù)庫連接串 string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //創(chuàng)建SqlConnection的實(shí)例 SqlConnection conn = null; try { conn = new SqlConnection(connStr); //打開數(shù)據(jù)庫 conn.Open(); string sql = "select * from course where name like '%{0}%'"; //填充占位符 sql = string.Format(sql, textBox1.Text); //創(chuàng)建SqlDataAdapter類的對象 SqlDataAdapter sda = new SqlDataAdapter(sql, conn); //創(chuàng)建DataSet類的對象 DataSet ds = new DataSet(); //使用SqlDataAdapter對象sda將查新結(jié)果填充到DataSet對象ds中 sda.Fill(ds); //設(shè)置表格控件的DataSource屬性 dataGridView1.DataSource = ds.Tables[0]; } catch (Exception ex) { MessageBox.Show("出現(xiàn)錯(cuò)誤!" + ex.Message); } finally { if (conn != null) { //關(guān)閉數(shù)據(jù)庫連接 conn.Close(); } } } }
運(yùn)行該窗體,查詢效果如下圖所示。
從上面的運(yùn)行效果可以看出,在文本框中輸入“計(jì)算機(jī)”,則可以實(shí)現(xiàn)查詢所有課程 名稱中含有“計(jì)算機(jī)”字樣的課程信息。
5) 實(shí)現(xiàn)修改功能在 DataGridView 控件中選中一條課程信息,單擊“修改”按鈕,彈出修改課程信息界面并在該界面中顯示要修改的信息,修改界面的設(shè)計(jì)如下圖所示。
選中 DataGridView 控件的一條課程信息,單擊“修改”按鈕。
“修改”按鈕的單擊事件中的代碼如下。 //修改課程信息 private void button2_Click(object sender, EventArgs e) { //獲取DataGridView控件中的值 //獲取課程編號 string id = dataGridView1.SelectedRows[0].Cells[0].Value.ToString(); //獲取課程名稱 string name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); //獲取課程名稱 string credit = dataGridView1.SelectedRows[0].Cells[2].Value.ToString(); //獲取課程名稱 string remark = dataGridView1.SelectedRows[0].Cells[3].Value.ToString(); //創(chuàng)建updateForm類的對象,并將課程信息傳遞給修改界面 updateForm updateform = new updateForm(id, name, credit, remark); //彈出修改信息窗口 DialogResult dr = updateForm.ShowDialog(); //判斷是否單擊確定按鈕 if (dr == DialogResult.OK) { //調(diào)用查詢?nèi)空n程方法 QueryAllCourse(); } }
修改界面 (UpdateForm) 中的代碼如下。
//帶參數(shù)的構(gòu)造方法 public updateForm(string id,string name,string credit,string remark) { InitializeComponent(); textBox1.Text = id; textBox2.Text = name; textBox3.Text = credit; textBox4.Text = remark; } //確認(rèn)按鈕單擊事件 private void button1_Click(object sender, EventArgs e) { //數(shù)據(jù)庫連接串 string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //創(chuàng)建SqlConnection的實(shí)例 SqlConnection conn = null; try { conn = new SqlConnection(connStr); //打開數(shù)據(jù)庫 conn.Open(); string sql = "update course set name='{0}',credit='{1}',remark='{2}' where id='{3}'"; //填充占位符 sql = string.Format(sql, textBox2.Text, textBox3.Text, textBox4.Text, textBox1.Text); //創(chuàng)建SqlCommand類的對象 SqlCommand cmd = new SqlCommand(sql, conn); //執(zhí)行修改操作的SQL cmd.ExecuteNonQuery(); //彈出成功提示 MessageBox.Show("修改成功!"); //設(shè)置當(dāng)前窗體DislogResult結(jié)果為OK this.DialogResult = DialogResult.OK; //關(guān)閉窗體 this.Close(); } catch (Exception ex) { MessageBox.Show("修改失敗!" + ex.Message); } finally { if (conn != null) { //關(guān)閉數(shù)據(jù)庫連接 conn.Close(); } } } //取消按鈕單擊事件 private void button2_Click(object sender, EventArgs e) { //關(guān)閉窗體 this.Close(); }
修改操作的運(yùn)行效果如下圖所示。
6) 實(shí)現(xiàn)刪除功能為“刪除”按鈕添加單擊事件,將選中的課程信息刪除并刷新界面中查詢出來的數(shù)據(jù)。實(shí)現(xiàn)的代碼如下。
//刪除按鈕的單擊事件 private void button3_Click(object sender, EventArgs e) { //獲取DataGridView控件中選中行的編號列的值 int id = int.Parse(dataGridView1.SelectedRows[0].Cells[0].Value.ToString()); //數(shù)據(jù)庫連接串 string connStr = "Data Source=.;Initial Catalog=test;User ID=sa;Password=root"; //創(chuàng)建SqlConnection的實(shí)例 SqlConnection conn = null; try { conn = new SqlConnection(connStr); //打開數(shù)據(jù)庫 conn.Open(); string sql = "delect from course where id='{0}'"; //填充占位符 sql = string.Format(sql, id); //創(chuàng)建SqlCommand類的對象 SqlCommand cmd = new SqlCommand(sql, conn); //執(zhí)行SQL語句 cmd.ExecuteNonQuery(); //彈出消息提示刪除成功 MessageBox.Show("刪除成功!"); //調(diào)用查詢?nèi)康姆椒?,刷新DataGridView控件中的數(shù)據(jù) QueryAllCourse(); } catch (Exception ex) { MessageBox.Show("刪除失敗!" + ex.Message); } finally { if (conn != null) { //關(guān)閉數(shù)據(jù)庫連接 conn.Close(); } } }
刪除操作的運(yùn)行效果如下圖所示。
單擊刪除消息框中的“確定”按鈕,'即可刷新 DataGridView 控件中的數(shù)據(jù)。< 上一頁C# DataGridViewC# DataGridView應(yīng)用案例下一頁 >
到此這篇關(guān)于C#數(shù)據(jù)表格(DataGridView)控件的應(yīng)用案例的文章就介紹到這了,更多相關(guān)C# DataGridView應(yīng)用內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 使用鼠標(biāo)點(diǎn)擊對Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08C#對Xamarin框架進(jìn)行數(shù)據(jù)綁定
這篇文章介紹了C#對Xamarin框架進(jìn)行數(shù)據(jù)綁定,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01