Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. checkbox in a data grid view....

checkbox in a data grid view....

Scheduled Pinned Locked Moved C#
questioncssjson
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jainiraj
    wrote on last edited by
    #1

    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....

    F E 2 Replies Last reply
    0
    • J jainiraj

      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....

      F Offline
      F Offline
      fjdiewornncalwe
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • J jainiraj

        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....

        E Offline
        E Offline
        ekba89
        wrote on last edited by
        #3

        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;
        }
        }
        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups