The datagrid| condition requires a data row for the DataGrid, depending on the conditions of the project, depending on the background hue. Since there are no related properties and methods in the DataGrid that can be set directly, it takes some effort to complete this feature. Search the internet for half a day, also did not find a solution. I had to do it by myself,:). Studied for a long time, and finally took care of it. Good things do not enjoy the exclusive, special posted out, hoping to bring some help to those in need.
{
//...
Use DataGridTableStyle to display the DataGrid.
DataGridTableStyle TableStyle = new DataGridTableStyle ();
Tablestyle.mappingname = "Customers";
int numcols = _dataset.tables["Customers"]. Columns.count;
Datagridcellcolortextboxcolumn Columntextcolumn;
for (int i = 0; i < numcols; ++i)
{
Columntextcolumn = new Datagridcellcolortextboxcolumn ();
Columntextcolumn.headertext = _dataset.tables["Customers"]. Columns[i]. ColumnName;
Columntextcolumn.mappingname = _dataset.tables["Customers"]. Columns[i]. ColumnName;
Creates an event that sets the background color for each cell.
Columntextcolumn.checkcellcolor + = new Cellcoloreventhandler (setcolorvalues);
public void Setcolorvalues (object sender, Datagridcellcoloreventargs e)
{
Sets the related row to a different background color, depending on the criteria.
The following example sets the row for Mexico for the Country (column 9th in the DataGrid) to Red, and the USA row to yellow.
if (Convert.ToString (datagrid1[e.row,8]) = = "Mexico")
E.backcolor = color.red;
else if (convert.tostring (datagrid1[e.row,8]) = = "USA")
E.backcolor = Color.yellow;
}
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;}
}
}
Establish a delegate for the event.
public delegate void Cellcoloreventhandler (object sender, Datagridcellcoloreventargs e);
public class Datagridcellcolortextboxcolumn:datagridtextboxcolumn
{
public event Cellcoloreventhandler Checkcellcolor;
Public Datagridcellcolortextboxcolumn ()
{
}
Inherits the DataGridTextBoxColumn pain event.
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)
{
Sets the background color of the current row when you re painting
Datagridcellcoloreventargs e = new Datagridcellcoloreventargs (rownum, Color.White);
Checkcellcolor (this, e);
if (E.backcolor!= color.white)
Backbrush = new SolidBrush (E.backcolor);
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.