The color of the row in the DataGrid that is set by the value of a column

Source: Internet
Author: User
The DataGrid is really today, and the interface is confusing.

To implement a row of the. Net window DataGrid, you can set the function of the row color based on the contents of the value of a column in the row.

First, attach a connection with a Datagrid. Many features expanded solution Windows Forms Datagrid

But without the solution of my needs, I finally managed to get it done with my colleagues ' help.


To set the color of the cell by the value of a cell I won't post it, there is a solution in the connection above.
The following is a solution that sets the whole row color by the value of a column. The key is to add a DataView attribute to the custom DataGridTextBoxColumn, plus overload paint ().
When using DataGridTextBoxColumn, assign the DataGrid-bound DataView to it.

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 was to conditionally set the Forebrush and/or Backbrush
Depending upon some crireria on the cell value
Here, we color anything which begins with a letter higher than ' F '
Try
{
Value from DataView, "ItemType" is the name of the row
String Colvalue = this. bindingdataview[rownum]["ItemType"]. ToString ();
Char val = colvalue[0];

if (Val > ' F ')//If the first letter is greater than ' 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 does the drawing with
The possibly changed brushes
Base. Paint (g, bounds, source, rownum, Backbrush, Forebrush, aligntoright);
}
}

}

Examples of Use
Datagridcoloredtextboxcolumn Colexceptiontype = new Datagridcoloredtextboxcolumn ();
Colitemtype.bindingdataview = Dtorderitem.defaultview; Assign a table's view value
Colitemtype.headertext = "ItemType";
Colitemtype.mappingname = "ItemType";
Colitemtype.width = 90;
Colitemtype.nulltext = "";
TableStyle. Gridcolumnstyles.add (Colitemtype);





Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.