DataGridView checkbox column - value and functionality












16















I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.



Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:



1) How do I make it so that the whole column is "checked" by default?
2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?



Here's the code to get the column inserted:



DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
doWork.HeaderText = "Include Dog";
doWork.FalseValue = "0";
doWork.TrueValue = "1";
dataGridView1.Columns.Insert(0, doWork);


So what next?
Any help would be greatly appreciated!










share|improve this question





























    16















    I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.



    Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:



    1) How do I make it so that the whole column is "checked" by default?
    2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?



    Here's the code to get the column inserted:



    DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
    doWork.HeaderText = "Include Dog";
    doWork.FalseValue = "0";
    doWork.TrueValue = "1";
    dataGridView1.Columns.Insert(0, doWork);


    So what next?
    Any help would be greatly appreciated!










    share|improve this question



























      16












      16








      16


      6






      I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.



      Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:



      1) How do I make it so that the whole column is "checked" by default?
      2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?



      Here's the code to get the column inserted:



      DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
      doWork.HeaderText = "Include Dog";
      doWork.FalseValue = "0";
      doWork.TrueValue = "1";
      dataGridView1.Columns.Insert(0, doWork);


      So what next?
      Any help would be greatly appreciated!










      share|improve this question
















      I've added a checkbox column to a DataGridView in my C# form. The function needs to be dynamic - you select a customer and that brings up all of their items that could be serviced, and you select which of them you wish to be serviced this time around.



      Anyway, the code will now add a chckbox to the beginning of the DGV. What I need to know is the following:



      1) How do I make it so that the whole column is "checked" by default?
      2) How can I make sure I'm only getting values from the "checked" rows when I click on a button just below the DGV?



      Here's the code to get the column inserted:



      DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
      doWork.HeaderText = "Include Dog";
      doWork.FalseValue = "0";
      doWork.TrueValue = "1";
      dataGridView1.Columns.Insert(0, doWork);


      So what next?
      Any help would be greatly appreciated!







      c# winforms datagridview checkbox






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 24 '13 at 5:30









      Community

      11




      11










      asked Aug 6 '09 at 9:17









      David ArcherDavid Archer

      60661634




      60661634
























          9 Answers
          9






          active

          oldest

          votes


















          37
















          1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:



            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
            row.Cells[CheckBoxColumn1.Name].Value = true;
            }



          2. The Click event might look something like this:



            private void button1_Click(object sender, EventArgs e)
            {
            List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
            if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
            {
            rows_with_checked_column.Add(row);
            }
            }
            // Do what you want with the check rows
            }







          share|improve this answer





















          • 1





            Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

            – David Archer
            Aug 6 '09 at 12:47











          • Actually, never mind that, I've found a way to do it. Thanks again for your help!

            – David Archer
            Aug 6 '09 at 13:15






          • 1





            Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

            – SwDevMan81
            Aug 7 '09 at 11:28











          • if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

            – PUG
            Aug 29 '12 at 17:17



















          14














          private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
          {
          DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
          ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

          if (ch1.Value == null)
          ch1.Value=false;
          switch (ch1.Value.ToString())
          {
          case "True":
          ch1.Value = false;
          break;
          case "False":
          ch1.Value = true;
          break;
          }
          MessageBox.Show(ch1.Value.ToString());
          }


          best solution to find if the checkbox in the datagridview is checked or not.






          share|improve this answer


























          • Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

            – illagrenan
            Apr 17 '12 at 20:40













          • Thanks for the great answer!

            – Sourav Sarkar
            Aug 1 '13 at 15:53






          • 1





            thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

            – Abbas Palash
            Mar 19 '15 at 9:13













          • Working fine... ;)

            – 0_0
            Mar 26 '15 at 14:53



















          4














          Here's a one liner answer for this question



          List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();





          share|improve this answer
























          • +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

            – Jeremy Thompson
            Mar 9 '12 at 3:16











          • i see but in mine it works, your welcome dude.

            – Rob
            Mar 9 '12 at 7:06



















          3














          it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:



          First you add a new .cs File to your project with a custom-checkbox cell, e.g.



          DataGridViewCheckboxCellFilter.cs:



          using System.Windows.Forms;

          namespace MyNamespace {
          public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
          public DataGridViewCheckboxCellFilter() : base() {
          this.FalseValue = 0;
          this.TrueValue = 1;
          this.Value = TrueValue;
          }
          }
          }


          After this, on your GridView, where you add the checkbox-column, you do:



          // add checkboxes
          DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
          {
          col_chkbox.HeaderText = "X";
          col_chkbox.Name = "checked";
          col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
          }
          this.Columns.Add(col_chkbox);


          And that's it! Everytime your checkboxes get added in a new row, they'll be set to true.
          Enjoy!






          share|improve this answer
























          • I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

            – Jack Fairfield
            Jun 22 '16 at 16:51











          • This is the answer to the question. Thank you.

            – EllieK
            Feb 15 '17 at 20:52





















          3














          If you try it on CellContentClick Event



          Use:



          dataGridView1.EndEdit();  //Stop editing of cell.
          MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());





          share|improve this answer

































            1














            If you have a gridview containing more than one checkbox ....
            you should try this ....



            Object o=new Object[6];

            for (int i = 0; i < dgverlist.RowCount; i++)
            {
            for (int j = 2; j < dgverlist.ColumnCount; j++)
            {
            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
            ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

            if (ch1.Value != null)
            {
            o[i] = ch1.OwningColumn.HeaderText.ToString();

            MessageBox.Show(o[i].ToString());
            }
            }
            }





            share|improve this answer

































              0














              To test if the column is checked or not:



              for (int i = 0; i < dgvName.Rows.Count; i++)
              {
              if ((bool)dgvName.Rows[i].Cells[8].Value)
              {
              // Column is checked
              }
              }





              share|improve this answer































                0














                1) How do I make it so that the whole column is "checked" by default?



                var doWork = new DataGridViewCheckBoxColumn();
                doWork.Name = "IncludeDog" //Added so you can find the column in a row
                doWork.HeaderText = "Include Dog";
                doWork.FalseValue = "0";
                doWork.TrueValue = "1";

                //Make the default checked
                doWork.CellTemplate.Value = true;
                doWork.CellTemplate.Style.NullValue = true;

                dataGridView1.Columns.Insert(0, doWork);


                2) How can I make sure I'm only getting values from the "checked" rows?



                foreach (DataGridViewRow row in dataGridView1.Rows)
                {
                if (row.IsNewRow) continue;//If editing is enabled, skip the new row

                //The Cell's Value gets it wrong with the true default, it will return
                //false until the cell changes so use FormattedValue instead.
                if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
                {
                //Do stuff with row
                }
                }





                share|improve this answer































                  0














                  if u make this column in sql database (bit) as a data type u should edit this code



                  DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                  doWork.HeaderText = "Include Dog";
                  doWork.FalseValue = "0";
                  doWork.TrueValue = "1";
                  dataGridView1.Columns.Insert(0, doWork);


                  with this



                  DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                  doWork.HeaderText = "Include Dog";
                  doWork.FalseValue = "False";
                  doWork.TrueValue = "True";
                  dataGridView1.Columns.Insert(0, doWork);





                  share|improve this answer























                    Your Answer






                    StackExchange.ifUsing("editor", function () {
                    StackExchange.using("externalEditor", function () {
                    StackExchange.using("snippets", function () {
                    StackExchange.snippets.init();
                    });
                    });
                    }, "code-snippets");

                    StackExchange.ready(function() {
                    var channelOptions = {
                    tags: "".split(" "),
                    id: "1"
                    };
                    initTagRenderer("".split(" "), "".split(" "), channelOptions);

                    StackExchange.using("externalEditor", function() {
                    // Have to fire editor after snippets, if snippets enabled
                    if (StackExchange.settings.snippets.snippetsEnabled) {
                    StackExchange.using("snippets", function() {
                    createEditor();
                    });
                    }
                    else {
                    createEditor();
                    }
                    });

                    function createEditor() {
                    StackExchange.prepareEditor({
                    heartbeatType: 'answer',
                    autoActivateHeartbeat: false,
                    convertImagesToLinks: true,
                    noModals: true,
                    showLowRepImageUploadWarning: true,
                    reputationToPostImages: 10,
                    bindNavPrevention: true,
                    postfix: "",
                    imageUploader: {
                    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                    allowUrls: true
                    },
                    onDemand: true,
                    discardSelector: ".discard-answer"
                    ,immediatelyShowMarkdownHelp:true
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1237829%2fdatagridview-checkbox-column-value-and-functionality%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    9 Answers
                    9






                    active

                    oldest

                    votes








                    9 Answers
                    9






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes









                    37
















                    1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:



                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      row.Cells[CheckBoxColumn1.Name].Value = true;
                      }



                    2. The Click event might look something like this:



                      private void button1_Click(object sender, EventArgs e)
                      {
                      List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
                      {
                      rows_with_checked_column.Add(row);
                      }
                      }
                      // Do what you want with the check rows
                      }







                    share|improve this answer





















                    • 1





                      Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                      – David Archer
                      Aug 6 '09 at 12:47











                    • Actually, never mind that, I've found a way to do it. Thanks again for your help!

                      – David Archer
                      Aug 6 '09 at 13:15






                    • 1





                      Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                      – SwDevMan81
                      Aug 7 '09 at 11:28











                    • if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                      – PUG
                      Aug 29 '12 at 17:17
















                    37
















                    1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:



                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      row.Cells[CheckBoxColumn1.Name].Value = true;
                      }



                    2. The Click event might look something like this:



                      private void button1_Click(object sender, EventArgs e)
                      {
                      List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
                      {
                      rows_with_checked_column.Add(row);
                      }
                      }
                      // Do what you want with the check rows
                      }







                    share|improve this answer





















                    • 1





                      Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                      – David Archer
                      Aug 6 '09 at 12:47











                    • Actually, never mind that, I've found a way to do it. Thanks again for your help!

                      – David Archer
                      Aug 6 '09 at 13:15






                    • 1





                      Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                      – SwDevMan81
                      Aug 7 '09 at 11:28











                    • if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                      – PUG
                      Aug 29 '12 at 17:17














                    37












                    37








                    37









                    1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:



                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      row.Cells[CheckBoxColumn1.Name].Value = true;
                      }



                    2. The Click event might look something like this:



                      private void button1_Click(object sender, EventArgs e)
                      {
                      List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
                      {
                      rows_with_checked_column.Add(row);
                      }
                      }
                      // Do what you want with the check rows
                      }







                    share|improve this answer

















                    1. There is no way to do that directly. Once you have your data in the grid, you can loop through the rows and check each box like this:



                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      row.Cells[CheckBoxColumn1.Name].Value = true;
                      }



                    2. The Click event might look something like this:



                      private void button1_Click(object sender, EventArgs e)
                      {
                      List<DataGridViewRow> rows_with_checked_column = new List<DataGridViewRow>();
                      foreach (DataGridViewRow row in dataGridView1.Rows)
                      {
                      if (Convert.ToBoolean(row.Cells[CheckBoxColumn1.Name].Value) == true)
                      {
                      rows_with_checked_column.Add(row);
                      }
                      }
                      // Do what you want with the check rows
                      }








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Oct 9 '15 at 8:25









                    slavoo

                    3,978122834




                    3,978122834










                    answered Aug 6 '09 at 12:17









                    SwDevMan81SwDevMan81

                    38.9k18125165




                    38.9k18125165








                    • 1





                      Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                      – David Archer
                      Aug 6 '09 at 12:47











                    • Actually, never mind that, I've found a way to do it. Thanks again for your help!

                      – David Archer
                      Aug 6 '09 at 13:15






                    • 1





                      Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                      – SwDevMan81
                      Aug 7 '09 at 11:28











                    • if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                      – PUG
                      Aug 29 '12 at 17:17














                    • 1





                      Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                      – David Archer
                      Aug 6 '09 at 12:47











                    • Actually, never mind that, I've found a way to do it. Thanks again for your help!

                      – David Archer
                      Aug 6 '09 at 13:15






                    • 1





                      Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                      – SwDevMan81
                      Aug 7 '09 at 11:28











                    • if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                      – PUG
                      Aug 29 '12 at 17:17








                    1




                    1





                    Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                    – David Archer
                    Aug 6 '09 at 12:47





                    Thanks so much! That's really useful, but just one thing... when I get to that point, to get the information from the checked rows, how would I get the information from a specific cell (e.g. the cell value in column 2 of all checked cells) Also... you really seem to know your stuff for C#, any books you can recommend? Thanks.

                    – David Archer
                    Aug 6 '09 at 12:47













                    Actually, never mind that, I've found a way to do it. Thanks again for your help!

                    – David Archer
                    Aug 6 '09 at 13:15





                    Actually, never mind that, I've found a way to do it. Thanks again for your help!

                    – David Archer
                    Aug 6 '09 at 13:15




                    1




                    1





                    Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                    – SwDevMan81
                    Aug 7 '09 at 11:28





                    Glad you found it out. As for a book to recommend, I can't say that I know of any for learning C#. I do use the msdn (msdn.microsoft.com/en-us/library/ms229335.aspx) website a lot for looking up methods/properties/descriptions/examples/etc, so I would say thats probably the best reference, oh an SO too ;)

                    – SwDevMan81
                    Aug 7 '09 at 11:28













                    if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                    – PUG
                    Aug 29 '12 at 17:17





                    if you click the check box 2,3 times consecutively, it gives exception could not convert Checked to bool. Initially it shows value as true then it changes it to checked.

                    – PUG
                    Aug 29 '12 at 17:17













                    14














                    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
                    {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

                    if (ch1.Value == null)
                    ch1.Value=false;
                    switch (ch1.Value.ToString())
                    {
                    case "True":
                    ch1.Value = false;
                    break;
                    case "False":
                    ch1.Value = true;
                    break;
                    }
                    MessageBox.Show(ch1.Value.ToString());
                    }


                    best solution to find if the checkbox in the datagridview is checked or not.






                    share|improve this answer


























                    • Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                      – illagrenan
                      Apr 17 '12 at 20:40













                    • Thanks for the great answer!

                      – Sourav Sarkar
                      Aug 1 '13 at 15:53






                    • 1





                      thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                      – Abbas Palash
                      Mar 19 '15 at 9:13













                    • Working fine... ;)

                      – 0_0
                      Mar 26 '15 at 14:53
















                    14














                    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
                    {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

                    if (ch1.Value == null)
                    ch1.Value=false;
                    switch (ch1.Value.ToString())
                    {
                    case "True":
                    ch1.Value = false;
                    break;
                    case "False":
                    ch1.Value = true;
                    break;
                    }
                    MessageBox.Show(ch1.Value.ToString());
                    }


                    best solution to find if the checkbox in the datagridview is checked or not.






                    share|improve this answer


























                    • Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                      – illagrenan
                      Apr 17 '12 at 20:40













                    • Thanks for the great answer!

                      – Sourav Sarkar
                      Aug 1 '13 at 15:53






                    • 1





                      thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                      – Abbas Palash
                      Mar 19 '15 at 9:13













                    • Working fine... ;)

                      – 0_0
                      Mar 26 '15 at 14:53














                    14












                    14








                    14







                    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
                    {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

                    if (ch1.Value == null)
                    ch1.Value=false;
                    switch (ch1.Value.ToString())
                    {
                    case "True":
                    ch1.Value = false;
                    break;
                    case "False":
                    ch1.Value = true;
                    break;
                    }
                    MessageBox.Show(ch1.Value.ToString());
                    }


                    best solution to find if the checkbox in the datagridview is checked or not.






                    share|improve this answer















                    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
                    {
                    DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                    ch1 = (DataGridViewCheckBoxCell)dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[0];

                    if (ch1.Value == null)
                    ch1.Value=false;
                    switch (ch1.Value.ToString())
                    {
                    case "True":
                    ch1.Value = false;
                    break;
                    case "False":
                    ch1.Value = true;
                    break;
                    }
                    MessageBox.Show(ch1.Value.ToString());
                    }


                    best solution to find if the checkbox in the datagridview is checked or not.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Dec 1 '11 at 1:15









                    Kate Gregory

                    17.5k74881




                    17.5k74881










                    answered Aug 29 '10 at 17:39









                    NazeerNazeer

                    14112




                    14112













                    • Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                      – illagrenan
                      Apr 17 '12 at 20:40













                    • Thanks for the great answer!

                      – Sourav Sarkar
                      Aug 1 '13 at 15:53






                    • 1





                      thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                      – Abbas Palash
                      Mar 19 '15 at 9:13













                    • Working fine... ;)

                      – 0_0
                      Mar 26 '15 at 14:53



















                    • Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                      – illagrenan
                      Apr 17 '12 at 20:40













                    • Thanks for the great answer!

                      – Sourav Sarkar
                      Aug 1 '13 at 15:53






                    • 1





                      thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                      – Abbas Palash
                      Mar 19 '15 at 9:13













                    • Working fine... ;)

                      – 0_0
                      Mar 26 '15 at 14:53

















                    Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                    – illagrenan
                    Apr 17 '12 at 20:40







                    Thanks, works great! To handle event only when cell has checkbox: // .... DataGridView dgv = (DataGridView)sender; if (dgv.CurrentCell.GetType() != typeof(DataGridViewCheckBoxCell)) { return; } // ...

                    – illagrenan
                    Apr 17 '12 at 20:40















                    Thanks for the great answer!

                    – Sourav Sarkar
                    Aug 1 '13 at 15:53





                    Thanks for the great answer!

                    – Sourav Sarkar
                    Aug 1 '13 at 15:53




                    1




                    1





                    thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                    – Abbas Palash
                    Mar 19 '15 at 9:13







                    thanks @Nazeer just a little tweak to your code snippet if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value; instead switch (ch1.Value.ToString()) { case "True": ch1.Value = false; break; case "False": ch1.Value = true; break; } a compact version if (ch1.Value == null) ch1.Value = false; ch1.Value = !(bool)ch1.Value;

                    – Abbas Palash
                    Mar 19 '15 at 9:13















                    Working fine... ;)

                    – 0_0
                    Mar 26 '15 at 14:53





                    Working fine... ;)

                    – 0_0
                    Mar 26 '15 at 14:53











                    4














                    Here's a one liner answer for this question



                    List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();





                    share|improve this answer
























                    • +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                      – Jeremy Thompson
                      Mar 9 '12 at 3:16











                    • i see but in mine it works, your welcome dude.

                      – Rob
                      Mar 9 '12 at 7:06
















                    4














                    Here's a one liner answer for this question



                    List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();





                    share|improve this answer
























                    • +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                      – Jeremy Thompson
                      Mar 9 '12 at 3:16











                    • i see but in mine it works, your welcome dude.

                      – Rob
                      Mar 9 '12 at 7:06














                    4












                    4








                    4







                    Here's a one liner answer for this question



                    List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();





                    share|improve this answer













                    Here's a one liner answer for this question



                    List<DataGridViewRow> list = DataGridView1.Rows.Cast<DataGridViewRow>().Where(k => Convert.ToBoolean(k.Cells[CheckBoxColumn1.Name].Value) == true).ToList();






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 24 '12 at 6:37









                    RobRob

                    3932829




                    3932829













                    • +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                      – Jeremy Thompson
                      Mar 9 '12 at 3:16











                    • i see but in mine it works, your welcome dude.

                      – Rob
                      Mar 9 '12 at 7:06



















                    • +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                      – Jeremy Thompson
                      Mar 9 '12 at 3:16











                    • i see but in mine it works, your welcome dude.

                      – Rob
                      Mar 9 '12 at 7:06

















                    +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                    – Jeremy Thompson
                    Mar 9 '12 at 3:16





                    +1 just what I was looking for Rob, Thanks. Note: I was getting a Object not set DBNUll exception, so I changed your code to this: '//find the selected rows - in one line of Lamba goodness - without Deferred Execution! List<DataGridViewRow> selectedSeriesCodes = gridSeriesList.Rows.Cast<DataGridViewRow>().Where(g => !string.IsNullOrEmpty(g.Cells["Selected"].Value.ToString()) && Convert.ToBoolean(g.Cells["Selected"].Value) == true).ToList();'

                    – Jeremy Thompson
                    Mar 9 '12 at 3:16













                    i see but in mine it works, your welcome dude.

                    – Rob
                    Mar 9 '12 at 7:06





                    i see but in mine it works, your welcome dude.

                    – Rob
                    Mar 9 '12 at 7:06











                    3














                    it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:



                    First you add a new .cs File to your project with a custom-checkbox cell, e.g.



                    DataGridViewCheckboxCellFilter.cs:



                    using System.Windows.Forms;

                    namespace MyNamespace {
                    public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
                    public DataGridViewCheckboxCellFilter() : base() {
                    this.FalseValue = 0;
                    this.TrueValue = 1;
                    this.Value = TrueValue;
                    }
                    }
                    }


                    After this, on your GridView, where you add the checkbox-column, you do:



                    // add checkboxes
                    DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
                    {
                    col_chkbox.HeaderText = "X";
                    col_chkbox.Name = "checked";
                    col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
                    }
                    this.Columns.Add(col_chkbox);


                    And that's it! Everytime your checkboxes get added in a new row, they'll be set to true.
                    Enjoy!






                    share|improve this answer
























                    • I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                      – Jack Fairfield
                      Jun 22 '16 at 16:51











                    • This is the answer to the question. Thank you.

                      – EllieK
                      Feb 15 '17 at 20:52


















                    3














                    it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:



                    First you add a new .cs File to your project with a custom-checkbox cell, e.g.



                    DataGridViewCheckboxCellFilter.cs:



                    using System.Windows.Forms;

                    namespace MyNamespace {
                    public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
                    public DataGridViewCheckboxCellFilter() : base() {
                    this.FalseValue = 0;
                    this.TrueValue = 1;
                    this.Value = TrueValue;
                    }
                    }
                    }


                    After this, on your GridView, where you add the checkbox-column, you do:



                    // add checkboxes
                    DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
                    {
                    col_chkbox.HeaderText = "X";
                    col_chkbox.Name = "checked";
                    col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
                    }
                    this.Columns.Add(col_chkbox);


                    And that's it! Everytime your checkboxes get added in a new row, they'll be set to true.
                    Enjoy!






                    share|improve this answer
























                    • I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                      – Jack Fairfield
                      Jun 22 '16 at 16:51











                    • This is the answer to the question. Thank you.

                      – EllieK
                      Feb 15 '17 at 20:52
















                    3












                    3








                    3







                    it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:



                    First you add a new .cs File to your project with a custom-checkbox cell, e.g.



                    DataGridViewCheckboxCellFilter.cs:



                    using System.Windows.Forms;

                    namespace MyNamespace {
                    public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
                    public DataGridViewCheckboxCellFilter() : base() {
                    this.FalseValue = 0;
                    this.TrueValue = 1;
                    this.Value = TrueValue;
                    }
                    }
                    }


                    After this, on your GridView, where you add the checkbox-column, you do:



                    // add checkboxes
                    DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
                    {
                    col_chkbox.HeaderText = "X";
                    col_chkbox.Name = "checked";
                    col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
                    }
                    this.Columns.Add(col_chkbox);


                    And that's it! Everytime your checkboxes get added in a new row, they'll be set to true.
                    Enjoy!






                    share|improve this answer













                    it took me a long time to figure out how to do this without having to loop through all the records. I have a bound datagridview-source, and all fields are bound except for the checkbox-column. So I don't have/need a loop to add each row and I didn't want to create one just for this purpuse. So after a lot of trying I finally got it. And it's actually very simple too:



                    First you add a new .cs File to your project with a custom-checkbox cell, e.g.



                    DataGridViewCheckboxCellFilter.cs:



                    using System.Windows.Forms;

                    namespace MyNamespace {
                    public class DataGridViewCheckboxCellFilter : DataGridViewCheckBoxCell {
                    public DataGridViewCheckboxCellFilter() : base() {
                    this.FalseValue = 0;
                    this.TrueValue = 1;
                    this.Value = TrueValue;
                    }
                    }
                    }


                    After this, on your GridView, where you add the checkbox-column, you do:



                    // add checkboxes
                    DataGridViewCheckBoxColumn col_chkbox = new DataGridViewCheckBoxColumn();
                    {
                    col_chkbox.HeaderText = "X";
                    col_chkbox.Name = "checked";
                    col_chkbox.CellTemplate = new DataGridViewCheckboxCellFilter();
                    }
                    this.Columns.Add(col_chkbox);


                    And that's it! Everytime your checkboxes get added in a new row, they'll be set to true.
                    Enjoy!







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered May 22 '11 at 23:35









                    andzepandzep

                    1,3991630




                    1,3991630













                    • I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                      – Jack Fairfield
                      Jun 22 '16 at 16:51











                    • This is the answer to the question. Thank you.

                      – EllieK
                      Feb 15 '17 at 20:52





















                    • I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                      – Jack Fairfield
                      Jun 22 '16 at 16:51











                    • This is the answer to the question. Thank you.

                      – EllieK
                      Feb 15 '17 at 20:52



















                    I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                    – Jack Fairfield
                    Jun 22 '16 at 16:51





                    I like this implementation best. Seems like the fastest performance wise. You don't have to do any looping.

                    – Jack Fairfield
                    Jun 22 '16 at 16:51













                    This is the answer to the question. Thank you.

                    – EllieK
                    Feb 15 '17 at 20:52







                    This is the answer to the question. Thank you.

                    – EllieK
                    Feb 15 '17 at 20:52













                    3














                    If you try it on CellContentClick Event



                    Use:



                    dataGridView1.EndEdit();  //Stop editing of cell.
                    MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());





                    share|improve this answer






























                      3














                      If you try it on CellContentClick Event



                      Use:



                      dataGridView1.EndEdit();  //Stop editing of cell.
                      MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());





                      share|improve this answer




























                        3












                        3








                        3







                        If you try it on CellContentClick Event



                        Use:



                        dataGridView1.EndEdit();  //Stop editing of cell.
                        MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());





                        share|improve this answer















                        If you try it on CellContentClick Event



                        Use:



                        dataGridView1.EndEdit();  //Stop editing of cell.
                        MessageBox.Show("0 = " + dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Oct 3 '12 at 17:03









                        Marko

                        15k103859




                        15k103859










                        answered Oct 3 '12 at 14:24









                        Atul JainAtul Jain

                        311




                        311























                            1














                            If you have a gridview containing more than one checkbox ....
                            you should try this ....



                            Object o=new Object[6];

                            for (int i = 0; i < dgverlist.RowCount; i++)
                            {
                            for (int j = 2; j < dgverlist.ColumnCount; j++)
                            {
                            DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                            ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

                            if (ch1.Value != null)
                            {
                            o[i] = ch1.OwningColumn.HeaderText.ToString();

                            MessageBox.Show(o[i].ToString());
                            }
                            }
                            }





                            share|improve this answer






























                              1














                              If you have a gridview containing more than one checkbox ....
                              you should try this ....



                              Object o=new Object[6];

                              for (int i = 0; i < dgverlist.RowCount; i++)
                              {
                              for (int j = 2; j < dgverlist.ColumnCount; j++)
                              {
                              DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                              ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

                              if (ch1.Value != null)
                              {
                              o[i] = ch1.OwningColumn.HeaderText.ToString();

                              MessageBox.Show(o[i].ToString());
                              }
                              }
                              }





                              share|improve this answer




























                                1












                                1








                                1







                                If you have a gridview containing more than one checkbox ....
                                you should try this ....



                                Object o=new Object[6];

                                for (int i = 0; i < dgverlist.RowCount; i++)
                                {
                                for (int j = 2; j < dgverlist.ColumnCount; j++)
                                {
                                DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                                ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

                                if (ch1.Value != null)
                                {
                                o[i] = ch1.OwningColumn.HeaderText.ToString();

                                MessageBox.Show(o[i].ToString());
                                }
                                }
                                }





                                share|improve this answer















                                If you have a gridview containing more than one checkbox ....
                                you should try this ....



                                Object o=new Object[6];

                                for (int i = 0; i < dgverlist.RowCount; i++)
                                {
                                for (int j = 2; j < dgverlist.ColumnCount; j++)
                                {
                                DataGridViewCheckBoxCell ch1 = new DataGridViewCheckBoxCell();
                                ch1 = (DataGridViewCheckBoxCell)dgverlist.Rows[i].Cells[j];

                                if (ch1.Value != null)
                                {
                                o[i] = ch1.OwningColumn.HeaderText.ToString();

                                MessageBox.Show(o[i].ToString());
                                }
                                }
                                }






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Feb 16 '12 at 6:58









                                Skuld

                                1,64222333




                                1,64222333










                                answered Feb 16 '12 at 6:29









                                Amin MansuriAmin Mansuri

                                111




                                111























                                    0














                                    To test if the column is checked or not:



                                    for (int i = 0; i < dgvName.Rows.Count; i++)
                                    {
                                    if ((bool)dgvName.Rows[i].Cells[8].Value)
                                    {
                                    // Column is checked
                                    }
                                    }





                                    share|improve this answer




























                                      0














                                      To test if the column is checked or not:



                                      for (int i = 0; i < dgvName.Rows.Count; i++)
                                      {
                                      if ((bool)dgvName.Rows[i].Cells[8].Value)
                                      {
                                      // Column is checked
                                      }
                                      }





                                      share|improve this answer


























                                        0












                                        0








                                        0







                                        To test if the column is checked or not:



                                        for (int i = 0; i < dgvName.Rows.Count; i++)
                                        {
                                        if ((bool)dgvName.Rows[i].Cells[8].Value)
                                        {
                                        // Column is checked
                                        }
                                        }





                                        share|improve this answer













                                        To test if the column is checked or not:



                                        for (int i = 0; i < dgvName.Rows.Count; i++)
                                        {
                                        if ((bool)dgvName.Rows[i].Cells[8].Value)
                                        {
                                        // Column is checked
                                        }
                                        }






                                        share|improve this answer












                                        share|improve this answer



                                        share|improve this answer










                                        answered Jan 12 '13 at 6:25









                                        RooziRoozi

                                        37535




                                        37535























                                            0














                                            1) How do I make it so that the whole column is "checked" by default?



                                            var doWork = new DataGridViewCheckBoxColumn();
                                            doWork.Name = "IncludeDog" //Added so you can find the column in a row
                                            doWork.HeaderText = "Include Dog";
                                            doWork.FalseValue = "0";
                                            doWork.TrueValue = "1";

                                            //Make the default checked
                                            doWork.CellTemplate.Value = true;
                                            doWork.CellTemplate.Style.NullValue = true;

                                            dataGridView1.Columns.Insert(0, doWork);


                                            2) How can I make sure I'm only getting values from the "checked" rows?



                                            foreach (DataGridViewRow row in dataGridView1.Rows)
                                            {
                                            if (row.IsNewRow) continue;//If editing is enabled, skip the new row

                                            //The Cell's Value gets it wrong with the true default, it will return
                                            //false until the cell changes so use FormattedValue instead.
                                            if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
                                            {
                                            //Do stuff with row
                                            }
                                            }





                                            share|improve this answer




























                                              0














                                              1) How do I make it so that the whole column is "checked" by default?



                                              var doWork = new DataGridViewCheckBoxColumn();
                                              doWork.Name = "IncludeDog" //Added so you can find the column in a row
                                              doWork.HeaderText = "Include Dog";
                                              doWork.FalseValue = "0";
                                              doWork.TrueValue = "1";

                                              //Make the default checked
                                              doWork.CellTemplate.Value = true;
                                              doWork.CellTemplate.Style.NullValue = true;

                                              dataGridView1.Columns.Insert(0, doWork);


                                              2) How can I make sure I'm only getting values from the "checked" rows?



                                              foreach (DataGridViewRow row in dataGridView1.Rows)
                                              {
                                              if (row.IsNewRow) continue;//If editing is enabled, skip the new row

                                              //The Cell's Value gets it wrong with the true default, it will return
                                              //false until the cell changes so use FormattedValue instead.
                                              if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
                                              {
                                              //Do stuff with row
                                              }
                                              }





                                              share|improve this answer


























                                                0












                                                0








                                                0







                                                1) How do I make it so that the whole column is "checked" by default?



                                                var doWork = new DataGridViewCheckBoxColumn();
                                                doWork.Name = "IncludeDog" //Added so you can find the column in a row
                                                doWork.HeaderText = "Include Dog";
                                                doWork.FalseValue = "0";
                                                doWork.TrueValue = "1";

                                                //Make the default checked
                                                doWork.CellTemplate.Value = true;
                                                doWork.CellTemplate.Style.NullValue = true;

                                                dataGridView1.Columns.Insert(0, doWork);


                                                2) How can I make sure I'm only getting values from the "checked" rows?



                                                foreach (DataGridViewRow row in dataGridView1.Rows)
                                                {
                                                if (row.IsNewRow) continue;//If editing is enabled, skip the new row

                                                //The Cell's Value gets it wrong with the true default, it will return
                                                //false until the cell changes so use FormattedValue instead.
                                                if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
                                                {
                                                //Do stuff with row
                                                }
                                                }





                                                share|improve this answer













                                                1) How do I make it so that the whole column is "checked" by default?



                                                var doWork = new DataGridViewCheckBoxColumn();
                                                doWork.Name = "IncludeDog" //Added so you can find the column in a row
                                                doWork.HeaderText = "Include Dog";
                                                doWork.FalseValue = "0";
                                                doWork.TrueValue = "1";

                                                //Make the default checked
                                                doWork.CellTemplate.Value = true;
                                                doWork.CellTemplate.Style.NullValue = true;

                                                dataGridView1.Columns.Insert(0, doWork);


                                                2) How can I make sure I'm only getting values from the "checked" rows?



                                                foreach (DataGridViewRow row in dataGridView1.Rows)
                                                {
                                                if (row.IsNewRow) continue;//If editing is enabled, skip the new row

                                                //The Cell's Value gets it wrong with the true default, it will return
                                                //false until the cell changes so use FormattedValue instead.
                                                if (Convert.ToBoolean(row.Cells["IncludeDog"].FormattedValue))
                                                {
                                                //Do stuff with row
                                                }
                                                }






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Oct 30 '14 at 5:31









                                                DarkChowderDarkChowder

                                                12




                                                12























                                                    0














                                                    if u make this column in sql database (bit) as a data type u should edit this code



                                                    DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                    doWork.HeaderText = "Include Dog";
                                                    doWork.FalseValue = "0";
                                                    doWork.TrueValue = "1";
                                                    dataGridView1.Columns.Insert(0, doWork);


                                                    with this



                                                    DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                    doWork.HeaderText = "Include Dog";
                                                    doWork.FalseValue = "False";
                                                    doWork.TrueValue = "True";
                                                    dataGridView1.Columns.Insert(0, doWork);





                                                    share|improve this answer




























                                                      0














                                                      if u make this column in sql database (bit) as a data type u should edit this code



                                                      DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                      doWork.HeaderText = "Include Dog";
                                                      doWork.FalseValue = "0";
                                                      doWork.TrueValue = "1";
                                                      dataGridView1.Columns.Insert(0, doWork);


                                                      with this



                                                      DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                      doWork.HeaderText = "Include Dog";
                                                      doWork.FalseValue = "False";
                                                      doWork.TrueValue = "True";
                                                      dataGridView1.Columns.Insert(0, doWork);





                                                      share|improve this answer


























                                                        0












                                                        0








                                                        0







                                                        if u make this column in sql database (bit) as a data type u should edit this code



                                                        DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                        doWork.HeaderText = "Include Dog";
                                                        doWork.FalseValue = "0";
                                                        doWork.TrueValue = "1";
                                                        dataGridView1.Columns.Insert(0, doWork);


                                                        with this



                                                        DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                        doWork.HeaderText = "Include Dog";
                                                        doWork.FalseValue = "False";
                                                        doWork.TrueValue = "True";
                                                        dataGridView1.Columns.Insert(0, doWork);





                                                        share|improve this answer













                                                        if u make this column in sql database (bit) as a data type u should edit this code



                                                        DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                        doWork.HeaderText = "Include Dog";
                                                        doWork.FalseValue = "0";
                                                        doWork.TrueValue = "1";
                                                        dataGridView1.Columns.Insert(0, doWork);


                                                        with this



                                                        DataGridViewCheckBoxColumn doWork = new DataGridViewCheckBoxColumn();
                                                        doWork.HeaderText = "Include Dog";
                                                        doWork.FalseValue = "False";
                                                        doWork.TrueValue = "True";
                                                        dataGridView1.Columns.Insert(0, doWork);






                                                        share|improve this answer












                                                        share|improve this answer



                                                        share|improve this answer










                                                        answered Jun 1 '16 at 23:10









                                                        Ahmed Soliman FlashaAhmed Soliman Flasha

                                                        16218




                                                        16218






























                                                            draft saved

                                                            draft discarded




















































                                                            Thanks for contributing an answer to Stack Overflow!


                                                            • Please be sure to answer the question. Provide details and share your research!

                                                            But avoid



                                                            • Asking for help, clarification, or responding to other answers.

                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                            To learn more, see our tips on writing great answers.




                                                            draft saved


                                                            draft discarded














                                                            StackExchange.ready(
                                                            function () {
                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f1237829%2fdatagridview-checkbox-column-value-and-functionality%23new-answer', 'question_page');
                                                            }
                                                            );

                                                            Post as a guest















                                                            Required, but never shown





















































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown

































                                                            Required, but never shown














                                                            Required, but never shown












                                                            Required, but never shown







                                                            Required, but never shown







                                                            Popular posts from this blog

                                                            MongoDB - Not Authorized To Execute Command

                                                            How to fix TextFormField cause rebuild widget in Flutter

                                                            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith