1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DataGridViewCheckBoxCsharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
//Ajout 3 colonnes
AddColorColumn();
//Mettre False comme valeur par defaut pour tous les
//Checkbox
//Ajout 10 clients
for( int i =0;i<=10;i++ )
{
if( i == 3 || i == 5)
{
this.DataGridView1.Rows.Add("ClientWithoutCheck", false, 1000.0 * i);
}
else
{
this.DataGridView1.Rows.Add("ClientWithCheck", false, 2000.0 * i);
}
}
}
private void AddColorColumn()
{
//Ajout 1ere colonne DataGridViewTextBoxColumn
DataGridViewTextBoxColumn textBoxColumn1 = new DataGridViewTextBoxColumn();
textBoxColumn1.HeaderText = "Client";
textBoxColumn1.ValueType =typeof(String);
this.DataGridView1.Columns.Add(textBoxColumn1);
//Ajout 2eme colonne DataGridViewCheckBoxColumn
DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
checkBoxColumn.HeaderText = "myCheckBox";
checkBoxColumn.ValueType = typeof(Boolean);
checkBoxColumn.Name = "myCheckBox";
this.DataGridView1.Columns.Add(checkBoxColumn);
//'Ajout 3eme colonne DataGridViewTextBoxColumn
DataGridViewTextBoxColumn textBoxColumn2 =new DataGridViewTextBoxColumn();
textBoxColumn2.HeaderText = "Price";
textBoxColumn2.ValueType = typeof(Double);
this.DataGridView1.Columns.Add(textBoxColumn2);
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (this.DataGridView1.Columns["myCheckBox"].Index ==
e.ColumnIndex && e.RowIndex >= 0 )
{
if ((double)this.DataGridView1.Rows[e.RowIndex].Cells[2].Value == 3000.0 | (double)this.DataGridView1.Rows[e.RowIndex].Cells[2].Value == 5000.0)
{
Rectangle newRect = new Rectangle(e.CellBounds.X + 1,
e.CellBounds.Y + 1, e.CellBounds.Width - 4,
e.CellBounds.Height - 4);
using (
Brush gridBrush = new SolidBrush(this.DataGridView1.GridColor),
backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
using (Pen gridLinePen = new Pen(gridBrush))
{
// Erase the cell.
e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
// Draw the grid lines (only the right and bottom lines;
// DataGridView takes care of the others).
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
e.CellBounds.Bottom - 1, e.CellBounds.Right - 1,
e.CellBounds.Bottom - 1);
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1,
e.CellBounds.Top, e.CellBounds.Right - 1,
e.CellBounds.Bottom);
// Draw the inset highlight box.
e.Graphics.DrawRectangle(Pens.Blue, newRect);
//commented. Draw the text content of the cell, ignoring alignment.
//if (e.Value != null)
//{
// e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
// Brushes.Crimson, e.CellBounds.X + 2,
// e.CellBounds.Y + 2, StringFormat.GenericDefault);
//}
//Le texte doit etre mis à blanc sinon il peint
//false
if (e.Value != null)
{
e.Graphics.DrawString("", e.CellStyle.Font,
Brushes.Crimson, e.CellBounds.X + 2,
e.CellBounds.Y + 2, StringFormat.GenericDefault);
}
e.Handled = true;
}
}
}
}
}
//on teste le ckeck - uncheck du CheckBox quand on clique dessus
//car la peinture ne suffit pas à elle seule
private void DataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
String str = this.DataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
MessageBox.Show(str);
}
}
} |
Partager