| Author: sonali 09 Aug 2007 | Member Level: Bronze | Rating:  Points: 2 |
hi hope this will help u this code will help u to even check and uncheck the check box property
using System; using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles;
class Form1 : Form { private DataGridView dataGridView1 = new DataGridView();
[STAThread] public static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); }
public Form1() { this.AutoSize = true; this.Load += new EventHandler(Form1_Load); }
public void 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.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. public void 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(); } }
SONAL
|
| Author: abirami 09 Aug 2007 | Member Level: Gold | Rating:  Points: 2 |
Thank u very much. its veryusefull for me.
|
| Author: dhirander Kumar 24 Dec 2008 | Member Level: Bronze | Rating:  Points: 0 |
This is really very helpful..... Thanx a lot sonal
|