《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

提交评论