In datagridview if we want to show some of the checkboxes in checkbox column as disabled one, below method can be used. Logic: Add an disable checkbox image to resources. In cell Painting event of Datagridview draw this disable checkbox image inside the cell.
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e) { //If first column is the checkbox column if (e.ColumnIndex == 0) { //Get the image from resource Image imgDisabled = (Image)Properties.Resources._disChecked; for (int i = 0; i < e.RowIndex; i++) {
try { //Clear the content in that cell using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { e.Graphics.FillRectangle(backColorBrush, e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height); }
////Draw 1 bottom line... e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1); //Draw right line... e.Graphics.DrawLine(Pens.DarkGray, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); ////Draw left line... //Get the X and Y co-ordinates int X = e.CellBounds.Left + ((e.CellBounds.Width - imgDisabled.Width) / 2); int Y = e.CellBounds.Top + ((e.CellBounds.Height - imgDisabled.Height) / 2); //Draw the image e.Graphics.DrawImage(imgDisabled, X, Y); e.Handled = true; } catch(Exception ex) {
} } } }
|
No responses found. Be the first to respond and make money from revenue sharing program.
|