checkbox in a data grid view....
-
I want that check box in a datagridview should act like a radio button.Means whenever I select a checkbox it will be selected and rest of all would be deselect. I have used cellContentClick event of the datagrid view.Structure of the datagridview is like- 1st column is checkbox and 2nd is text box.following is the code i have used- for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (i == e.RowIndex) { dataGridView1.Rows[i].Cells[0].Value = true; } else { dataGridView1.Rows[i].Cells[0].Value = false; } } The above code runs good when i select another checkbox.But when one checkbox is selected/checked and now I again click on the same checkbox then it is deselected/unchecked.I want when the same checkbox is checked again it won't unchecked and remian checked. How can I do this? Thanks in adv....
-
I want that check box in a datagridview should act like a radio button.Means whenever I select a checkbox it will be selected and rest of all would be deselect. I have used cellContentClick event of the datagrid view.Structure of the datagridview is like- 1st column is checkbox and 2nd is text box.following is the code i have used- for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (i == e.RowIndex) { dataGridView1.Rows[i].Cells[0].Value = true; } else { dataGridView1.Rows[i].Cells[0].Value = false; } } The above code runs good when i select another checkbox.But when one checkbox is selected/checked and now I again click on the same checkbox then it is deselected/unchecked.I want when the same checkbox is checked again it won't unchecked and remian checked. How can I do this? Thanks in adv....
If you must use checkboxes and not radiobuttons, then you could add something like the following to your call. You will of course want to add some better validation.
for( int i = 0; i < dataGridView1.Rows.Count; i++ ) { CheckBox ctl = (CheckBox)dataGridView1.Rows\[i\].FindControl( "CheckBoxControlName" ); if( ctl != null ) { ctl.Checked = ( i == e.RowIndex ); ctl.Enabled = !( i == e.RowIndex ); } }
This would simply disable the currently selected checkbox while allowing any other to be selected. (NOTE: I just hammered this out in the editor. If I have a syntax error or typo... sorry.)
modified on Wednesday, October 13, 2010 2:50 PM
-
I want that check box in a datagridview should act like a radio button.Means whenever I select a checkbox it will be selected and rest of all would be deselect. I have used cellContentClick event of the datagrid view.Structure of the datagridview is like- 1st column is checkbox and 2nd is text box.following is the code i have used- for (int i = 0; i < dataGridView1.Rows.Count; i++) { if (i == e.RowIndex) { dataGridView1.Rows[i].Cells[0].Value = true; } else { dataGridView1.Rows[i].Cells[0].Value = false; } } The above code runs good when i select another checkbox.But when one checkbox is selected/checked and now I again click on the same checkbox then it is deselected/unchecked.I want when the same checkbox is checked again it won't unchecked and remian checked. How can I do this? Thanks in adv....
You can do that by changing readonly property as below.
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
if (i == e.RowIndex)
{
dataGridView1.Rows[i].Cells[0].Value = true;
dataGridView1.Rows[i].Cells[0].ReadOnly = true;
}
else
{
dataGridView1.Rows[i].Cells[0].Value = false;
dataGridView1.Rows[i].Cells[0].ReadOnly = false;
}
}
}