In a data grid view I need to loop on the rows and get the rows that contain a checked checkbox dgv.rows[i].cells[0].value is returning empty in the both cases all this is happening on the event CellContentClick
Asked
Active
Viewed 1,348 times
0
-
After I try a lot of thing I found that the problem is with the event because when I use another event the value is true. any Idea about another event that fire when I change the check of the checkbox? – Mario Feb 21 '11 at 09:41
-
You need to post some code. We've all provided valid answers based on the information you've provided. Without the relevant code, there's not much else we can do. – Metro Smurf Feb 21 '11 at 15:07
3 Answers
0
Try:
'VB
Dim MyCheckBox As CheckBox = _
CType(dgv.rows[i].cells[0].findcontrol("checkbox_id"), CheckBox)
C#:
//C#
CheckBox MyCheckBox =
dgv.Rows[i].Cells[0].FindControl("checkbox_id") as CheckBox;
The Value property on a cell refers to text content when the cell doesn't contain any other controls.

mellamokb
- 56,094
- 12
- 110
- 136
-
the question is in C# code and your answers are for VB for info you don't have to use findControl you can use ((checkbox)dgv.rows[ind].cells[0].children[0]) – Mario Feb 17 '11 at 14:54
0
If the checkbox does not contain any data, the result will be a null value. You can parse the value in your loop with a bool.Parse()
assuming the value isn't null, i.e.,
for ( int i = 0; i < dgv.Rows.Count; i++ )
{
var val = dgv.Rows[i].Cells[0].Value;
if ( val == null ) { continue; }
bool isChecked = bool.Parse( val.ToString() );
}

Metro Smurf
- 37,266
- 20
- 108
- 140
-
Metro I already said that dgv.Rows[i].Cells[0].Value is not working it return an empty string in both cases – Mario Feb 18 '11 at 09:20
-
@Mario - you also said `dgv.rows[ind].cells[0].children[0]` was a valid statement (refer to your comment to @mellamokb); there is no such property named `Children` on the `Cells` property. If the value is an empty string, then whatever you're using for binding is setting the value to empty. You should post all the relevant code. – Metro Smurf Feb 18 '11 at 15:03
-
please to read carefully the comment the property children exist in the **VB code** – Mario Feb 21 '11 at 09:38
0
static class DataGridViewExtensions
{
public static IEnumerable<DataGridViewRow> CheckedRows(this DataGridView dgv, string checkedColumnName)
{
return CheckedRows(dgv, dgv.Columns[checkedColumnName].Index);
}
public static IEnumerable<DataGridViewRow> CheckedRows(this DataGridView dgv, int checkedColumnIndex)
{
foreach (DataGridViewRow row in dgv.Rows)
{
DataGridViewCheckBoxCell cell = row.Cells[checkedColumnIndex] as DataGridViewCheckBoxCell;
Debug.Assert(cell != null, "The column specified is not a check box column");
if (cell != null && (bool)cell.Value)
yield return row;
}
}
}

Tergiver
- 14,171
- 3
- 41
- 68