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. Accessing CheckBox on row in Datagridview

Accessing CheckBox on row in Datagridview

Scheduled Pinned Locked Moved C#
helpquestion
10 Posts 4 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.
  • S Offline
    S Offline
    Smith
    wrote on last edited by
    #1

    Why on earth it's so difficult to find a sample on the web for this topic? Nobody has ever tried putting a checkbox on a datagridview on a winform? Or my search is wrong? This thread's subject has been my search key. Gettin nothin. What's wrong with the below code? DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[0].Cells[0]; if (chk.Selected) { MessageBox.Show("Selected!"); } It's always checking the "cells" selected state. I want to know if the check box has been selected(Checked!) or not. Yes, I added column 0 to be a DataGridViewCheckBoxCell. Anybody can help?

    :beer:

    D 1 Reply Last reply
    0
    • S Smith

      Why on earth it's so difficult to find a sample on the web for this topic? Nobody has ever tried putting a checkbox on a datagridview on a winform? Or my search is wrong? This thread's subject has been my search key. Gettin nothin. What's wrong with the below code? DataGridViewCheckBoxCell chk = (DataGridViewCheckBoxCell)dataGridView1.Rows[0].Cells[0]; if (chk.Selected) { MessageBox.Show("Selected!"); } It's always checking the "cells" selected state. I want to know if the check box has been selected(Checked!) or not. Yes, I added column 0 to be a DataGridViewCheckBoxCell. Anybody can help?

      :beer:

      D Offline
      D Offline
      dan sh
      wrote on last edited by
      #2

      The Value property would give the checked state of the cell. :) BTW, instead of casting, you can use as keyword along with the null check.

      S 1 Reply Last reply
      0
      • D dan sh

        The Value property would give the checked state of the cell. :) BTW, instead of casting, you can use as keyword along with the null check.

        S Offline
        S Offline
        Smith
        wrote on last edited by
        #3

        How do I access it's value? The below attempt is successfuly throwing a cast exception. if (CheckState.Checked == (CheckState)dataGridView1.Rows[0].Cells[0].Value) { MessageBox.Show("ok"); }

        :beer:

        L D 2 Replies Last reply
        0
        • S Smith

          How do I access it's value? The below attempt is successfuly throwing a cast exception. if (CheckState.Checked == (CheckState)dataGridView1.Rows[0].Cells[0].Value) { MessageBox.Show("ok"); }

          :beer:

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          so look at dataGridView1.Rows[0].Cells[0].Value.ToString() to discover what might be wrong. everything responds well to ToString(), except null of course. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read formatted code with indentation, so please use PRE tags for code snippets.


          I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


          S 1 Reply Last reply
          0
          • S Smith

            How do I access it's value? The below attempt is successfuly throwing a cast exception. if (CheckState.Checked == (CheckState)dataGridView1.Rows[0].Cells[0].Value) { MessageBox.Show("ok"); }

            :beer:

            D Offline
            D Offline
            dan sh
            wrote on last edited by
            #5

            The property Value will have value as true or false. Try casting that to boolean.

            S 1 Reply Last reply
            0
            • L Luc Pattyn

              so look at dataGridView1.Rows[0].Cells[0].Value.ToString() to discover what might be wrong. everything responds well to ToString(), except null of course. :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read formatted code with indentation, so please use PRE tags for code snippets.


              I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


              S Offline
              S Offline
              Smith
              wrote on last edited by
              #6

              Something's troubling. It requires me to "check" the checkbox at least once before checking it's VALUE to stop it from whining. If I call dataGridView1.Rows[0].Cells[0].Value.ToString() straight, without touching the grid, it's throwing NULL object exception.

              :beer:

              L 1 Reply Last reply
              0
              • D dan sh

                The property Value will have value as true or false. Try casting that to boolean.

                S Offline
                S Offline
                Smith
                wrote on last edited by
                #7

                Please check my reply to Luc.

                :beer:

                D 1 Reply Last reply
                0
                • S Smith

                  Something's troubling. It requires me to "check" the checkbox at least once before checking it's VALUE to stop it from whining. If I call dataGridView1.Rows[0].Cells[0].Value.ToString() straight, without touching the grid, it's throwing NULL object exception.

                  :beer:

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  Smith# wrote:

                  It requires me to "check" the checkbox at least once before checking it's VALUE

                  Not quite. There must be many ways to make Value hold an appropriate value, or to overcome a possible null. :)

                  Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                  I only read formatted code with indentation, so please use PRE tags for code snippets.


                  I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).


                  1 Reply Last reply
                  0
                  • S Smith

                    Please check my reply to Luc.

                    :beer:

                    D Offline
                    D Offline
                    Dr Walt Fair PE
                    wrote on last edited by
                    #9

                    Essentially .Value can have 3 values in the case of a CheckBox: true, false and null. Whenever you deal with DataGridView cell values, you need to check for null. Whether it's a CheckBox cell or not, accessing a null is bad.

                    CQ de W5ALT

                    Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                    S 1 Reply Last reply
                    0
                    • D Dr Walt Fair PE

                      Essentially .Value can have 3 values in the case of a CheckBox: true, false and null. Whenever you deal with DataGridView cell values, you need to check for null. Whether it's a CheckBox cell or not, accessing a null is bad.

                      CQ de W5ALT

                      Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                      S Offline
                      S Offline
                      Smith
                      wrote on last edited by
                      #10

                      Hi Walt Fair, thanks for your reply. Well,I'm still not convinced with the replies. The grid has been drawn on the screen, the rows have been created, the cells are displayed beautifully with checkboxes. Then why the "Value" has to be NULL? and why it's not NULL after I check the cell once?

                      :beer:

                      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