8

I have a dataGridView in a Winform, I added to the datagrid a column with a checkbox using a code I saw here :

    DataGridViewCheckBoxColumn column = new DataGridViewCheckBoxColumn();
    {
        column.HeaderText = "Export";
        column.Name = "Export";
        column.AutoSizeMode =
            DataGridViewAutoSizeColumnMode.DisplayedCells;
        column.FlatStyle = FlatStyle.Standard;
        column.CellTemplate = new DataGridViewCheckBoxCell(false);
        column.CellTemplate.Style.BackColor = Color.White;
    }

    gStudyTable.Columns.Insert(0, column);  

this works but I want the checkBox to be checked as a default saw I added :

    foreach (DataGridViewRow row in gStudyTable.Rows)
    {                
        row.Cells[0].Value = true;
    }

but the checkbox col is still unchecked. I'm using a collection as my data source and I change the value of the col after I added the data source.

Sylca
  • 2,523
  • 4
  • 31
  • 51
meirrav
  • 761
  • 1
  • 9
  • 27
  • You could try changing the the data source's collection instead of the cell value itself. Something like `dataSourceCollection[0].Export = true` etc... – kor_ Dec 10 '12 at 06:56
  • the checkbox isn't part of my data source – meirrav Dec 10 '12 at 06:59
  • you can set checkbox value to true whenever your datagrid DataBindingComplete event raised : http://social.msdn.microsoft.com/Forums/en/csharplanguage/thread/2c7fe077-095e-472f-9833-c0633eb7035f – Mate Dec 10 '12 at 07:11
  • Related [Check/Uncheck a checkbox on datagridview](http://stackoverflow.com/q/13338837/1577396) – Mr_Green Dec 10 '12 at 08:11

8 Answers8

12

I think there is no way of setting the checked value on column declaration. You will have to iterate through the rows checking it after datasource is set (for example in DataBindingComplete event):

for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
dataGridView1.Rows[i].Cells[0].Value = true;
}

With your column name:

for (int i = 0; i < dataGridView1.Rows.Count -1; i++)
{
   dataGridView1.Rows[i].Cells["Export"].Value = true;
}
Daniel
  • 398
  • 2
  • 16
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82
  • 2
    It applies to all rows except the last one. Please Change the for loop to: for (int i = 0; i < dataGridView1.Rows.Count; i++), without -1 – dsmyrnaios Oct 24 '16 at 12:05
3

Try to do it like this:

foreach (DataGridViewRow row in dgv.Rows)     
{
    row.Cells[CheckBoxColumn.Name].Value = true;     
} 
Sylca
  • 2,523
  • 4
  • 31
  • 51
1

Make sure your DataGridView is shown when you set the value of your DataGridViewCheckBoxCells: I had mine in the second tab of a TabControl and the cells were always unchecked after initialization. To solve this, I had to move cell initialization to TabControl's SelectedIndexChanged event.

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (this.tabControl1.SelectedIndex == 1)
    {
        foreach (DataGridViewRow row in this.myGridView.Rows)
        {
            ((DataGridViewCheckBoxCell)row.Cells[0]).Value = true;
        }
    }
}
Sue Maurizio
  • 662
  • 7
  • 17
0

You can use datatable and create columns in datagridview and then add rows, provided first column value as 'True' or 'False' for each row.

try this

0

if ((bool)this.dataGridView2.Rows[i].Cells[0].FormattedValue == true)

0

During or after load the value in a grid, for check the value in grid use this code and set the properties of grid column

foreach (DataGridViewRow row in dataGridView.Rows)
{
    row.Cells[0].Value = 1;
}

enter image description here

daniele3004
  • 13,072
  • 12
  • 67
  • 75
0

Have a look at the properties within your .xsd file for the table format:

Table row properties

Then make sure you set the default value to something sensible:

set default data column property value

It will then automatically set the default value to the binding source.

Draken
  • 3,134
  • 13
  • 34
  • 54
Phill
  • 1
-2

An easy way is to use the property NewRowIndex as shown below:

dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[5].Value = true;
dvgInvoiceItems.Rows[dvgInvoiceItems.NewRowIndex-1].Cells[6].Value = true;
FelixSFD
  • 6,052
  • 10
  • 43
  • 117