0
votes

I have a Datagridview which is set to readonly and programmically set to checked / unchecked / indeterminate. However, checked and uncheck options work fine, but the indeterminate state is not working at all. The code example is included in a cellcontentclick handler.

 If Not temp_man_pos = Nothing And Not temp_man_neg = Nothing Then
            If temp_abweichung <= temp_man_pos And temp_abweichung >= (-(temp_man_neg)) And Not temp_man_pos = Nothing And Not temp_man_neg = Nothing Then
                                   Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = CheckState.Checked
                ' Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = 1
            Else

                Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = CheckState.Unchecked
                '  Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = 0
            End If
        Else
                           Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = CheckState.Indeterminate
        End If

In the Datagridview Options, the True Value is manually set to 1, the false to 0 and indeterminate to 2, which should be the same value like checkstate.indeterminate.

I also tried with tristate option but the .usedefault value is displayed as checked in the checkbox.

                Me.DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True
            Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").ValueType = GetType(TriState)
            Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = TriState.UseDefault

True and False Values are working great though.

What am I doing wrong?

2
It is not really clear what the problem is. “Not working” is not a very good description of the problem. The Indeterminate value will always display as checked unless TriState is set to true in which case the value should display as a solid box inside the check box. Again it is not clear exactly what the problem is.JohnG
Thanks for response. The indeterminate value is displayed as checked. If i set Tristate to true the tristate.usedefault value is -1, which is also displayed as checked. i can change the true, false and indeterminate values in the Datagridview setting of the checkboxes, but that doesn´t make a difference.T.S
Is it possible that the dgv.value is boolean, which can only be set to true or false and ignores the threestate?T.S

2 Answers

1
votes

Thanks for the response.

Finally I figured out to solve the problem, though I`m a little confused about it...

Problem:

I set Threestate to true, the indeterminate value to 2 and tried to set the Threestate by code as below:

DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = CheckState.Indeterminate

That solution don´t displayed the indeterminate state in the Datagridview Checkbox, the Checkbox was displayed as checked.

Solution:

I set the Datagridview value as below:

Me.DataGridView1.Rows(x).Cells("KAL_Measuring_Data_Result_1").Value = DBNull.Value

Now, the indeterminate state is displayed right in the Datagridview Checkbox!

So, valuetype to Tristate don´t worked, Threestate Value manually set to 2 with Checkstate.Indeterminate as Checkbox value either.

Can someone explain this behavior?

Thank you Guys for your help, hopefully my answer is understandable!

0
votes

I am still somewhat confused as to what is not working. I could be wrong about this, but the code below may help to demonstrate what I am describing. In the case when the ThreeState property of the check box column is set to “False”: this will have only TWO (2) possible values: zero (0) for false and ANY other number for true. Even if the number is -1, 4, -3 all these values will set the check state to “Checked” and the only value for false is zero (0).

For the case when the ThreeState property of the check box column is set to “True”: then this will have Three (3) or more possible values: zero (0) for false, One (1) for true and Two (2) for the IndeterminateValue. Any other value in this state will throw an error if the values are not 0, 1 or 2. Two (2) appears to be a default value for Indeterminate.

Using this, if you set the check box columns IndeterminateValue to some value other than zero (0) or One (1) then there will be 4 possible values. Example: If you set, the intermediate value to -1, THEN Zero (0) will be false, 1 will be true, -1 will be the Indeterminate value AND Two (2) will also be an indeterminate value. Use the test code Supplied further down and you will see this is the case. Comment out the lines below to see what happens when these values are set to the check box column.

cbCol.ThreeState = True
cbCol.IndeterminateValue = -1

The code below demonstrates that when the ThreeState is set to true AND the IndeterminateValue is set to -1, then the check box cell will display a solid “Indeterminate” value when it is given a Negative One (-1) AND a Two (2) (default value). In addition, since the ThreeState is set to true, any other values other than Zero (0), One (1), Negative 1 (-1) AND Two (2) will throw an error for an invalid state. This can be seen by the value in row 10 below, which will throw an error because it is not a 0, 1, -1 or 2. I hope this make sense.

enter image description here

Public Class Form1

  Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    SetDGVColumns()
    FillGrid()
  End Sub

  Private Sub SetDGVColumns()
    DataGridView1.Columns.Add("Name", "Name")
    Dim cbCol As DataGridViewCheckBoxColumn = New DataGridViewCheckBoxColumn()
    cbCol.ThreeState = True
    cbCol.IndeterminateValue = -1
    DataGridView1.Columns.Add(cbCol)
  End Sub

  Private Sub FillGrid()
    DataGridView1.Rows.Add("Row1", CheckState.Checked)
    DataGridView1.Rows.Add("Row2", CheckState.Unchecked)
    DataGridView1.Rows.Add("Row3", CheckState.Indeterminate)
    DataGridView1.Rows.Add("Row4", True)
    DataGridView1.Rows.Add("Row5", False)
    DataGridView1.Rows.Add("Row6", 1)
    DataGridView1.Rows.Add("Row7", 0)
    DataGridView1.Rows.Add("Row8", -1)
    DataGridView1.Rows.Add("Row9", 2)
    DataGridView1.Rows.Add("Row10", -3)
  End Sub

  Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MessageBox.Show("Row1 check state value: " + DataGridView1.Rows(0).Cells(1).Value.ToString() + Environment.NewLine +
                          "Row2 check state value: " + DataGridView1.Rows(1).Cells(1).Value.ToString() + Environment.NewLine +
                          "Row3 check state value: " + DataGridView1.Rows(2).Cells(1).Value.ToString())
  End Sub
End Class