C# Datagridview combox列 初始化颜色
private void m_dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
var a = e.Value; if (a != null && LstColors.Contains(a))
{
var color = GetColorFromCode(a.ToString()); var g = e.Graphics;
Brush b=new SolidBrush(color);
var rec = e.CellBounds; rec.X += 2;
rec.Y += 2;
rec.Width -= 20;
rec.Height -= 10; e.PaintContent(rec);
g.FillRectangle(b, rec);
e.Handled = true;
} }
public partial class Form1 : Form
{ private string xml =
@"<?xml version='1.0' standalone='yes'?>
<GenioCodes>
<Code No.='1' name='A' Colour='Blue' />
<Code No.='2' name='B' Colour='Green' />
<Code No.='3' name='C' Colour='Red' />
<Code No.='4' name='N' Colour='Black' />
</GenioCodes>"; DataSet m_dataSet = new DataSet();
List<ComboBox>ListCbx=new List<ComboBox>();
public Form1()
{
InitializeComponent(); // reading xml from string
var reader = XmlReader.Create(new StringReader(xml));
m_dataSet.ReadXml(reader);
m_dataGridView.DataSource = m_dataSet.Tables[0]; var column = m_dataGridView.Columns["Colour"];
int idx = column.Index;
// removing text column
m_dataGridView.Columns.RemoveAt(idx); // adding comboBox column
var cbo = new DataGridViewComboBoxColumn
{
Name = "color",
DataPropertyName = "Colour",
};
// unique color codes for comboBox
var colorCodes = m_dataSet.Tables[0].AsEnumerable()
.Select(r => r["Colour"])
.Distinct()
.ToList();
cbo.DataSource = colorCodes; // restore column in orignal position
m_dataGridView.Columns.Insert(idx, cbo);
m_dataGridView.EditingControlShowing += ComboBoxShowing; //disallow adding and deleting rows
m_dataGridView.AllowUserToAddRows = false;
m_dataGridView.AllowUserToDeleteRows = false; } /// <summary>
/// Activates custom drawing in comboBoxes
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void ComboBoxShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is ComboBox theCB)
{
theCB.DrawMode = DrawMode.OwnerDrawFixed;
try
{
theCB.DrawItem -= new DrawItemEventHandler(this.ComboItemDraw); }
catch { }
theCB.DrawItem += new DrawItemEventHandler(this.ComboItemDraw); ListCbx.Add(theCB);
}
} /// <summary>
/// Custom drawing for comboBox items
/// </summary>
private void ComboItemDraw(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics; Rectangle rDraw = e.Bounds;
rDraw.Inflate(-1, -1); bool bSelected = Convert.ToBoolean(e.State & DrawItemState.Selected);
bool bValue = Convert.ToBoolean(e.State & DrawItemState.ComboBoxEdit); rDraw = e.Bounds;
rDraw.Inflate(-1, -1); if (bSelected & !bValue)
{
g.FillRectangle(Brushes.LightBlue, rDraw);
g.DrawRectangle(Pens.Blue, rDraw);
}
else
{
g.FillRectangle(Brushes.White, e.Bounds);
} if (e.Index < 0)
return;
string code = ((ComboBox)sender).Items[e.Index].ToString(); Color c = GetColorFromCode(code);
string s = c.ToString(); SolidBrush b = new SolidBrush(c);
Rectangle r = new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
g.FillRectangle(b, r);
g.DrawRectangle(Pens.Black, r);
//g.DrawString(s, Form.DefaultFont, Brushes.Black, e.Bounds.Left + 25, e.Bounds.Top + 1); b.Dispose(); }
List<String> LstColors = new List<string>() { "Blue", "Green", "Red", "Black" };
/// <summary>
/// Returns color for a given code
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
private Color GetColorFromCode(string code)
{
switch (code)
{
case "Blue": return Color.Blue;
case "Green": return Color.Green;
case "Red": return Color.Red;
case "Black": return Color.Black;
}
return Color.Red;
}
int index; private void m_dataGridView_CellEnter(object sender, DataGridViewCellEventArgs e)
{
index = e.RowIndex;
//实现单击一次显示下拉列表框
if (m_dataGridView.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn && e.RowIndex != -1)
{
SendKeys.Send("{F4}");
}
} private void m_dataGridView_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
var a = e.Value; if (a != null && LstColors.Contains(a))
{
var color = GetColorFromCode(a.ToString()); var g = e.Graphics;
Brush b=new SolidBrush(color);
var rec = e.CellBounds; rec.X += 2;
rec.Y += 2;
rec.Width -= 20;
rec.Height -= 10; e.PaintContent(rec);
g.FillRectangle(b, rec);
e.Handled = true;
} }
}
来自问题:https://q.cnblogs.com/q/137678/#a_279065
C# Datagridview combox列 初始化颜色的更多相关文章
- .NET组件控件实例编程系列——5.DataGridView数值列和日期列
在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会自动显示对应类型的列.当然我们自己可以手工选择列的类型,例如ComboBox列.Button列.Link列. ...
- DataGridView数值列和日期列
本文转自:http://www.cnblogs.com/conexpress/p/5923324.html 在使用DataGridView编辑数据的时候,编辑的单元格一般会显示为文本框,逻辑值和图片会 ...
- ALV的颜色分为行的颜色、列的颜色和CELL的颜色
ALV的颜色分为行的颜色.列的颜色和CELL的颜色.任务要求,将一定的Tabellenfeld 用黄色填充,也就是说CELL的颜色 DATA:ls_cellcolorTYPElvc_s_scol,co ...
- DataGridView绑定数据库,取得的数据插入到DataGridView指定列(一)
实现: 点击button1,从数据库中获得数据,指定数据库的某列数据插入到DataGridView指定列 一.双击button1进入事件代码 private void button1_Click(ob ...
- HTML输出 一 控制列背景颜色
#将需要读取的域名和端口列表保存在名为ports01.txt.ports02的文件中,文件与脚本位于相同目录下$CurrentPath = $MyInvocation.MyCommand.Path.s ...
- groupbox 下的datagridview的列标题字体修改混乱
groupbox 下的datagridview的列标题字体修改混乱
- dev gridControl 自定义绘制列头颜色
1.添加事件CustomDrawColumnHeader private void gvw1_CustomDrawColumnHeader(object sender, DevExpress.Xtra ...
- C#中关于DataGridView行和列的背景色-前景色设置
关于DataGridView行和列的背景色-前景色设置 1.设定DataGridView全部单元格的Style DataGridView内所有单元格的Style变更,可以使用DataGridView ...
- datagridview 日期列排序
1.datagridview 日期列排序 private void Form1_Load(object sender, EventArgs e) { //方法1 dataGridView1.Colum ...
- (转)datagridview 自定义列三步走
本文转载自:http://blog.csdn.net/zx13525079024/article/details/4814642 我们如果想自定义实现datagridview的某列,例如是datagr ...
随机推荐
- Excel 分组后计算
分组后的计算都类似,仍然采用 groups 函数,分组并同时计算出各洲的 GDP 总量,然后再求一遍各洲的 GDP 总量占全球 GDP 的百分比值. SPL 代码如下: A B 1 =clipbo ...
- 报表如何批量导出成 excel 文件
需求说明 报表展现后可以通过工具栏中的导出按钮将当前展现的报表导出成 excel 文件,但是在实际使用中通常会要求报表不需要展现,直接通过一些操作将报表导出成 excel 文件,并且往往会要求批量导出 ...
- 抓包整理————tcp 协议[八]
前言 简单介绍一下tcp 协议. 正文 tcp历史: advanced research projects agency network: 1973年: tcp/ip 协议 tcpv4 协议分层后的网 ...
- Web前端 - Vue
<!-- id标识vue作用的范围 --> <div id="app"> <!-- {{}} 插值表达式,绑定vue中的data数据 --> { ...
- Javscript数组的常用方法有哪些?
数组基本操作可以归纳为 增.删.改.查,需要留意的是哪些方法会对原数组产生影响,哪些方法不会 下面对数组常用的操作方法做一个归纳 增 下面前三种是对原数组产生影响的增添方法,第四种则不会对原数组产生影 ...
- .NET Emit 入门教程:第六部分:IL 指令:9:详解 ILGenerator 指令方法:运算操作指令(指令篇结束)
前言: 经过前面几篇的学习,我们了解到指令的大概分类,如: 参数加载指令,该加载指令以 Ld 开头,将参数加载到栈中,以便于后续执行操作命令. 参数存储指令,其指令以 St 开头,将栈中的数据,存储到 ...
- 力扣34(java)-在排序数组中查找元素的第一个和最后一个位置(中等)
题目: 给你一个按照非递减顺序排列的整数数组 nums,和一个目标值 target.请你找出给定目标值在数组中的开始位置和结束位置. 如果数组中不存在目标值 target,返回 [-1, -1]. 你 ...
- 开源数据库PolarDB为什么能捕获娃哈哈的心?
简介: 在10月25日由阿里云开发者社区.PolarDB开源社区.infoQ联合举办的「开源人说」第三期--<数据库PolarDB专场>沙龙上,中启乘数科技(杭州)有限公司联合创始人唐成带 ...
- 双11特刊|十年磨一剑,云原生多模数据库Lindorm 2021双11总结
前言 2021 年,转眼 Lindorm 已经在阿里发展了十年的时间,从基于 HBase 深度改造的 Lindorm 1.0 版本,到全面重构,架构大幅升级的 Lindorm 2.0 版本:从单一的 ...
- 函数计算GB镜像秒级启动:下一代软硬件架构协同优化揭秘
简介: 优化镜像加速冷启动大致分为两种做法:降低绝对延迟和降低冷启动概率.自容器镜像上线以来我们已经通过镜像加速技术,分阶段降低了绝对延迟.本文在此基础上,介绍借助函数计算下一代IaaS底座神龙裸金属 ...