DataGridView checkbox column - value and functionality

后端 未结 11 1687
一向
一向 2020-11-28 12:17

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 s

相关标签:
11条回答
  • 2020-11-28 12:47

    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!

    0 讨论(0)
  • 2020-11-28 12:52

    It is quite simple

    DataGridViewCheckBoxCell checkedCell = (DataGridViewCheckBoxCell) grdData.Rows[e.RowIndex].Cells["grdChkEnable"];
        
    bool isEnabled = false;
    if (checkedCell.AccessibilityObject.State.HasFlag(AccessibleStates.Checked))
    {
        isEnabled = true;
    }
    if (isEnabled)
    {
       // do your business process;
    }
    
    0 讨论(0)
  • 2020-11-28 12:54
    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
      }
      
    0 讨论(0)
  • 2020-11-28 12:55

    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());
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-28 12:55

    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);
    
    0 讨论(0)
提交回复
热议问题