Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > working with DataGridViewCheckBoxCell
 

working with DataGridViewCheckBoxCell

Hi. I hope someone can assist. Here is the problem,

I have a datagrid view that has two bound columns and 1 unbound (which is the checkbox column).

I wanted to selected several default checkboxes when the form loads.

//selecting all rows for the example

This code will certainly CHECK each checkbox ....

dataGridView1.EditMode = DataGridViewEditMode.EditProgrammatically;
for (int i = 0; i < gridRows; i++)
{
dataGridView1.Select();
dataGridView1.CurrentCell = ((DataGridViewCheckBoxCell)( dataGridView1.RowsIdea.Cells[0]));
dataGridView1.BeginEdit(false);
((DataGridViewCheckBoxCell)(dataGridView1.RowsIdea.Cells[0])).Selected = true;
((DataGridViewCheckBoxCell)(dataGridView1.RowsIdea.Cells[0])).Value = true;

SendKeys.Send(" "); //this has been the only method I could find that would actually 'select' the checkbox
}

dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;


BUT, only the last column is being picked up (ie. the only one to show a value):

for (int i = 0; i < gridRows; i++)
{
DataGridViewRow dgvr = dataGridView1.RowsIdea;
DataGridViewCheckBoxCell chkCell = ((DataGridViewCheckBoxCell)(dgvr.Cells[0]));
if (((Boolean)(chkCell.EditingCellFormattedValue)))
{
studentStatusCode.Add(dgvr.Cells["studentStatusCodeDataGridViewTextBoxColumn"].Value.ToString());
}
}


Any help would be greatly appreciated.

wsharn  Thursday, February 21, 2008 11:49 PM

Hi wsharn,

Based on your post, you are now displaying data in DataGridView in a mixed mode (bound and unbound), then the unbound column in you DataGridView will not keep the value, and when click on the column header to force data to sort, the values in unbound column will get lost. This is because when cell is databound, the cell doesn’t “know�or keep the value at all. Anytime the cell’s value is needed the grid goes to the data source and looks up the value for the column and row and returns that as the cell’s value. But for the unbound columns there is no data source for them. To workaround this, we can enable the VirtuallMode of the DataGridView and use a custom data cache to store the data. See my sample below:

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private Dictionary<string, bool> store = new Dictionary<string, bool>();

private void Form2_Load(object sender, EventArgs e)

{

dt.Columns.Add("aa", typeof(int));

dt.Columns.Add("bb");

dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };

for (int i = 0; i < 10; i++)

{

dt.Rows.Add(i, "bbb" + i);

}

this.dataGridView1.DataSource = dt.DefaultView;

this.dataGridView1.Columns[0].ReadOnly = true;

this.dataGridView1.VirtualMode = true;

dataGridView1.Columns.Insert(0,new DataGridViewCheckBoxColumn());

dataGridView1.Columns[0].Frozen = true;

//here is the code to check all the checkboxes

for (int i = 0; i < this.dataGridView1.RowCount; i++)

{

if (this.dataGridView1[1, i].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, i].Value.ToString()))

store[this.dataGridView1[1, i].Value.ToString()] = true;

else

store.Add(this.dataGridView1[1, i].Value.ToString(), true);

}

}

this.dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);

this.dataGridView1.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

}

private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)

{

if (this.dataGridView1[1, e.RowIndex].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, e.RowIndex].Value.ToString()))

{

// Use the store if the e value has been modified and stored.

e.Value = store[this.dataGridView1[1, e.RowIndex].Value.ToString()];

}

else

{

e.Value = false;

}

}

}

private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)

{

if (this.dataGridView1[1, e.RowIndex].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, e.RowIndex].Value.ToString()))

store[this.dataGridView1[1, e.RowIndex].Value.ToString()] = bool.Parse(e.Value.ToString());

else

store.Add(this.dataGridView1[1, e.RowIndex].Value.ToString(), bool.Parse(e.Value.ToString()));

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, February 27, 2008 11:30 AM

Hi wsharn,

Based on your post, you are now displaying data in DataGridView in a mixed mode (bound and unbound), then the unbound column in you DataGridView will not keep the value, and when click on the column header to force data to sort, the values in unbound column will get lost. This is because when cell is databound, the cell doesn’t “know�or keep the value at all. Anytime the cell’s value is needed the grid goes to the data source and looks up the value for the column and row and returns that as the cell’s value. But for the unbound columns there is no data source for them. To workaround this, we can enable the VirtuallMode of the DataGridView and use a custom data cache to store the data. See my sample below:

Code Snippet

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

}

DataTable dt = new DataTable();

private Dictionary<string, bool> store = new Dictionary<string, bool>();

private void Form2_Load(object sender, EventArgs e)

{

dt.Columns.Add("aa", typeof(int));

dt.Columns.Add("bb");

dt.PrimaryKey = new DataColumn[] { dt.Columns[0] };

for (int i = 0; i < 10; i++)

{

dt.Rows.Add(i, "bbb" + i);

}

this.dataGridView1.DataSource = dt.DefaultView;

this.dataGridView1.Columns[0].ReadOnly = true;

this.dataGridView1.VirtualMode = true;

dataGridView1.Columns.Insert(0,new DataGridViewCheckBoxColumn());

dataGridView1.Columns[0].Frozen = true;

//here is the code to check all the checkboxes

for (int i = 0; i < this.dataGridView1.RowCount; i++)

{

if (this.dataGridView1[1, i].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, i].Value.ToString()))

store[this.dataGridView1[1, i].Value.ToString()] = true;

else

store.Add(this.dataGridView1[1, i].Value.ToString(), true);

}

}

this.dataGridView1.CellValueNeeded += new DataGridViewCellValueEventHandler(dataGridView1_CellValueNeeded);

this.dataGridView1.CellValuePushed += new DataGridViewCellValueEventHandler(dataGridView1_CellValuePushed);

}

private void dataGridView1_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)

{

if (this.dataGridView1[1, e.RowIndex].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, e.RowIndex].Value.ToString()))

{

// Use the store if the e value has been modified and stored.

e.Value = store[this.dataGridView1[1, e.RowIndex].Value.ToString()];

}

else

{

e.Value = false;

}

}

}

private void dataGridView1_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)

{

if (this.dataGridView1[1, e.RowIndex].Value != null)

{

if (store.ContainsKey(this.dataGridView1[1, e.RowIndex].Value.ToString()))

store[this.dataGridView1[1, e.RowIndex].Value.ToString()] = bool.Parse(e.Value.ToString());

else

store.Add(this.dataGridView1[1, e.RowIndex].Value.ToString(), bool.Parse(e.Value.ToString()));

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, February 27, 2008 11:30 AM
Thank a lot
Tiru.m  Friday, September 18, 2009 10:50 AM

You can use google to search for other answers

Custom Search

More Threads

• Help in datagridview Navigation
• Cannot enlarge the width of last column of DataGridView?
• Window Dates Source
• Using Old Designer by default in VS2005
• Can i set a variable in SQL for using that variable in VB
• mouse hover style???????????
• Problem with DataGrid column width
• Inserting records not working...2 DGVs, Master/Detail, ForeignKeyConstraint, bound to 1 dataset (2 tables)
• Windows Application to SQL Server Connection
• Using Preview Data window with guid parameters