views:

2191

answers:

3

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.!
}
+1  A: 

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
Im sorry .I do not understand how can i work on the model ?
Thank you Marc for your code is it posible for me to select a value from the combo box which is in the datagridview?
If it is data-bound, then changing the *bound* value should fix this too, I believe.
Marc Gravell
+1  A: 

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
((DataGridRowView)dgvr.DataBoundItem).Row Row there is no property like te row?
Sorry, the cast was wrong. I have amended the code above.
Justin Bannister
I also have a comboBox in the datagridView and also want to set it to some value ? How is that done ?
Is the combo box data bound?
Justin Bannister
No added seperately ,Actually there are 5 columns in the datagridview 1.CheckBox(Not Databound!)2. TextBox(Bound)3.ComboBox(not databound)4.ComboBox(not databound)5.TextBox(Not databound!)
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.
Justin Bannister
A: 

The following worked for me, it checked the checkboxes perfectly :)

foreach (DataGridViewRow row in dgvDataGridView.Rows)

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

      }