Windows Develop Bookmark and Share   
 index > Windows Forms General > About Checkbox column in datagridview
 

About Checkbox column in datagridview

Hello Every body

My name is ramesh.

i have one problem i am facing in datagridview in c# windows application.

i haveadded Name column is of type texbox, another column with name Showrecord of typeCheckbox.

at the run timeiam adding only selected rowsfrom the datatable based on some condition to datagridview.

when i am trying to select the Showrecord the value is remains false only irrespective of wheather i checked

or unchecked but in the formdatagridviewit showscorrect only, ie when i checked checkbox in the grid is checked and vice versa. i have tryed selction change event,

dataGridView1_CellContentClick event but no use

example code

--------------------

private void Form1_Load(object sender, EventArgs e)

{

string[] s = new string[] { "ramesh","false" };// just for example only

dataGridView1.Rows.Add(s);

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

if (Boolean.Parse(dataGridView1.Rows[0].Cells[0].Value.ToString()) == true)

{

MessageBox.Show(dataGridView1.Rows[e.ColumnIndex].Cells[e.ColumnIndex].Value.ToString());

//Value is always false only

}

}

this event is fired when ever i change the state of check box in the datagridview

i Have also use

private void dataGridView1_SelectionChanged(object sender, EventArgs e)

{

if (Boolean.Parse(dataGridView1.Rows[0].Cells[0].Value.ToString()) == true)

{

MessageBox.Show(dataGridView1.Rows[1].Cells[1].Value.ToString());

}

}

but this event is fired only when i changed one cell to another

plz i want the soltion ASAP

plz............

Thank You

Regards

Ramesh

Rameshb106  Friday, November 30, 2007 3:00 PM

Hi Ramesh,

Based on your post, the cell’s value always return false even the check box is checked when clicking on the cell’s content, right? When the DataGridView is not bound to data, the editing cell won’t commit the value until you move to another. If you want to get the cell’s value in a editing cell, you need submit the value manually. Try something like the following:

Code Block

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

if (dataGridView1.IsCurrentCellDirty)

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

if (Boolean.Parse(dataGridView1.Rows[0].Cells[1].Value.ToString()) == true)

{

MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

}

}

Hope this helps
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, December 05, 2007 8:18 AM

Hi Ramesh,

Based on your post, the cell’s value always return false even the check box is checked when clicking on the cell’s content, right? When the DataGridView is not bound to data, the editing cell won’t commit the value until you move to another. If you want to get the cell’s value in a editing cell, you need submit the value manually. Try something like the following:

Code Block

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

if (dataGridView1.IsCurrentCellDirty)

{

dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);

}

if (Boolean.Parse(dataGridView1.Rows[0].Cells[1].Value.ToString()) == true)

{

MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

}

}

Hope this helps
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Wednesday, December 05, 2007 8:18 AM

Hello Sir

Thank You For your valuable information

its working from Your code.

Now another my proble in same senerio is i want to make a checked checkbox in

datagridview as uncheck based on some condition(ie if the user selects fist row and it is mandatatory that he has to select the next row which is same as first row if he not select that then ) i want to uncheck that using code.

i tryed by doing reverse engineering like making the isdirty proprtey true but it is thriuging an error its is read only

sample code

routenumber = "rrt";

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

if (dataGridView1.IsCurrentCellDirty)

DataGridViewDataErrorContexts.Commit);

if (Boolean.Parse(dataGridView1.Rows[0].Cells[1].Value.ToString()) == true)

MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

DataGridViewDataErrorContexts.Commit);

You can use google to search for other answers

Custom Search

More Threads

• Re: Windows Forms GUI standards
• How to upload file using WebBrowser class?
• Label Tooltip not showing when AutoEllipsis=true
• control.Dispose()
• Getting a handle to a MDIClient
• Persist ChildForm in an MDI Form - Can This Be Done?
• Keyboard Hook System-Wide Problem
• How to force delete a directory/file?
• Split Container
• .net 2.0 scheduled task