.NET Tips : DataGrid中由某列的值设定行的颜色
来源:互联网 发布:用java编写计算器界面 编辑:程序博客网 时间:2024/05/21 09:50
今天真是的,又被界面搞的晕头转向.
为了实现.Net window DataGrid 的某一行可以根据该行某列的值的内容设定该行颜色的功能.
先贴一个连接,里面有DataGrid很多功能扩充的解决方案Windows Forms Datagrid
不过没有我这个需求的解决方案,最后终于还是在同事的帮助下搞定了.
由某一个单元格的值设定该单元格的颜色的实现我就不贴了,上面的连接里面有解决方案.
下面是由某列的值设定整行颜色的一个解决方案. 关键是在定制的DataGridTextBoxColumn里面添加一个DataView的属性,另外重载Paint() .?
在使用DataGridTextBoxColumn的时候,将DataGrid绑定的DataView赋值给它.
public?class public class DataGridColoredTextBoxColumn : DataGridTextBoxColumn
?{
???? private? System.Data.DataView m_bindDataView;
??? ?public?DataView BindingDataView
??? {
??????? get
??????? {
???????????? return m_bindDataView;
??????? }
??????? set
??????? {
??????????? m_bindDataView = value;
???? }
??}
??protected override void Paint(System.Drawing.Graphics g,
???System.Drawing.Rectangle bounds, System.Windows.Forms.CurrencyManager
???source, int rowNum, System.Drawing.Brush backBrush, System.Drawing.Brush
???foreBrush, bool alignToRight)
??{
????? // the idea is to conditionally set the foreBrush and/or backbrush
????? // depending upon some crireria on the cell value
????? // Here, we color anything that begins with a letter higher than 'F'
????? try
????? {
??????????? //从DataView中取值,"ItemType"为行的名称
????????????string colValue = this.BindingDataView[rowNum]["ItemType"].ToString();?????
?? ???????? char val = colValue[0];
??????????? if( val > 'F' ) //如果首字母大于 'F'
??????????? {
???????????????? backBrush = new SolidBrush(Color.BlueViolet );
???????????????? foreBrush = new SolidBrush(Color.White);
?????????? ?}
??? ?? }
????? catch(Exception ex)
????? {
????????? //empty catch
????? }
????? finally
????? {
???????? ?// make sure the base class gets called to do the drawing with
???????? // the possibly changed brushes
???????? base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
????? }
?? }?
?}
使用的例子
?? DataGridColoredTextBoxColumn colExceptionType = new DataGridColoredTextBoxColumn();
???colItemType.BindingDataView = dtOrderItem.DefaultView; //将table的view赋值
???colItemType.HeaderText =“ItemType”;
???colItemType.MappingName = “ItemType“;
???colItemType.Width = 90;
???colItemType.NullText = "";
???tablestyle.GridColumnStyles.Add(colItemType);
- .NET Tips : DataGrid中由某列的值设定行的颜色
- 如何设定DataGrid列的宽度
- ABAP ALV的颜色设定(单元格,行,列的颜色设定)
- 给DataGrid中满足条件的超链接列加颜色
- 如何在ASP.NET中用程序代码设定DATAGRID每列的宽度以及折不折行
- 在DataGrid中,使鼠标到达的行和列显示不同的颜色
- 在DataGrid中,使鼠标到达的行和列显示不同的颜色 (转载)
- 在DataGrid中,使鼠标到达的行和列显示不同的颜色
- 在DataGrid中,使鼠标到达的行和列显示不同的颜色
- ASP.NET,datagrid,textbox datagrid中隐藏列之后无法获得其中textbox值的问题
- 如何在c#中设定和控制DataGrid的列宽
- easyui datagrid中动态改变某行的颜色
- 读取datagrid中某一行某一列的值
- flex中获取datagrid选中行的列值
- 根据记录中某个域值来设定视图行的颜色
- MyEclipse中背景颜色的设定
- postgres中自动增长列的设定
- datagrid 设定列宽
- 软件人员的生涯规划
- [原创软件]中文简体、繁体、BIG5互转
- load data from (txt/ table and so on)
- 如何实现可以带详细表格的DropDownList
- 研发中心招聘的背后
- .NET Tips : DataGrid中由某列的值设定行的颜色
- Thinking in Java学习扎记(七)
- Thinking in Java学习扎记(八)
- Thinking in Java学习扎记(九)
- [转贴]TUXEDO精华贴总结
- 在一个函数中如何知道调用者(Python)
- delphi中的XML解析控件TXMLDocument的用法
- Simple chat application for ASP.NET
- 给青年一代的11点忠告——Bill Gates