一、DataGridView 单元格验证
1、定义单元格验证
要求:验证错误后焦点不离开。
实现:
单元格的验证可以使用dgv_details_CellValidating事件。
验证不通过时调用e.Cancel = true;终止事件链,单元格将保持编辑状态。
调用dgv_details.CancelEdit();可以使单元格的内容会滚到修改前的值。
使用System.Windows.Forms.SendKeys.Send("^a");将全选单元格的内容。
2、单元格选中并开始编辑状态
实现:
-
- dgv_details.Focus();
-
- dgv_details.CurrentCell=dgv_details[0,0];
-
-
dgv_details.BeginEdit(false);
3、定制自动生成绑定了列
实现:
-
dgv_details.AutoGenerateColumns=false;
4、设置列的背景色
实现:
- ColorGridReadOnlyColor=Color.LightGoldenrodYellow;
-
dgv_details.Columns[1].DefaultCellStyle.BackColor=WinKeys.GridReadOnlyColor;
5、DataGridView单元格验证的设计的问题
问题:绑定还是不绑定?
1)绑定的优势:比较简单,代码少。
2)绑定得缺点:DataGridView中的数据受数据源的影响(主键约束、值类型约束)。不一至时会激发DataError事件,输入的内容无法保存到单元格中和数据源中。特殊的验证(比如长度、格式等)还是需要另外写代码实现。
关于增加行的问题。增加新行时多主键的验证有问题,而且验证不通过时会将新行全部删除。限制很多,很不方便。
3)非绑定的优势:验证等处理比较灵活。不受数据源的约束。
4)非绑定得缺点:显示和向数据库更新数据时需要比较多的代码实现,效率比较低。
5)总的感觉在处理验证比较麻烦的场合,我还是比较喜欢非绑定的方式。如果数据量大,验证比较简单的场合使用绑定模式比较好。
二、DataGridView重绘代码
1、CellFormatting事件,一般重绘单元格属性。
-
privateBitmaphighPriImage;
-
privateBitmapmediumPriImage;
-
privateBitmaplowPriImage;
-
privatevoiddataGridView1_CellFormatting(objectsender,
- System.Windows.Forms.DataGridViewCellFormattingEventArgse)
- {
-
-
if(dataGridView1.Columns[e.ColumnIndex].Name.Equals("Balance"))
- {
- Int32intValue;
-
if(Int32.TryParse((String)e.Value,outintValue)&&
- (intValue<0))
- {
- e.CellStyle.BackColor=Color.Red;
- e.CellStyle.SelectionBackColor=Color.DarkRed;
- }
- }
-
-
if(dataGridView1.Columns[e.ColumnIndex].Name.Equals("Priority"))
- {
-
-
StringstringValue=e.Valueasstring;
-
if(stringValue==null)return;
-
- DataGridViewCellcell=dataGridView1[e.ColumnIndex,e.RowIndex];
- cell.ToolTipText=stringValue;
-
-
switch(stringValue)
- {
-
case"high":
- e.Value=highPriImage;
-
break;
-
case"medium":
- e.Value=mediumPriImage;
-
break;
-
case"low":
- e.Value=lowPriImage;
-
break;
- }
- }
- }
2、CellPainting事件,一般用于合并单元格用
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.DrawString 自己来“画”。
下面的代码可以对DataGridView第1列内容相同的单元格进行合并:
-
#region"合并单元格的测试"
-
privateint?nextrow=null;
-
privateint?nextcol=null;
-
privatevoiddataGridView1_CellFormatting(objectsender,System.Windows.Forms.DataGridViewCellFormattingEventArgse)
- {
-
if(this.dataGridView1.Columns["description"].Index==e.ColumnIndex&&e.RowIndex>=0)
- {
-
if(this.nextcol!=null&e.ColumnIndex==this.nextcol)
- {
- e.CellStyle.BackColor=Color.LightBlue;
-
this.nextcol=null;
- }
-
if(this.nextrow!=null&e.RowIndex==nextrow)
- {
- e.CellStyle.BackColor=Color.LightPink;
-
this.nextrow=null;
- }
-
if(e.RowIndex!=this.dataGridView1.RowCount-1)
- {
-
if(e.Value.ToString()==this.dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString())
- {
- e.CellStyle.BackColor=Color.LightPink;
- nextrow=e.RowIndex+1;
- }
- }
- }
-
if(this.dataGridView1.Columns["name"].Index==e.ColumnIndex&&e.RowIndex>=0)
- {
-
if(e.Value.ToString()==this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString())
- {
- e.CellStyle.BackColor=Color.LightBlue;
- nextcol=e.ColumnIndex+1;
- }
- }
- }
-
-
-
privatevoiddataGridView1_CellPainting(objectsender,System.Windows.Forms.DataGridViewCellPaintingEventArgse)
- {
-
-
if(this.dataGridView1.Columns["description"].Index==e.ColumnIndex&&e.RowIndex>=0)
- {
-
using(
-
BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor),
-
backColorBrush=newSolidBrush(e.CellStyle.BackColor))
- {
-
using(PengridLinePen=newPen(gridBrush))
- {
-
- e.Graphics.FillRectangle(backColorBrush,e.CellBounds);
-
-
-
if(e.RowIndex!=this.dataGridView1.RowCount-1)
- {
-
if(e.Value.ToString()!=this.dataGridView1.Rows[e.RowIndex+1].Cells[e.ColumnIndex].Value.ToString())
- {
- e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,
-
e.CellBounds.Right-1,e.CellBounds.Bottom-1);
-
-
if(e.Value!=null)
- {
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- Brushes.Crimson,e.CellBounds.X+2,
- e.CellBounds.Y+2,StringFormat.GenericDefault);
- }
- }
- }
-
else
- {
- e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,
-
e.CellBounds.Right-1,e.CellBounds.Bottom-1);
-
-
if(e.Value!=null)
- {
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- Brushes.Crimson,e.CellBounds.X+2,
- e.CellBounds.Y+2,StringFormat.GenericDefault);
- }
- }
-
- e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,
- e.CellBounds.Top,e.CellBounds.Right-1,
- e.CellBounds.Bottom-1);
-
e.Handled=true;
- }
- }
- }
-
-
if(this.dataGridView1.Columns["name"].Index==e.ColumnIndex&&e.RowIndex>=0)
- {
-
using(
-
BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor),
-
backColorBrush=newSolidBrush(e.CellStyle.BackColor))
- {
-
using(PengridLinePen=newPen(gridBrush))
- {
-
- e.Graphics.FillRectangle(backColorBrush,e.CellBounds);
-
if(e.Value.ToString()!=this.dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex+1].Value.ToString())
- {
-
- e.Graphics.DrawLine(gridLinePen,e.CellBounds.Right-1,e.CellBounds.Top,
- e.CellBounds.Right-1,e.CellBounds.Bottom-1);
-
-
if(e.Value!=null)
- {
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- Brushes.Crimson,e.CellBounds.X+2,
- e.CellBounds.Y+2,StringFormat.GenericDefault);
- }
- }
-
- e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,e.CellBounds.Bottom-1,
- e.CellBounds.Right-1,e.CellBounds.Bottom-1);
-
e.Handled=true;
- }
- }
- }
-
}
-
#endregion
三、在GridView中如何格式化Money型字段(downmoon)?
-
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"DataKeyNames="PKID"
-
DataMember="DefaultView"DataSourceID="SqlDataSource1">
-
<Columns>
-
<asp:BoundFieldDataField="PKID"HeaderText="PKID"InsertVisible="False"ReadOnly="True"
-
SortExpression="PKID"/>
-
<asp:TemplateField>
-
<HeaderTemplate>
-
amount</HeaderTemplate>
-
<ItemTemplate>
-
<asp:LabelID="txtMoney"Text='<%#Decimal.Parse(DataBinder.Eval(Container.DataItem,"amount").ToString())%>'runat="server"/>
-
</ItemTemplate>
-
</asp:TemplateField>
-
<asp:BoundFieldDataField="TestID"HeaderText="TestID"SortExpression="TestID"/>
-
<asp:BoundFieldDataField="testString"HeaderText="testString"SortExpression="testString"/>
-
</Columns>
-
</asp:GridView>
这段代码中,
amount为Money型字段,无论如何只能显示成
1234.5600
而不能显示成
1,234.56
用
-
<asp:BoundFieldDataField="amount"HeaderText="amount"DataFormatString="{0:n2}"/>
-
<asp:BoundFieldDataField="amount"HeaderText="amount"DataFormatString="{0:c2}"/>
也不行!
后来在MSDN上找到了答案
-
<asp:BoundFieldDataField="amount"HeaderText="amount"DataFormatString="{0:#,###.00}"HtmlEncode="False"/>
关键在于HtmlEncode="False"
http://blogs.msdn.com/danielfe/archive/2006/02/08/527628.aspx
(源文:http://blog.csdn.net/downmoon/archive/2007/11/01/1860611.aspx)
四、DataGridView合并单元格 编辑单元格
同事的一个项目需要将DataGridView单元格中的内容分不同颜色显示,想了想只有重绘了。
这种方法还可以用做合并单元格。
参考代码:
-
privatevoiddataGridView1_CellPainting(objectsender,DataGridViewCellPaintingEventArgse)
- {
-
if(e.RowIndex==0&&e.ColumnIndex>=0)
- {
-
intleft=e.CellBounds.Left;
-
inttop=e.CellBounds.Top;
-
intright=e.CellBounds.Right;
-
intbottom=e.CellBounds.Bottom;
-
e.Graphics.FillRectangle(newSolidBrush(Color.White),e.CellBounds);
-
e.Handled=true;
-
BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor);
-
PengridLinePen=newPen(gridBrush);
- e.Graphics.DrawLine(gridLinePen,right-1,
- top,right-1,
- bottom-1);
- e.Graphics.DrawLine(gridLinePen,left,
- bottom-1,right,
- bottom-1);
-
Brushb1=newSolidBrush(Color.Black);
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- b1,left+2,
- top+1,StringFormat.GenericDefault);
-
Brushb2=newSolidBrush(Color.Red);
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- b2,left+2,
- top+10,StringFormat.GenericDefault);
- }
-
DataGridViewSelectedCellCollectiondgvscc=this.dataGridView1.SelectedCells;
-
foreach(DataGridViewCelldgvcindgvscc)
- {
-
if(e.RowIndex==0
- &&e.RowIndex==dgvc.RowIndex
- &&e.ColumnIndex==dgvc.ColumnIndex)
- {
-
intleft=e.CellBounds.Left;
-
inttop=e.CellBounds.Top;
-
intright=e.CellBounds.Right;
-
intbottom=e.CellBounds.Bottom;
-
-
e.Graphics.FillRectangle(newSolidBrush(Color.FromArgb(10,36,106)),e.CellBounds);
-
-
BrushgridBrush=newSolidBrush(this.dataGridView1.GridColor);
-
PengridLinePen=newPen(gridBrush);
- e.Graphics.DrawLine(gridLinePen,right-1,
- top,right-1,
- bottom-1);
- e.Graphics.DrawLine(gridLinePen,left,
- bottom-1,right,
- bottom-1);
-
-
Brushb1=newSolidBrush(Color.White);
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- b1,left+2,
- top+1,StringFormat.GenericDefault);
-
Brushb2=newSolidBrush(Color.White);
- e.Graphics.DrawString((String)e.Value,e.CellStyle.Font,
- b2,left+2,
- top+10,StringFormat.GenericDefault);
- }
- }
-
e.Handled=true;
- }
感谢各位作者。
分享到:
相关推荐
DataGridView控件用法合集 56. DataGridView根据值不同在另一列中显示相应图片 57. DataGridView中显示进度条(ProgressBar) 58. DataGridView中添加MaskedTextBox 59. DataGridView中Enter键按下焦点移至旁边的...
对datagridview 控件的用法、技巧提供详细的实例,参考价值较大。
包括ToolStrip控件应用、泛型应用、注册表管理、WMI管理规范使用、XML文件处理、多线程处理、程序异常处理、文件压缩及解压缩、文件加密解密、文件访问权限、API函数调用、GDI+图像文字特效处理、数据更新并发冲突...
使用VS中自带的C#开发app的资料,包括传值、datagridview等使用技巧以及代码参考
0826 将DataGridView控件中的数据导入Excel 495 0827 无法获取自定义环境变量的值 496 0828 将组件放到COM+服务器上去 496 0829 调用非托管的DLL文件 497 0830 如何将GridView控件数据导入Word 497 0831 ...
14 <br>0028 “///”符号的使用技巧 14 <br>0029 使用注释取消程序语句的执行 15 <br>2.2 语句 15 <br>0030 跳转语句GOTO的使用 15 <br>0031 Continue语句的使用 16 <br>0032 Break...
3、将查询结果集赋给dateGridView 4、让文本框同步显示dataGridView中的当前记录 5、根据所选的查询条件动态组合SQL语句 6、用数据库表中的指定列值初始化checkListBox控件 7、利用循环将查询结果赋给含checkBox列...
.net中的正则表达式使用高级技巧 (一) C#静态成员和方法的学习小结 C#中结构与类的区别 C#中 const 和 readonly 的区别 利用自定义属性,定义枚举值的详细文本 Web标准和ASP.NET - 第一部分 XHTML介绍 在ASP.NET...
第二篇包含了C#语言相关的高级概念,如集合对象、泛型、程序集、多线程等等,重点讲述了VC#中数据处理的相关技术,如ADO.NET编程、数据集、数据绑定、DataGridView控件、打印和报表等,同时,对面向对象的分析与设计...
932 Datagridview控件的使用. 221 9 综合示例 226 仟务实训部分.… 229 1:航班查询窗体设计 29 2:显示所有航班信息 232 3:根据两地査询航班 .233 4:航班详细信息查询 234 巩固练习 235 第10章课程总复习… …)误!...