using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Windows.Forms.VisualStyles; namespace eBroker.Leo.W ...{ publicclass DataGridViewDisableCheckBoxColumn : DataGridViewCheckBoxColumn ...{ public DataGridViewDisableCheckBoxColumn() ...{ this.CellTemplate =new DataGridViewDisableCheckBoxCell(); } } publicclass DataGridViewDisableCheckBoxCell : DataGridViewCheckBoxCell ...{ privatebool enabledValue; publicbool Enabled ...{ get ...{ return enabledValue; } set ...{ enabledValue = value; } } // Override the Clone method so that the Enabled property is copied. publicoverrideobject Clone() ...{ DataGridViewDisableCheckBoxCell cell = (DataGridViewDisableCheckBoxCell)base.Clone(); cell.Enabled =this.Enabled; return cell; } // By default, enable the CheckBox cell. public DataGridViewDisableCheckBoxCell() ...{ this.enabledValue =true; } protectedoverridevoid Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates elementState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) ...{ // The checkBox cell is disabled, so paint the border, // background, and disabled checkBox for the cell. if (!this.enabledValue) ...{ // Draw the cell background, if specified. if ((paintParts & DataGridViewPaintParts.Background) == DataGridViewPaintParts.Background) ...{ SolidBrush cellBackground = new SolidBrush(cellStyle.BackColor); graphics.FillRectangle(cellBackground, cellBounds); cellBackground.Dispose(); } // Draw the cell borders, if specified. if ((paintParts & DataGridViewPaintParts.Border) == DataGridViewPaintParts.Border) ...{ PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle); } // Calculate the area in which to draw the checkBox. CheckBoxState state = value !=null&& (bool)value ? CheckBoxState.CheckedDisabled : CheckBoxState.UncheckedDisabled; Size size = CheckBoxRenderer.GetGlyphSize(graphics, state); Point center =new Point(cellBounds.X, cellBounds.Y); center.X += (cellBounds.Width - size.Width) /2; center.Y += (cellBounds.Height - size.Height) /2; // Draw the disabled checkBox. CheckBoxRenderer.DrawCheckBox(graphics, center, state); } else ...{ // The checkBox cell is enabled, so let the base class // handle the painting. base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); } } } }
Test:-->>
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace eBroker.Leo.W ...{ publicpartialclass Form1 : Form ...{ private DataGridView dataGridView1 =new DataGridView(); public Form1() ...{ InitializeComponent(); this.AutoSize =true; this.Load +=new EventHandler(Form1_Load); } publicvoid Form1_Load(object sender, EventArgs e) ...{ DataGridViewCheckBoxColumn column0 = new DataGridViewCheckBoxColumn(); DataGridViewDisableCheckBoxColumn column1 = new DataGridViewDisableCheckBoxColumn(); column0.Name ="CheckBoxes"; column1.Name ="DisableCheckBoxes"; dataGridView1.Columns.Add(column0); dataGridView1.Columns.Add(column1); dataGridView1.RowCount =8; dataGridView1.AutoSize =true; dataGridView1.AllowUserToAddRows =false; dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; // Set the text for each checkBox. for (int i =0; i < dataGridView1.RowCount; i++) ...{ dataGridView1.Rows[i].Cells["DisableCheckBoxes"].Value = false; } dataGridView1.CellValueChanged += new DataGridViewCellEventHandler(dataGridView1_CellValueChanged); dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dataGridView1_CurrentCellDirtyStateChanged); dataGridView1.CellClick += new DataGridViewCellEventHandler(dataGridView1_CellClick); this.Controls.Add(dataGridView1); } // This event handler manually raises the CellValueChanged event // by calling the CommitEdit method. void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e) ...{ if (dataGridView1.IsCurrentCellDirty) ...{ dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit); } } // If a check box cell is clicked, this event handler disables // or enables the checkBox in the same row as the clicked cell. publicvoid dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e) ...{ if (dataGridView1.Columns[e.ColumnIndex].Name =="CheckBoxes") ...{ DataGridViewDisableCheckBoxCell checkBoxCell = (DataGridViewDisableCheckBoxCell)dataGridView1. Rows[e.RowIndex].Cells["DisableCheckBoxes"]; DataGridViewCheckBoxCell checkCell = (DataGridViewCheckBoxCell)dataGridView1. Rows[e.RowIndex].Cells["CheckBoxes"]; checkBoxCell.Enabled =!(Boolean)checkCell.Value; dataGridView1.Invalidate(); } } // If the user clicks on an enabled checkBox cell, this event handler // reports that the checkBox is enabled. void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) ...{ if (dataGridView1.Columns[e.ColumnIndex].Name =="DisableCheckBoxes") ...{ DataGridViewDisableCheckBoxCell checkBoxCell = (DataGridViewDisableCheckBoxCell)dataGridView1. Rows[e.RowIndex].Cells["DisableCheckBoxes"]; MessageBox.Show("checkBox #"+ e.RowIndex + " is "+ (checkBoxCell.Enabled ?"enabled" : "disabled")); // disallow changing the value of a disabled checkBox checkBoxCell.ReadOnly =!checkBoxCell.Enabled; } } } }
using System;using System.Collections.Generic;using System.Text;using System.Windows.Forms;using System.Drawing;using System.Windows.Forms.VisualStyles;namespace eBroker.Leo.W...{ public class Da