技术标签: C# c# exception styles windows dataset forms
C#DataGridView中的常用技巧
只列出技巧部分,后面会有补充
(最基本的技巧). 获取某列中的某行(某单元格)中的内容
this.currentposition = this.dataGridView1.BindingContext [this.dataGridView1.DataSource, this.dataGridView1.DataMember].Position; bookContent = this.database.dataSet.Tables[0].Rows [this.currentposition][21].ToString().Trim(); MessageBox.Show(bookContent); |
1、自定义列
//定义列宽 this.dataGridView1.Columns[0].Width = 80; this.dataGridView1.Columns[1].Width = 80; this.dataGridView1.Columns[2].Width = 180; this.dataGridView1.Columns[3].Width = 120; this.dataGridView1.Columns[4].Width = 120; Customize Cells and Columns in the Windows Forms DataGridView Control by Extending Their Behavior and Appearance Host Controls in Windows Forms DataGridView Cells |
继承 DataGridViewTextBoxCell 类生成新的Cell类,然后再继承 DataGridViewColumn 生成新的Column类,并指定
CellTemplate为新的Cell类。新生成的Column便可以增加到DataGridView中去。
2、自动适应列宽
Programmatically Resize Cells to Fit Content in the Windows Forms DataGridView Control
Samples:
DataGridView.AutoSizeColumns( DataGridViewAutoSizeColumnCriteria.HeaderAndDisplayedRows); DataGridView.AutoSizeColumn( DataGridViewAutoSizeColumnCriteria.HeaderOnly, 2, false); DataGridView.AutoSizeRow( DataGridViewAutoSizeRowCriteria.Columns, 2, false); DataGridView.AutoSizeRows( DataGridViewAutoSizeRowCriteria.HeaderAndColumns, 0, dataGridView1.Rows.Count, false); |
3、可以绑定并显示对象
Bind Objects to Windows Forms DataGridView Controls
4、可以改变表格线条风格
Change the Border and Gridline Styles in the Windows Forms DataGridView Control
Samples:
this.dataGridView1.GridColor = Color.BlueViolet; this.dataGridView1.BorderStyle = BorderStyle.Fixed3D; this.dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None; this.dataGridView1.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; this.dataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single; |
5、动态改变列是否显示,和动态改变列的显示顺序
Change the Order of the Columns in the Windows Forms DataGridView Control
Samples:
customersDataGridView.Columns["CustomerID"].Visible = false; customersDataGridView.Columns["ContactName"].DisplayIndex = 0; customersDataGridView.Columns["ContactTitle"].DisplayIndex = 1; customersDataGridView.Columns["City"].DisplayIndex = 2; customersDataGridView.Columns["Country"].DisplayIndex = 3; customersDataGridView.Columns["CompanyName"].DisplayIndex = 4; |
6、可以在列中显示图像
Display Images in Cells of the Windows Forms DataGridView Control
Samples:
Icon treeIcon = new Icon(this.GetType(), "tree.ico"); DataGridViewImageColumn iconColumn = new DataGridViewImageColumn (); iconColumn.Image = treeIcon.ToBitmap(); iconColumn.Name = "Tree"; iconColumn.HeaderText = "Nice tree"; dataGridView1.Columns.Insert(2, iconColumn); |
7、格式化显示内容:
Format Data in the Windows Forms DataGridView Control
Samples:
this.dataGridView1.Columns["UnitPrice"].DefaultCellStyle.Format = "c"; this.dataGridView1.Columns["ShipDate"].DefaultCellStyle.Format = "d"; this.dataGridView1.DefaultCellStyle.NullValue = "no entry"; this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewWrapMode.Wrap; this.dataGridView1.Columns["CustomerName"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight; |
8、在拖动列的滚动条时可以将指定的列冻结
Freeze Columns in the Windows Forms DataGridView Control
Samples:将指定列及以前的列固定不动
this.dataGridView1.Columns["AddToCartButton"].Frozen = true; |
9、获取选择的单元格,行,列
Get the Selected Cells, Rows, and Columns in the Windows Forms DataGridView Control
Samples:
见msdn。
10、显示录入时出现的错误信息
Handle Errors that Occur During Data Entry in the Windows Forms DataGridView Control
Samples:
private void dataGridView1_DataError(object sender, DataGridViewDataErrorEventArgs e) { // If the data source raises an exception when a cell value is // commited, display an error message. if (e.Exception != null && e.Context == DataGridViewDataErrorContext.Commit) { MessageBox.Show("CustomerID value must be unique."); } } |
11、大数据量显示采用Virtual Mode
Implement Virtual Mode in the Windows Forms DataGridView Control
12、设置指定的列只读
Make Columns in the Windows Forms DataGridView Control Read-Only
Samples:
dataGridView1.Columns["CompanyName"].ReadOnly = true; |
13、移去自动生成的列
Remove Autogenerated Columns from a Windows Forms DataGridView Control
Sample:
dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = customerDataSet; dataGridView1.Columns.Remove ("Fax"); 或: dataGridView1.Columns["CustomerID"].Visible = false; |
14、自定义选择模式
Set the Selection Mode of the Windows Forms DataGridView Control
Sample:
this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect; this.dataGridView1.MultiSelect = false; |
15、自定义设定光标进入单元格是否编辑模式(编辑模式)
Specify the Edit Mode for the Windows Forms DataGridView Control
this.dataGridView1.EditMode = DataGridViewEditMode.EditOnEnter; |
16、新行指定默认值
Specify Default Values for New Rows in the Windows Forms DataGridView Control
Sample:
private void dataGridView1_DefaultValuesNeeded(object sender, System.Windows.Forms.DataGridViewRowEventArgs e) { e.Row.Cells["Region"].Value = "WA"; e.Row.Cells["City"].Value = "Redmond"; e.Row.Cells["PostalCode"].Value = "98052-6399"; e.Row.Cells["Region"].Value = "NA"; e.Row.Cells["Country"].Value = "USA"; e.Row.Cells["CustomerID"].Value = NewCustomerId(); } |
17、数据验证
Validate Data in the Windows Forms DataGridView Control
Samples:
private void dataGridView1_CellValidating(object sender, DataGridViewCellValidatingEventArgs e) { // Validate the CompanyName entry by disallowing empty strings. if (dataGridView1.Columns[e.ColumnIndex].Name == "CompanyName") { if (e.FormattedValue.ToString() == String.Empty) { dataGridView1.Rows[e.RowIndex].ErrorText = "Company Name must not be empty"; e.Cancel = true; } } } |
18、数据提交到dataset中
DataSet ds = new DataSet("MyDataSet"); ds.Tables[biaom.Trim()].Rows.Clear(); try { for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { DataTable dt = ds.Tables[biaom.Trim()]; DataRow myrow = ds.Tables[biaom.Trim()].NewRow(); for (int j = 0; j < dataGridView1.Columns.Count; j++) { myrow[j] = Convert.ToString(dataGridView1.Rows[i].Cells[j].Value); } ds.Tables[biaom.Trim()].Rows.Add(myrow); } } catch (Exception) { MessageBox.Show("输入类型错误!"); return; } |
文章浏览阅读529次。百分百题库提供建筑八大员(材料员)考试试题、建筑八大员(材料员)考试预测题、建筑八大员(材料员)考试真题、建筑八大员(材料员)证考试题库等,提供在线做题刷题,在线模拟考试,助你考试轻松过关。310项目经理部应编制机械设备使用计划并报()审批。A监理单位B企业C建设单位D租赁单位答案:B311对技术开发、新技术和新工艺应用等情况进行的分析和评价属于()。A人力资源管理考核B材料管理考核C机械设备管理考核D技术管理考核答案:D312建筑垃圾和渣土._料账的试题
文章浏览阅读614次。本文由chatgpt生成,文章没有在chatgpt生成的基础上进行任何的修改。以上只是chatgpt能力的冰山一角。作为通用的Aigc大模型,只是展现它原本的实力。对于颠覆工作方式的ChatGPT,应该选择拥抱而不是抗拒,未来属于“会用”AI的人。AI职场汇报智能办公文案写作效率提升教程 专注于AI+职场+办公方向。下图是课程的整体大纲下图是AI职场汇报智能办公文案写作效率提升教程中用到的ai工具。_python自动打开浏览器
文章浏览阅读545次。Linux中安装JDK-RPM方式_linux 安装jdk rpm
文章浏览阅读25次。免费领取项目源码,请关注赞收藏并私信博主,谢谢-高校志愿者管理系统主要功能模块包括页、个人资料(个人信息。修改密码)、公共管理(轮播图、系统公告)、用户管理(管理员、志愿用户)、信息管理(志愿资讯、资讯分类)、活动分类、志愿活动、报名信息、活动心得、留言反馈,采取面对对象的开发模式进行软件的开发和硬体的架设,能很好的满足实际使用的需求,完善了对应的软体架设以及程序编码的工作,采取SQL Server 作为后台数据的主要存储单元,采用Asp.Net技术进行业务系统的编码及其开发,实现了本系统的全部功能。
文章浏览阅读122次。原标题:小米OV对于是否采用鸿蒙保持沉默,原因是中国制造需要它们目前华为已开始对鸿蒙系统大规模宣传,不过中国手机四强中的另外三家小米、OPPO、vivo对于是否采用鸿蒙系统保持沉默,甚至OPPO还因此而闹出了一些风波,对此柏铭科技认为这是因为中国制造当下需要小米OV几家继续将手机出口至海外市场。 2020年中国制造支持中国经济渡过了艰难的一年,这一年中国进出口贸易额保持稳步增长的势头,成为全球唯一..._小米宣布用鸿蒙系统
文章浏览阅读1.3k次。1.Kafka Eagle实现kafka消息监控的代码细节是什么?2.Kafka owner的组成规则是什么?3.怎样使用SQL进行kafka数据预览?4.Kafka Eagle是否支持多集群监控?1.概述在《Kafka 消息监控 - Kafka Eagle》一文中,简单的介绍了 Kafka Eagle这款监控工具的作用,截图预览,以及使用详情。今天_kafka eagle git
文章浏览阅读1.1k次,点赞29次,收藏19次。Eva.js 是一个专注于开发互动游戏项目的前端游戏引擎。:Eva.js 提供开箱即用的游戏组件供开发人员立即使用。是的,它简单而优雅!:Eva.js 由高效的运行时和渲染管道 (Pixi.JS) 提供支持,这使得释放设备的全部潜力成为可能。:得益于 ECS(实体-组件-系统)架构,你可以通过高度可定制的 API 扩展您的需求。唯一的限制是你的想象力!_eva.js
文章浏览阅读1k次。Objective-C概述Objective-C是一种面向对象的计算机语言,1980年代初布莱德.考斯特在其公司Stepstone发明Objective-C,该语言是基于SmallTalk-80。1988年NeXT公司发布了OC,他的开发环境和类库叫NEXTSTEP, 1994年NExt与Sun公司发布了标准的NEXTSTEP系统,取名openStep。1996_objective-c特点及应用领域
文章浏览阅读955次,点赞20次,收藏16次。TIM(Timer)定时器定时器可以对输入的时钟进行计数,并在计数值达到设定值时触发中断16位计数器、预分频器、自动重装寄存器的时基单元,在 72MHz 计数时钟下可以实现最大 59.65s 的定时,59.65s65536×65536×172MHz59.65s65536×65536×721MHz不仅具备基本的定时中断功能,而且还包含内外时钟源选择、输入捕获、输出比较、编码器接口、主从触发模式等多种功能。_stm32 tim寄存器详解
文章浏览阅读1.5k次。对于任何有兴趣学习前端 Web 开发的人来说,了解 HTML、CSS 和JavaScript 之间的区别至关重要。这三种前端语言都是您访问过的每个网站的用户界面构建块。而且,虽然每种语言都有不同的功能重点,但它们都可以共同创建令人兴奋的交互式网站,让用户保持参与。因此,您会发现学习所有三种语言都很重要。如果您有兴趣从事前端开发工作,可以通过多种方式学习这些语言——在艾编程就可以参与到学习当中来。在本文中,我们将回顾每种语言的特征、它们如何协同工作以及您可以在哪里学习它们。HTML vs C._艾编程学习资料
文章浏览阅读2.8k次。点云配准主要针对点云的:不完整、旋转错位、平移错位。因此要得到完整点云就需要对局部点云进行配准。为了得到被测物体的完整数据模型,需要确定一个合适的坐标系变换,将从各个视角得到的点集合并到一个统一的坐标系下形成一个完整的数据点云,然后就可以方便地进行可视化,这就是点云数据的配准。点云配准技术通过计算机技术和统计学规律,通过计算机计算两个点云之间的错位,也就是把在不同的坐标系下的得到的点云进行坐标变..._局部点云与全局点云配准
文章浏览阅读273次。提取码:0oorGoogle和YouTube由于Python的高可适应性、易于维护以及适合于快速开发而采用它。如果你想要编写高质量、高效的并且易于与其他语言和工具集成的代码,《Python学习手册:第4 版》将帮助你使用Python快速实现这一点,不管你是编程新手还是Python初学者。本书是易于掌握和自学的教程,根据作者Python专家Mark Lutz的著名培训课程编写而成。《Python学习..._零基础学pythonpdf电子书