任務(wù)4 聯(lián)系人分組管理_第1頁
任務(wù)4 聯(lián)系人分組管理_第2頁
任務(wù)4 聯(lián)系人分組管理_第3頁
任務(wù)4 聯(lián)系人分組管理_第4頁
任務(wù)4 聯(lián)系人分組管理_第5頁
已閱讀5頁,還剩30頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、1任務(wù)任務(wù)4 4 聯(lián)系人分組管理聯(lián)系人分組管理2Demo:實現(xiàn)聯(lián)系人分組管理實現(xiàn)聯(lián)系人分組管理本章任務(wù)3本章任務(wù)本章任務(wù)Activity:實現(xiàn)學(xué)生選課系統(tǒng):實現(xiàn)學(xué)生選課系統(tǒng)的用戶管理功能。的用戶管理功能。4聯(lián)系人分組管理界面聯(lián)系人分組管理界面設(shè)計聯(lián)系人分組管理界面5為了以列表形式顯示聯(lián)系人分組信息,我們在窗體上用了一個新的控件DataGridView,該控件位于工具箱的“數(shù)據(jù)”選項卡內(nèi)。設(shè)置DataGridView控件的Name屬性為dgvGroupList,并修改Columns屬性,添加3列,各列類型均為DataGridViewTextBoxColumn。DataGridView設(shè)置6Dat

2、aGridView設(shè)置DataGridView控件各列屬性設(shè)置 屬性設(shè)置屬性設(shè)置 列名稱列名稱屬性名稱屬性名稱設(shè)置結(jié)果設(shè)置結(jié)果說明說明Column1HeaderText編號列標(biāo)題文本DataPropertyNameId要綁定的數(shù)據(jù)列的名稱VisibleFalse指定列是否可見指定列是否可見Column2HeaderText分組名稱列標(biāo)題文本DataPropertyNameGroupName要綁定的數(shù)據(jù)列的名稱VisibleTrue指定列是否可見Column3HeaderText備注列標(biāo)題文本DataPropertyNameMemo要綁定的數(shù)據(jù)列的名稱VisibleTrue指定列是否可見窗體上各

3、按鈕屬性設(shè)置 屬性設(shè)置屬性設(shè)置 控件名稱控件名稱屬性屬性設(shè)置結(jié)果設(shè)置結(jié)果btnAddText增加btnDeleteText刪除btnModifyText修改btnCloseText關(guān)閉9/實現(xiàn)分組信息顯示(1)首先在代碼中添加對)首先在代碼中添加對System.Data.SqlClient命名空間的引用:命名空間的引用: using System.Data.SqlClient;(2)為窗體類定義如下的實例變量:)為窗體類定義如下的實例變量: SqlDataAdapter da; DataSet ds;代碼編寫10/實現(xiàn)分組信息顯示(3)編寫自定義方法)編寫自定義方法Fill(),向,向Data

4、GridView控件中填充數(shù)據(jù):控件中填充數(shù)據(jù):public void Fill() string sql = select Id,GroupName,Memo from ContactGroup order by Id desc; using (SqlConnection conn = new SqlConnection(DBHelper.connString) da = new SqlDataAdapter(sql, conn); ds = new DataSet(); da.Fill(ds); dgvGroupList.DataSource = ds.Tables0; (4)在窗體的)在

5、窗體的Load事件中調(diào)用事件中調(diào)用Fill方法:方法:private void FormGourpList_Load(object sender, EventArgs e) Fill(); 代碼編寫11運行程序,即可在窗體上顯示出分組信息 :代碼編寫12DataSet對象13創(chuàng)建DataSet對象DataSet ds = new DataSet();創(chuàng)建DataSet對象后可以臨時存儲數(shù)據(jù),那么如何將數(shù)據(jù)放到數(shù)據(jù)集中呢?這就需要用到數(shù)據(jù)適配器DataAdapter對象。 DataSet對象14(1)創(chuàng)建數(shù)據(jù)庫連接對象)創(chuàng)建數(shù)據(jù)庫連接對象SqlConnection conn = new SqlC

6、onnection(DBHelper.connString);(2)建立從數(shù)據(jù)庫查詢數(shù)據(jù)用的)建立從數(shù)據(jù)庫查詢數(shù)據(jù)用的SQL語句語句string sql = select Id,GroupName,Memo from ContactGroup;(3)通過上面創(chuàng)建的)通過上面創(chuàng)建的SQL語句和數(shù)據(jù)庫連接對象創(chuàng)建語句和數(shù)據(jù)庫連接對象創(chuàng)建SqlDataAdapte對象對象SqlDataAdapter da = new SqlDataAdapter(sql, conn);(4)調(diào)用)調(diào)用SqlDataAdapter對象的對象的Fill方法向數(shù)據(jù)集中填充數(shù)據(jù)方法向數(shù)據(jù)集中填充數(shù)據(jù)DataSet ds=

7、new DataSet();da.Fill(ds); SqlDataAdapter對象 15數(shù)據(jù)集中有了數(shù)據(jù)以后,我們只需設(shè)置數(shù)據(jù)集中有了數(shù)據(jù)以后,我們只需設(shè)置DataGridView控件控件的的DataSource屬性,就可以顯示數(shù)據(jù)了:屬性,就可以顯示數(shù)據(jù)了:dgvGroupList.DataSource = ds.Tables0;顯示DataSet中的數(shù)據(jù)16如果如果DataSet中已包含數(shù)據(jù),我們可以通過下面的代碼來訪中已包含數(shù)據(jù),我們可以通過下面的代碼來訪問第一個表中第問第一個表中第i行第行第j列的數(shù)據(jù)(索引均從列的數(shù)據(jù)(索引均從0開始):開始):ds.Tables0.Rowsi.I

8、temArrayj;還可以通過下面的代碼獲取數(shù)據(jù)表中記錄的還可以通過下面的代碼獲取數(shù)據(jù)表中記錄的行數(shù)行數(shù):ds.Tables0.Rows.Count;還可以通過下面的代碼獲取數(shù)據(jù)表中字段的還可以通過下面的代碼獲取數(shù)據(jù)表中字段的列數(shù)列數(shù):ds.Tables0. Columns.Count ;訪問DataSet中的數(shù)據(jù)17/結(jié)合使用結(jié)合使用SqlDataAdapter和和DataSet,我們也可以用下面的代碼來實現(xiàn)用戶登錄功能:,我們也可以用下面的代碼來實現(xiàn)用戶登錄功能:string connString = DBHelper.connString;string sqlStr = string.F

9、ormat(select * from User where UserName=0 a n d P a s s w o r d = 1 , t x t U s e r N a m e .Te x t .T r i m ( ) , txtUserPassword.Text.Trim();using (SqlConnection conn = new SqlConnection(connString) SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn); DataSet ds = new DataSet(); da.Fill(ds); if(d

10、s.Tables0.Rows.Count!=0) MessageBox.Show(登錄成功!登錄成功!); else MessageBox.Show(用戶名或密碼錯誤!用戶名或密碼錯誤!); 訪問DataSet中的數(shù)據(jù)18設(shè)計新增分組窗體 :實現(xiàn)聯(lián)系人分組新增功能窗體中各控件屬性設(shè)置 屬性設(shè)置屬性設(shè)置 控件類型控件類型控件名稱控件名稱屬性屬性設(shè)置結(jié)果設(shè)置結(jié)果LabellblGroupNameText分組名稱lblGroupMemoText備注TextBoxtxtGroupNametxtGroupMemoMultilineTrueButtonbtnSaveText保存btnCloseText關(guān)閉

11、20/判斷分組名稱是否重復(fù)判斷分組名稱是否重復(fù) bool CheckGroupName(string groupName) bool check = true; using (SqlConnection conn = new SqlConnection(DBHelper.connString) string sql = string.Format(select count(*) from ContactGroup where GroupName=0, groupName); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int

12、 n = Convert.ToInt32(cmd.ExecuteScalar(); if (n = 1) MessageBox.Show(分組名稱重復(fù),請修改!分組名稱重復(fù),請修改!); txtGroupName.Focus(); check = false; return check; 代碼編寫21/“保存保存”按鈕的單擊事件按鈕的單擊事件 private void btnSave_Click(object sender, EventArgs e) /獲取并驗證數(shù)據(jù)獲取并驗證數(shù)據(jù) /保存到數(shù)據(jù)庫中保存到數(shù)據(jù)庫中 using (SqlConnection conn = new SqlConne

13、ction(DBHelper.connString) string sql = string.Format(insert into ContactGroup values(0,1),groupName, memo); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int n = Convert.ToInt32(cmd.ExecuteNonQuery(); if (n != 1) MessageBox.Show(添加分組失?。√砑臃纸M失?。?; else MessageBox.Show(添加分組成功!添加分組成功!); 代碼編寫

14、22/“關(guān)閉關(guān)閉”按鈕的單擊事件按鈕的單擊事件 private void btnClose_Click(object sender, EventArgs e) this.Close();/窗體窗體FormGroupList上的上的“增加增加”按鈕編寫單擊事件按鈕編寫單擊事件 private void btnAdd_Click(object sender, EventArgs e) FormGroupAdd f = new FormGroupAdd(); f.ShowDialog(); Fill();代碼編寫23/如果分組下存在聯(lián)系人信息,不允許刪除!如果分組下存在聯(lián)系人信息,不允許刪除! pr

15、ivate void btnDelete_Click(object sender, EventArgs e) int iid = (int)dgvGroupList.CurrentRow.Cells0.Value; if (MessageBox.Show(確定要刪除嗎?確定要刪除嗎?, 詢問詢問, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes) return; using (SqlConnection conn = new SqlConnection(DBHelper.connString) strin

16、g sql = string.Format(select count(*) from Contact where GroupId=0, id); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int n = Convert.ToInt32(cmd.ExecuteScalar(); if (n = 1) MessageBox.Show(該分組下存在聯(lián)系人信息,不允許刪除!該分組下存在聯(lián)系人信息,不允許刪除!); return; 刪除分組24/刪除分組刪除分組using (SqlConnection conn = new SqlC

17、onnection(DBHelper.connString) string sql = string.Format(delete from ContactGroup where Id=0, id); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int n = Convert.ToInt32(cmd.ExecuteNonQuery(); if (n != 1) MessageBox.Show(刪除失敗!刪除失??!); else MessageBox.Show(刪除成功!刪除成功!); Fill(); 刪除分組25設(shè)計修改分組信

18、息窗體 :修改分組信息窗體中各控件屬性設(shè)置 控件類型控件類型控件名稱控件名稱屬性屬性設(shè)置結(jié)果設(shè)置結(jié)果LabellblIdText分組編號lblGroupNameText分組名稱lblGroupMemoText備注TextBoxtxtIdEnabledFalsetxtGroupNametxtGroupMemoMultilineTrueButtonbtnSaveText保存修改分組信息27(1)定義)定義FormGroupDetail窗體的私有字段窗體的私有字段int id;(2)給)給FormGroupDetail窗體增加一個構(gòu)造方法窗體增加一個構(gòu)造方法public FormGroupDetai

19、l(int id) this.id = id; InitializeComponent(); 修改分組信息28(3)補(bǔ)充)補(bǔ)充FormGroupList窗體中窗體中“修改修改”按鈕的單擊事件代碼按鈕的單擊事件代碼private void btnModify_Click(object sender, EventArgs e) int id = 0; try id = (int)dgvGroupList.CurrentRow.Cells0.Value; catch (System.Exception ex) MessageBox.Show(請選擇有效數(shù)據(jù)行!請選擇有效數(shù)據(jù)行!); return;

20、FormGroupDetail f = new FormGroupDetail(id); f.ShowDialog(); Fill(); 修改分組信息29/在在FormGroupDetail窗體的窗體的Load事件中根據(jù)編號查詢該分組信息事件中根據(jù)編號查詢該分組信息 private void FormGroupDetail_Load(object sender, EventArgs e) txtId.Text = id.ToString();/編號已通過重載的構(gòu)造方法傳入編號已通過重載的構(gòu)造方法傳入 string connString = DBHelper.connString; string

21、 sqlStr = string.Format(select * from ContactGroup where id=0, id); using (SqlConnection conn = new SqlConnection(connString) SqlCommand cmd = new SqlCommand(sqlStr, conn); conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); if (dr.Read() txtGroupName.Text = drGroupName.ToString(); txtGroupMemo.Tex

22、t = drMemo.ToString(); dr.Close(); 修改分組信息30經(jīng)過這幾步后,單擊FormGroupList窗體中“修改”按鈕,即可在FormGroupDetail窗體中顯示選中的分組信息,以供用戶查看、修改。 修改分組信息31/實現(xiàn)修改分組信息后的保存功能實現(xiàn)修改分組信息后的保存功能 private void btnSave_Click(object sender, EventArgs e) string groupName = txtGroupName.Text.Trim(); string memo = txtGroupMemo.Text.Trim(); using

23、 (SqlConnection conn = new SqlConnection(DBHelper.connString) string sql = string.Format(update ContactGroup set GroupName=0,Memo=1 where Id=2, groupName, memo,id); SqlCommand cmd = new SqlCommand(sql, conn); conn.Open(); int n = Convert.ToInt32(cmd.ExecuteNonQuery(); if (n != 1) MessageBox.Show(更新失敗!更新失??!); else MessageBox.Show(更新成功!更新成功!); 修改分組信息32/我們還可以編寫我們還可以編寫FormGroupList窗體中窗體中DataGridView控件的控件的CellDoubleClick事事件代碼,使得用戶在件代碼,使得用戶在DataGridView控件中雙擊某行,也能查看、修改分組信息:控件中雙擊某行,也能查看、修改分組信息: private void dgvGroupList_CellDoubleClick(object sender

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論