Friday, April 15, 2011

Programatically loop through a DatagridView and check checkboxes

Hi ,

I have DataGridView bound by a datatable i have checkboxes to the same.

I want to navigate or loop through the the datagridview and check mark these checkboxes ,Below is the syntax i use .

foreach(DataGridViewRow dr in dgvColumns.Rows)
{
    DataGridViewCheckBoxCell checkCell =
        (DataGridViewCheckBoxCell)dr.Cells["CheckBoxes"];
    checkCell.Value=1;
    //Also tried checkCell.Selected=true;
    //Nothing seems to have worked.!
}
From stackoverflow
  • If it is bound to a DataTable, can you not work on the model (the table) instead? The DataGridView is a view...

    Try looping over the rows in the table, setting the values. For example (below) - note that I don't update the DataGridView - just the DataTable:

    using System;
    using System.Data;
    using System.Windows.Forms;
    
    static class Program
    {
        [STAThread]
        static void Main()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Selected", typeof(bool));
            table.Rows.Add("Fred", false);
            table.Rows.Add("Jo", false);
            table.Rows.Add("Andy", true);
    
            Button btn = new Button();
            btn.Text = "Select all";
            btn.Dock = DockStyle.Bottom;
            btn.Click += delegate
            {
                foreach (DataRow row in table.Rows)
                {
                    row["Selected"] = true;
                }
            };
    
            DataGridView grid = new DataGridView();
            grid.Dock = DockStyle.Fill;
            grid.DataSource = table;
    
            Form form = new Form();
            form.Controls.Add(grid);
            form.Controls.Add(btn);
            Application.Run(form);
        }
    }
    
    Marc Gravell : If it is data-bound, then changing the *bound* value should fix this too, I believe.
  • Something along the lines of:

    foreach(DataGridViewRow dgvr in dgvColumns.Rows)
    {
        // Get the underlying datarow
        DataRow dr = ((DataRowView)dgvr.DataBoundItem).Row;
    
        // Update the appropriate column in the data row.
        // Assuming this is your column name in your 
        // underlying data table
        dr["CheckBoxes"] = 1;
    }
    
    Justin Bannister : Sorry, the cast was wrong. I have amended the code above.
    Justin Bannister : Is the combo box data bound?
    Justin Bannister : Assuming you have populated the combo with data by adding items. Simply get a reference to the DataGridComboBoxCell and set the value of the item. But the item must be in the list.
  • The following worked for me, it checked the checkboxes perfectly :)

    foreach (DataGridViewRow row in dgvDataGridView.Rows)

         {
                 ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
    
          }
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.