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. How do I initialize the checkbox column?

How do I initialize the checkbox column?

Scheduled Pinned Locked Moved C#
question
10 Posts 2 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.
  • P Offline
    P Offline
    printscreen12345
    wrote on last edited by
    #1

    I have inserted a datagridviewcheckboxcolumn. But the values of those the checkbox cells are all set to null by default. How do I change them all to "false" which is unchecked?

    N 1 Reply Last reply
    0
    • P printscreen12345

      I have inserted a datagridviewcheckboxcolumn. But the values of those the checkbox cells are all set to null by default. How do I change them all to "false" which is unchecked?

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello Is your DataGridView databound?? In this case you could just use this line of code to return false to the datasource when the user leaves the value null: dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false; If you still want to initialize the values of the cells, I remember something about default values in DatagridView, but i don't seem to recall it at the moment.. What I can think of now is to handle the RowsAdded and initialize the new cells value to false.

      Regards:rose:

      P 1 Reply Last reply
      0
      • N Nader Elshehabi

        Hello Is your DataGridView databound?? In this case you could just use this line of code to return false to the datasource when the user leaves the value null: dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false; If you still want to initialize the values of the cells, I remember something about default values in DatagridView, but i don't seem to recall it at the moment.. What I can think of now is to handle the RowsAdded and initialize the new cells value to false.

        Regards:rose:

        P Offline
        P Offline
        printscreen12345
        wrote on last edited by
        #3

        Hi I have set the datagridview.datasource to the defaultview of a datatable. Is it databound then? The thing is that the checkbox column is inserted to the datagridview with null values by default. The idea is to let the user to check for export. Cos the unchecked boxes are of null values, I've got an error when the null value is accessed.

        N 1 Reply Last reply
        0
        • P printscreen12345

          Hi I have set the datagridview.datasource to the defaultview of a datatable. Is it databound then? The thing is that the checkbox column is inserted to the datagridview with null values by default. The idea is to let the user to check for export. Cos the unchecked boxes are of null values, I've got an error when the null value is accessed.

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello I provided you with two possible solutions in my last post. Are they not suitable?? 1- You could ignore the null value and return false to the datasource. This should eleminate the error:

          dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false;

          2-Otherwise you could handle the RowsAdded event and set the value of your column for each new row to false

          Regards:rose:

          P 1 Reply Last reply
          0
          • N Nader Elshehabi

            Hello I provided you with two possible solutions in my last post. Are they not suitable?? 1- You could ignore the null value and return false to the datasource. This should eleminate the error:

            dataGridView1.Columns[0].DefaultCellStyle.DataSourceNullValue = false;

            2-Otherwise you could handle the RowsAdded event and set the value of your column for each new row to false

            Regards:rose:

            P Offline
            P Offline
            printscreen12345
            wrote on last edited by
            #5

            Hi I've tried both but they don't seem to work. It could be that the datagridview is not databound??? For the 2nd solution, it seemed to work initially, but I don't know why the value switched back to null again. I have made the datagridview a global variable, so nothing should have changed the value back. Now I don't know what to do.

            N 1 Reply Last reply
            0
            • P printscreen12345

              Hi I've tried both but they don't seem to work. It could be that the datagridview is not databound??? For the 2nd solution, it seemed to work initially, but I don't know why the value switched back to null again. I have made the datagridview a global variable, so nothing should have changed the value back. Now I don't know what to do.

              N Offline
              N Offline
              Nader Elshehabi
              wrote on last edited by
              #6

              Hello

              printscreen12345 wrote:

              I don't know why the value switched back to null again.

              What??!!:wtf: What do you mean switched back again?!! Are you sure you did initialize them to false?? Also, how do you populate your DataGridView if it's not databound?

              Regards:rose:

              P 1 Reply Last reply
              0
              • N Nader Elshehabi

                Hello

                printscreen12345 wrote:

                I don't know why the value switched back to null again.

                What??!!:wtf: What do you mean switched back again?!! Are you sure you did initialize them to false?? Also, how do you populate your DataGridView if it's not databound?

                Regards:rose:

                P Offline
                P Offline
                printscreen12345
                wrote on last edited by
                #7

                Hi Yes, I did initialise them to false in the "try" block and they switched back to null in the "finally" block. I'm not sure if the DataGridView is databound or not. As mentioned in my earlier messsage, I have set dataGridView1.DataSource to a defaultView of a datatable. Is this databound??

                N 1 Reply Last reply
                0
                • P printscreen12345

                  Hi Yes, I did initialise them to false in the "try" block and they switched back to null in the "finally" block. I'm not sure if the DataGridView is databound or not. As mentioned in my earlier messsage, I have set dataGridView1.DataSource to a defaultView of a datatable. Is this databound??

                  N Offline
                  N Offline
                  Nader Elshehabi
                  wrote on last edited by
                  #8

                  Hello Yes this is databound, as long as your DataGridView is populated automatically via the view. now let's work this out step by step.. 1-Does your try throws an exception?? I mean do you catch anything in the catch block?? 2- After you change the value to false, make a break point and watch the value of the last row to be changed.

                  Regards:rose:

                  P 1 Reply Last reply
                  0
                  • N Nader Elshehabi

                    Hello Yes this is databound, as long as your DataGridView is populated automatically via the view. now let's work this out step by step.. 1-Does your try throws an exception?? I mean do you catch anything in the catch block?? 2- After you change the value to false, make a break point and watch the value of the last row to be changed.

                    Regards:rose:

                    P Offline
                    P Offline
                    printscreen12345
                    wrote on last edited by
                    #9

                    Hi, There is no exception thrown at all. It goes straight into the finally block from the last line of code in the try block. I set a break point at the last line of code in the try block and watched the value of the 1st 10 rows of the checkboxcolumn. They were all set to false. But when it stepped into the next line of code (which is the "}" of the try block), they all turned back to null value. This means, it is at the end of the try block that they were switched back. I just realised one thing. The datagridview is displayed when I press a search button. It is very strange that the false values are all switched back to null only the first time when the search button is pressed. They all remained set to false the second time onwards. Actually it has the same kind of problem with the row numbers display - it will never display the row numbers for the first time but it will from the 2nd time onward. Strange?!! Thanks -- modified at 21:12 Tuesday 29th August, 2006

                    N 1 Reply Last reply
                    0
                    • P printscreen12345

                      Hi, There is no exception thrown at all. It goes straight into the finally block from the last line of code in the try block. I set a break point at the last line of code in the try block and watched the value of the 1st 10 rows of the checkboxcolumn. They were all set to false. But when it stepped into the next line of code (which is the "}" of the try block), they all turned back to null value. This means, it is at the end of the try block that they were switched back. I just realised one thing. The datagridview is displayed when I press a search button. It is very strange that the false values are all switched back to null only the first time when the search button is pressed. They all remained set to false the second time onwards. Actually it has the same kind of problem with the row numbers display - it will never display the row numbers for the first time but it will from the 2nd time onward. Strange?!! Thanks -- modified at 21:12 Tuesday 29th August, 2006

                      N Offline
                      N Offline
                      Nader Elshehabi
                      wrote on last edited by
                      #10

                      Hello

                      printscreen12345 wrote:

                      Strange?!!

                      Well, errors like these in Visual Studio are what keeps Borland on business;P Anyway, My last question would be: Do you put the false value directly into the cells, or do you put some sort of a local reference field in the try block to hold it?? If you put it in a local field it will turn to null once the block is finished, otherwise really... I'm out of guesses.

                      printscreen12345 wrote:

                      it is at the end of the try block that they were switched back.

                      I guess I'd pass out if I see something like that while debugging??:-D

                      Regards:rose:

                      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