《Windows程序設計基礎-基于.NET平臺》課件-CORE-05_第1頁
《Windows程序設計基礎-基于.NET平臺》課件-CORE-05_第2頁
《Windows程序設計基礎-基于.NET平臺》課件-CORE-05_第3頁
《Windows程序設計基礎-基于.NET平臺》課件-CORE-05_第4頁
《Windows程序設計基礎-基于.NET平臺》課件-CORE-05_第5頁
已閱讀5頁,還剩25頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1使用DataGridView第五章2教學目標掌握DataGridView的使用。掌握DataGridView的數據處理。

3DataGridView:結構4DataGridView:結構由行和列的集合構成。Columns決定了顯示的方式。行和列決定了一個單元格(DataGridViewCell)。Columns(列集合)即:DataGridViewColumnCollection決定了DataGridView的顯示。列集合由DataGridViewColumn組成。5DataGridView:列類型DataGridViewTextBoxColumn用TextBox作為顯示/輸入工具的列使用該類型的列當用戶編輯數據時,將使用TextBox作為輸入空間DataGridViewCheckBoxColumn用CheckBox作為顯示/輸入工具的列DataGridViewImageColumn用于顯示圖像DataGridViewButtonColumn該列的每個單元格中顯示按鈕UseColumnTextFontButtonValue屬性設置為true,使每個單元格顯示相同的按鈕文本Text屬性是按鈕的文本單元格的Button點擊事件是通過DataGridView的CellContentClick事件響應的DataGridViewComboBoxColumn用于在單元格中顯示下拉列表,可以包含如同ComboBox控件一樣的選擇項DataGridViewLinkColumn用于在單元格中顯示連接6DataGridView:列類型例:Chapter05_017步驟:定義一個編輯控件,該控件要實現IDataGridViewEditingControl接口。從DataGridViewCell或者它的繼承類開始開始實現一個單元格,并明確指出寄宿編輯控件類型為(1)的類型,同時也指出容納的值類型為(1)中的值類型。從DataGridViewColumn或者任何它的繼承類開始定義新的列類型并重載CellTemplate屬性,指明要使用的單元格。DataGridView:擴展列類型例:Chapter05_02DataGridView:擴展列類型9格式化DataGridView基本格式對象:DataGridViewCellStyle表示控件中的各個單元格的格式設置和樣式信息頭格式列格式行格式格式事件10格式化DataGridView頭格式:行頭格式: ColumnHeaderDefaultCellStyle,ColumnHeader…等列頭格式: ColumnHeaderDefaultCellStyle,ColumnHeader…等11格式化DataGridView列格式:Column的DefaultCellStyle:設置單元格的默認單元格樣式。(背景)this.data1.DefaultCellStyle.BackColor=Color.black;DisplayStyle:獲取或設置一個值,該值確定當組合框處于非編輯模式時它的顯示方式。12格式化DataGridView行格式:RowsDefaultCellStyle:獲取或設置應用于DataGridView的行單元格的默認樣式。AlternatingRowsDefaultCellStyle:設置交替行的格式RowTemplate:獲取或設置一行,該行表示控件中所有行的模板。DataGridView中的列對象具有DefaultStyle等屬性,可以單獨對一個列指定格式格式化DataGridViewDataGridViewComboBoxColumnboxc=newDataGridViewComboBoxColumn();boxc.HeaderText=“國家”;…DataGridViewCellStylecellStyle=newDataGridViewCellStyle();cellStyle.BackColor=Color.FromKnowVolor(KnownColor.Aqua);cellStyle.ForeColor=Color.Red;cellStyle.Font=newFont(“Tahoma”,10);boxc.DefaultCellStyle=cellStyle;…14格式化DataGridView格式事件:如:

CellFormatting:單元格的內容被顯示前被調用,用于格式化單元格設置該事件的DataGridViewCellFormattingEventArgs參數的CellStyle進行格式化。privatevoiddataGridView1_CellFormatting(objectsender,DataGridViewCellFormattingEventArgse){if(e.Value!=null&&e.Value.ToString().IndexOf("China")>=0){e.CellStyle.ForeColor=Color.Blue;e.CellStyle.BackColor=Color.Red;e.CellStyle.Font=newFont("Tahoma",10,FontStyle.Bold);}}DataGridView其他的顯示格式的屬性:CellBorderStyle:是DataGridViewCellBorderStyle類型的枚舉值None:Raised:三維凸起邊框Single:單行邊框SingleHorizontal:水平單行邊框Sunken:三維凹陷邊框SunkenHorizontal:水平三維凹陷邊框GridColor:當CellBorderStyle是Single、SingleHorizontal、SingleVertical時設置網格線的顏色格式化DataGridView16DataGridView:顯示模式顯示模式:非綁定:適合于顯示數據量比較少的數據,必須手動填充該控件的行記錄通常使用DataGridView的Rows屬性的Add方法綁定:適合同外部數據進行交互通過設置DataGridView的DataSource屬性可將控件直接連接到數據源無需主動管理即可存入和提取數據行當AutoGenerateColumns屬性為true時,將在控件中為數據源中的第一列創建相對應的列虛模式(略,可不講解)可以實現自己的數據管理,在與大量數據交互時優化17DataGridView:顯示模式非綁定的數據處理:通過手動(or設計時)添加列定義顯示結構通過Rows的Add等類似方法添加記錄通過枚舉Cell集合即(枚舉Columns和Rows實現)讀取數據dataGridView1.ColumnCount=4;dataGridView1.Rows.Add(newobject[]{“Col1”,”Col2”,”Col3”,”Col4”});18DataGridView:顯示模式獲取非綁定電子表格的值主要是通過查詢每個單元格的Value屬性來實現的foreach(DataGridViewRowrowinthis.dataGridView1.Rows){ if(!row.IsNewRow) foreach(DataGridViewCellcellinrow.Cells) MessageBox.Show(cell.Value.ToString());}19(1)通過設置DataGridView的DataSource屬性,可將DataGridView控件直接連接到其數據源,此時,DataGridView自動構造相應的列類型,如想指定列類型,須設置AutoGenerateColumns屬性為falseDataGridView:綁定模式20(2)用戶在DataGridView上對數據的更改將反映到DataSourceDataGridView:綁定模式21(3)DataSource的值類型必須實現下列接口之一:IList接口,如ArrayList,數組等。IListSource接口,如,DataTable和DataSet類IBindingList接口,如,BindingList類。IBindingListView接口,如,BindingSource類。DataGridView:綁定模式22(4)DataSource綁定到BindingSource控件

(*BindingSource實現了IBindingListView接口,符合規則3)DataGridView:綁定模式DataGridView綁定到數據源時,必須將DataMember屬性設置為指定要綁定的列表或表的字符串DataGridView綁定到BindingSource時,可以不設置DataGridView的DataMember屬性,而設置BindingSource組件的DataMember屬性dataGridView.DataSource=dataSet;dataGridView.DataMember=tableName;bindingSource.DataSource=dataSet;bindingSource.DataMember=tableName;…dataGridView.DataSource=bindingSource;23使用綁定模式操作數據源顯示數據:指定列的創建方式:自動|Coding|設計狀態指定從數據源獲取數據,并置入符合綁定要求的數據類型將(2)獲取的對象指定給DataSource屬性。privateDataTableGetDataSource(){SqlConnectioncon=newSqlConnection();con.ConnectionString="DataSource=jy;InitialCatalog=peoples;IntegratedSecurity=True";DataSetds=newDataSet();SqlCommandcmd=newSqlCommand("select*fromemps",con);SqlDataAdapterda=newSqlDataAdapter(cmd);using(con){con.Open();da.Fill(ds);}if(ds.Tables.Count>0&&ds.Tables[0]!=null)returnds.Tables[0];elsereturnnull;}privatevoidBing(){dataGridView1.AutoGenerateColumns=false;dataGridView1.DataSource=GetDataSource();}使用DataGridView和ADO.NET維護記錄DataGridView的AllowUserToAddRows屬性可指示是否顯示新記錄行DefaultValuesNeeded事件可以初始化字段值CellValidating事件驗證輸入CellEndEdit事件:當用戶輸入為空時,將以一個紅色的點提示用戶輸入錯誤,發現輸入錯誤后,可以按“ESC”按鈕取消例:Chapter05_0327使用綁定模式操作數據源要使用SqlDataAdapter更新外部數據,需要設置SqlDataAdapter的InsertCommand、UpdataCommand、DeleteCommand命令28使用綁定模式操作數據源數據維護1(介紹:使用ADO.NET批量更新方式):指定列的創建方式:自動|Coding|設計狀態指定。使用ADO.NET的

溫馨提示

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

評論

0/150

提交評論