The cell implementation of the DataGridView with icons. The cells of the datagridview

Source: Internet
Author: User

The cell implementation of the DataGridView with icons. The cells of the datagridview

Purpose:

Expand the table control that comes with C # WinForm to enable it to automatically determine the upper and lower limits of data and identify overflow.

The method used here is: extend the table's column object: DataGridViewColumn.

1. Create class: DecimalCheckCell

/// <Summary> /// Value cell for range check /// </summary> public class DecimalCheckCell: DataGridViewTextBoxCell {private bool checkMaxValue = false; private bool checkMinValue = false; private decimal maxValue = 0; private decimal minValue = 0; public decimal MaxValue {get {return maxValue;} internal set {maxValue = value ;}} public decimal MinValue {get {return minValue;} internal set {minValue = Value ;}} public bool CheckMaxValue {get {return checkMaxValue;} internal set {checkMaxValue = value ;}} public bool CheckMinValue {get {return checkMinValue ;} internal set {checkMinValue = value;} public override object Clone () {DecimalCheckCell c = base. clone () as DecimalCheckCell; c. checkMaxValue = this. checkMaxValue; c. checkMinValue = this. checkMinValue; c. maxValue = this. maxVa Lue; c. minValue = this. minValue; return c;} protected override void Paint (Graphics graphics, Rectangle labels, Rectangle cellBounds, int rowIndex, inclucellstate, object value, object formattedValue, string errorText, and DataGridViewCellStyle cellStyle, dataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) {// Paint the base content base. pa Int (graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); // determines if (this. rowIndex <0 | this. owningRow. isNewRow) // The row sequence number is not-1 and is not a new record row (seemingly useless) return; if (value = null) return; decimal vCurValue = Convert. toDecimal (value); bool overValue = false; Image img = null; if (checkMaxValue) {overValue = vCurValue> maxValu E; img = VsTest. Properties. Resources. Undo; // picture from the added resource file} if (checkMinValue &&! OverValue) {overValue = vCurValue <minValue; img = VsTest. properties. resources. redo; // image from the added resource file} // draw the image after the value text if (overValue & img! = Null) {var vSize = graphics. measureString (vCurValue. toString (), cellStyle. font); System. drawing. drawing2D. graphicsContainer container = graphics. beginContainer (); graphics. setClip (cellBounds); graphics. drawImageUnscaled (img, new Point (cellBounds. location. X + (int) vSize. width, cellBounds. location. y); graphics. endContainer (container) ;}} protected override bool SetValue (int rowIndex, object value) {if (rowIndex> = 0) {try {decimal vdeci = Convert. toDecimal (value); // filter non-numeric base. errorText = string. empty;} catch (Exception ex) {base. errorText = "input error" + ex. message; return false ;}} return base. setValue (rowIndex, value );}}

2. Create a class: DecimalCheckColumn

/// <Summary> /// value column for range check /// </summary> public class DecimalCheckColumn: DataGridViewColumn {private bool checkMaxValue = false; private bool checkMinValue = false; private decimal maxValue = 0; private decimal minValue = 0; public decimal MaxValue {get {return maxValue;} set {maxValue = value; (base. cellTemplate as DecimalCheckCell ). maxValue = value ;}} public decimal MinValue {get {return minValue;} set {minValue = value; (base. cellTemplate as DecimalCheckCell ). minValue = value ;}/// <summary> // check whether the upper limit of the value is set, use with MaxValue /// </summary> public bool CheckMaxValue {get {return checkMaxValue;} set {checkMaxValue = value; (base. cellTemplate as DecimalCheckCell ). checkMaxValue = value ;}/// <summary> /// check whether the value limit is set, use with MinValue /// </summary> public bool CheckMinValue {get {return checkMinValue;} set {checkMinValue = value; (base. cellTemplate as DecimalCheckCell ). checkMinValue = value ;}} public DecimalCheckColumn (): base (new DecimalCheckCell () {} public override object Clone () {DecimalCheckColumn c = base. clone () as DecimalCheckColumn; c. checkMaxValue = this. checkMaxValue; c. checkMinValue = this. checkMinValue; c. maxValue = this. maxValue; c. minValue = this. minValue; return c ;}}

3. Now you can useDrag a dview control on the form and add the following code:

Private void TestForm_Load (object sender, EventArgs e) {InitControlsProperties (); // initialization // bind the data DataTable dTabel = new DataTable (); dTabel. columns. add ("ID", typeof (int); dTabel. columns. add ("TestValue", typeof (decimal); Random rnd = new Random (); for (int I = 0; I <10; I ++) // random 10 count {var Vd = dTabel. newRow (); SVD [0] = I + 1; MDD [1] = rnd. next (50); dTabel. rows. add (VD);} this. dataGridView1.DataSource = dTabel;} private void InitControlsProperties () {DecimalCheckColumn ColumnRoleID = new DecimalCheckColumn (); ColumnRoleID. dataPropertyName = "ID"; ColumnRoleID. defaultCellStyle. alignment = maid. middleLeft; ColumnRoleID. name = "ID"; ColumnRoleID. headerText = "no."; ColumnRoleID. width = 50; this. dataGridView1.Columns. add (ColumnRoleID); DecimalCheckColumn ColumnRoleName = new DecimalCheckColumn (); ColumnRoleName. dataPropertyName = "TestValue"; ColumnRoleName. defaultCellStyle. alignment = maid. middleLeft; ColumnRoleName. name = "TestValue"; ColumnRoleName. headerText = "Test Data"; ColumnRoleName. width = 100; ColumnRoleName. checkMaxValue = true; // check the maximum value of ColumnRoleName. maxValue = 41; ColumnRoleName. checkMinValue = true; // check the minimum value of ColumnRoleName. minValue = 7; this. dataGridView1.Columns. add (ColumnRoleName); // this. dataGridView1.AllowUserToAddRows = false; // this. dataGridView1.AllowUserToDeleteRows = false; // this. dataGridView1.ReadOnly = true; this. dataGridView1.AutoGenerateColumns = false ;}

Running EffectAs shown in the left

 
   

What is the ghost on the right?

There is another problem that cannot be solved: by default, the data loaded for the first time cannot completely determine whether the threshold is exceeded. Sometimes one or two of them can be judged, and sometimes they cannot be determined at all, however, you only need to click each cell with the mouse, and it can be automatically recognized. No cause is found.

 

[Http://www.cnblogs.com/CUIT-DX037/]

 

Related Article

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.