请问如果设置DataGrid(win form)中指定单元格的背景色??

qiuwenhui 2004-10-27 03:52:48
如题。
...全文
180 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qiuwenhui 2004-10-28
  • 打赏
  • 举报
回复
fellowcheng(头都编大了) 的方法可以实现对指定行的背景色设定,
而langmafeng(乞力马扎罗) 提供的方法则可以对指定单元格的样式进行设定,
包括背景色、字体颜色、字体样式等,严重学习!

langmafeng 2004-10-27
  • 打赏
  • 举报
回复
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q927q
fellowcheng 2004-10-27
  • 打赏
  • 举报
回复
前面少了一些
//dgBrowse是dataGrid
DataGridTableStyle tableStyle = new DataGridTableStyle();
tableStyle.MappingName = "";

int numCols = fl_DB.Tables[""].Columns.Count;
DataGridCellColorTextBoxColumn columnTextColumn ;
for(int i = 0; i < numCols; ++i)
{
columnTextColumn = new DataGridCellColorTextBoxColumn();
columnTextColumn.HeaderText = fl_DB.Tables[""].Columns[i].ColumnName;
columnTextColumn.MappingName = fl_DB.Tables[""].Columns[i].ColumnName;

//为每个单元格建立设置背景色的事件.
columnTextColumn.CheckCellColor += new CellColorEventHandler(SetColorValues);

tableStyle.GridColumnStyles.Add(columnTextColumn);
}

this.dgBrowse.TableStyles.Clear();
this.dgBrowse.TableStyles.Add(tableStyle);
fellowcheng 2004-10-27
  • 打赏
  • 举报
回复
只改某几条,好象只能自己写了,我也是照别人的,给你参考一下
public void SetColorValues(object sender, DataGridCellColorEventArgs e)
{
//根据条件, 将相关行设置不同的背景色.
if (Convert.ToInt16(dgBrowse[e.Row,1])>1000)
e.BackColor=Color.PowderBlue;
else if (Convert.ToInt16(dgBrowse[e.Row,1])<100)
e.BackColor=Color.Pink;
}


public class DataGridCellColorEventArgs : EventArgs
{
private int _row;
private Color _backcolor;

public DataGridCellColorEventArgs(int row, Color val)
{
_row = row;
_backcolor = val;
}
public int Row
{
get{ return _row;}
set{ _row = value;}
}
public Color BackColor
{
get{ return _backcolor;}
set{ _backcolor = value;}
}
}



//为事件建立委托.
public delegate void CellColorEventHandler(object sender, DataGridCellColorEventArgs e);

public class DataGridCellColorTextBoxColumn : DataGridTextBoxColumn
{
public event CellColorEventHandler CheckCellColor;

public DataGridCellColorTextBoxColumn()
{
}

//继承DataGridTextBoxColumn的Pain事件.
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)
{
if(CheckCellColor != null)
{
//重绘画时,设置当前行的背景色
DataGridCellColorEventArgs e = new DataGridCellColorEventArgs(rowNum, Color.White);
CheckCellColor(this, e);

if(e.BackColor != Color.White)
backBrush = new SolidBrush(e.BackColor);
}

base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
}

protected override void Edit(System.Windows.Forms.CurrencyManager source, int rowNum,

System.Drawing.Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
base.Edit(source, rowNum, bounds, readOnly, instantText, cellIsVisible);
}
}
qiuwenhui 2004-10-27
  • 打赏
  • 举报
回复
我的需求是当DataGrid中记录的满足某个条件时,
这条记录的背景色就变成其他颜色,比如说红色。
从而与其他记录区别开来。
sogasoga 2004-10-27
  • 打赏
  • 举报
回复
见帮助:

private void AddCustomDataTableStyle()
{
myDataGridTableStyle1 = new DataGridTableStyle();

// EventHandlers
myDataGridTableStyle1.GridLineColorChanged += new System.EventHandler(GridLineColorChanged_Handler);
myDataGridTableStyle1.MappingName = "Customers";

// Set other properties.
myDataGridTableStyle1.AlternatingBackColor=System.Drawing.Color.Gold;
myDataGridTableStyle1.BackColor = System.Drawing.Color.White;
myDataGridTableStyle1.GridLineStyle=System.Windows.Forms.DataGridLineStyle.Solid;
myDataGridTableStyle1.GridLineColor=Color.Red;

// Set the HeaderText and Width properties.
DataGridColumnStyle myBoolCol = new DataGridBoolColumn();
myBoolCol.MappingName = "Current";
myBoolCol.HeaderText = "IsCurrent Customer";
myBoolCol.Width = 150;
myDataGridTableStyle1.GridColumnStyles.Add(myBoolCol);

// Add a second column style.
DataGridColumnStyle myTextCol = new DataGridTextBoxColumn();
myTextCol.MappingName = "custName";
myTextCol.HeaderText = "Customer Name";
myTextCol.Width = 250;
myDataGridTableStyle1.GridColumnStyles.Add(myTextCol);

// Create new ColumnStyle objects
DataGridColumnStyle cOrderDate = new DataGridTextBoxColumn();
cOrderDate.MappingName = "OrderDate";
cOrderDate.HeaderText = "Order Date";
cOrderDate.Width = 100;

// Use a PropertyDescriptor to create a formatted column.
PropertyDescriptorCollection myPropertyDescriptorCollection = BindingContext
[myDataSet, "Customers.custToOrders"].GetItemProperties();

// Create a formatted column using a PropertyDescriptor.
DataGridColumnStyle csOrderAmount =
new DataGridTextBoxColumn(myPropertyDescriptorCollection["OrderAmount"], "c", true);
csOrderAmount.MappingName = "OrderAmount";
csOrderAmount.HeaderText = "Total";
csOrderAmount.Width = 100;

// Add the DataGridTableStyle instances to the GridTableStylesCollection.
myDataGrid.TableStyles.Add(myDataGridTableStyle1);
}
private void GridLineColorChanged_Handler(object sender,EventArgs e)
{
MessageBox.Show("GridLineColor Changed", "DataGridTableStyle");
}

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧