0

I want to check all/ only certain Checkboxes as checked. I tried various versions from stackoverflow but none seem to work out. The code is called directly after dynamically creating the datagrid as I only want to load the Data once. - Datagrid is created in my Form_Load The value of the Checkboxes are changed but not displayed.

//This is how i create the Datagrid column - not question relevant

for (int kacnt = 1; kacnt <= Ei.Kaanzahl; kacnt++)
  {
    DataGridViewCheckBoxColumn Kachk = new DataGridViewCheckBoxColumn();
    Kachk.HeaderText = "Kamera" + kacnt;
    Kachk.Width = 70;
    WarDataGridView.Columns.Add(Kachk);
  }

// The code I actually have problems with - the display of the value

       foreach (DataGridViewRow row in WarDataGridView.Rows)
  {
    for (int col = 1; col < WarDataGridView.ColumnCount; col++)
    {
     (WarDataGridView.Rows[row.Index].Cells[col] as DataGridViewCheckBoxCell).Value = true;}}
NayNay
  • 95
  • 1
  • 1
  • 6
  • It's unclear what you are asking. Please consider posting a [MCVE] to reproduce the problem and describe the problem and expected result. – Reza Aghaei Dec 27 '18 at 15:06
  • Can you also mention event or method, in which you are trying to update DataGridView source? You will need to refresh once datagridview source is being updated.. try following stackoverflow discussion,, https://stackoverflow.com/questions/7008361/how-can-i-refresh-c-sharp-datagridview-after-update – Hitesh Gaur Dec 27 '18 at 15:26
  • What is the column index of the column you want to check? You are starting at index 1 which means you are skipping the first column (index 0). Also, if your checkbox column is hidden, you need to use `WarDataGridView.Columns.Count` in your for loop. `WarDataGridView.ColumnCount` only returns displayed columns. – Smitty-Werben-Jager-Manjenson Dec 27 '18 at 15:33
  • Edited the above things.. I only want to check displayed columns and yes i am skipping the first column intentionally since it is a Text Column.. Thanks a lot tho – NayNay Dec 27 '18 at 15:36
  • Are you using a datasource, i.e. `WarDataGridView.DataSource = dataSource_dataTable`? I've noticed the data isn't finished loading immediately after databinding. In that case you need a `WarDataGridView.DataBindingComplete` event, and set the columns from there. You also may need to set the particular column with `Kamerachk.TrueValue = true` and `Kamerachk .FalseValue = false` – Smitty-Werben-Jager-Manjenson Dec 27 '18 at 15:42
  • am not using a Datasource but will try that out. Thanks a lot – NayNay Dec 27 '18 at 15:43
  • It worked out!! Thank you a lot - would love to mark your answer it as THE answer. – NayNay Dec 27 '18 at 15:54
  • I'll post as an answer... Just click the check mark to accept as the answer. You can upvote the comment as well if you'd like. – Smitty-Werben-Jager-Manjenson Dec 27 '18 at 15:56

1 Answers1

1

Set your columns' True and False values like so:

Kamerachk.TrueValue = true; and Kamerachk.TrueValue = false;