简体   繁体   English

如何将DataGridView中的CheckBox列值设置为true

[英]How to set the CheckBox column in DataGridView value to true

GD All, GD全部,

I've looking around for a solution to my below challenge. 我一直在寻找以下挑战的解决方案。

I have got a form with an unbound datagridview, the dg has one added column that allows user to select a method to be used. 我有一个带有未绑定datagridview的表单,dg中有一个添加的列,允许用户选择要使用的方法。 The state of the event is stored in a database and after re-opening the form the code checks if the event is in an 'open' state, if so it compares the previously selected method with the methods in the datagrid and should set the previously activated method to be the 'selected' method. 事件的状态存储在数据库中,并且在重新打开表单后,代码将检查事件是否处于“打开”状态,如果是,则它将先前选择的方法与数据网格中的方法进行比较,并应设置先前的方法激活的方法为“选定”方法。

Yet I can't seem to get this to work unfortunately... 但是我似乎无法使它正常工作...

The below code loops through the methods in the dg and compares the values, if it meets the methodID it should set the value to 'True' or to the TrueValue anyway. 下面的代码循环遍历dg中的方法并比较值,如果满足methodID,则应将值设置为“ True”或TrueValue。

This is initialized if the database check returns true and after full initialisation of the form, where session.methodID is a field in the returned LINQ query. 如果数据库检查返回true,并且在完全初始化表单后(其中session.methodID是返回的LINQ查询中的字段),则将初始化该字段。

For Each r As DataGridViewRow In dgMethods.Rows

   If r.Cells(1).Value = session.methodID Then
      Dim c As DataGridViewCheckBoxCell = r.Cells(0)
      c.Value = c.TrueValue
   End If

Next

Unfortunately, this doesn't set the checkbox to 'Checked'. 不幸的是,这并未将复选框设置为“已检查”。 The loop runs and evaluates the comparison between r.Cells(1).Value and session.methodID correct and triggers correctly. 循环运行并评估r.Cells(1).Valuesession.methodID之间的比较正确并正确触发。

The interesting thing is if I do a similar loop after the 'CellContentClick' event it does do exactly what is expected. 有趣的是,如果我在'CellContentClick'事件之后执行了类似的循环,则它确实执行了预期的操作。 (the example below sets all checkbox values to checked) (下面的示例将所有复选框的值都设置为选中)

Private Sub dgMethods_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgMethods.CellContentClick

    'Only single selection allowed, so clear table before submitting new selection
    For Each r As DataGridViewRow In dgMethods.Rows
        Dim c As DataGridViewCheckBoxCell = r.Cells(0)
        c.Value = c.TrueValue

    Next

    dgMethods.CommitEdit(DataGridViewDataErrorContexts.Commit)

End Sub

So, apparently there is a difference in the state between just calling the loop on the dgMethods and when the dgMethods.CellContentClick event has triggered, yet I do not know which one ? 因此,显然仅在dgMethods上调用循环与dgMethods.CellContentClick事件触发之间的状态有所不同,但我不知道哪一个? There are many many post on trying to set the CheckBox column yet I have not been able to get any of them working. 有很多关于尝试设置CheckBox列的文章,但我还没有使它们中的任何一个起作用。

Anybody have any idea ? 有人有什么主意吗?

I would appreciate your suggestions ? 我会很感激您的建议?

I was not sure of being undestand your question... but there's s simple way to check and change the state of a chechbox cell in a datagridview: 我不确定是否会理解您的问题...但是有一种简单的方法可以检查和更改datagridview中chechbox单元的状态:

 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        For Each dr As DataGridViewRow In DataGridView1.Rows
            If CBool(dr.Cells(0).Value) = True Then dr.Cells(0).Value = False : Continue For
            If CBool(dr.Cells(0).Value) = False Then dr.Cells(0).Value = True
        Next

    End Sub

In this example, when you click this button for each row in the datagridview, checks the checkboxcell and set the value to FALSE or TRUE depending of his value. 在此示例中,当您为datagridview中的每一行单击此按钮时,请选中checkbox并将其值设置为FALSE或TRUE。

Hope this helps you. 希望这对您有所帮助。

And let me one more tip. 再给我一个小费。 If you get acces to the cells for his name instead of his index use his name, it should helps you avoiding troubles ;) 如果您获得了他的名字而不是索引使用他的名字的单元格,它应该可以帮助您避免麻烦;)

GD All, GD全部,

After searching further I came across the following interesting behaviour. 经过进一步搜索后,我发现了以下有趣的行为。

The method selection process is part of a form called 'frmAddEvent', the frmAddEvent form is called from a main form using below routine. 方法选择过程是名为“ frmAddEvent”的表单的一部分,使用以下例程从主表单中调用frmAddEvent表单。

The new form instance is created and afterwards filled using a public sub in the form class called InitializeForm() which uses a GUID parameter to retrieve corresponding data to set the form fields. 创建新的表单实例,然后使用名为InitializeForm()的表单类中的公共子项填充该表单,该子类使用GUID参数检索对应的数据以设置表单字段。

If Not (isOpened(rsTankName.unqID)) Then
   Dim newForm As New frmAddEvent() '(rsTankName)
   newForm.InitializeForm(rsTankName)
   newForm.Show()

Else

End If

The initialization sub queries several datatables and sets the corresponding fields in the new form instance correctly if applicable. 初始化子查询几个数据表,并在适用的情况下正确设置新表单实例中的相应字段。 Part of that setting is the method selection in the dgMethods datagridview. 该设置的一部分是dgMethods datagridview中的method选择。

It would appear that the sequence in which you call the form makes all the difference as the below code works perfectly: 似乎您调用表单的顺序产生了所有不同,因为以下代码可以完美地工作:

If Not (isOpened(rsTankName.unqID)) Then
   Dim newForm As New frmAddEvent() '(rsTankName)

   newForm.Show()
   newForm.InitializeForm(rsTankName)
Else

End If

So calling the newForm.InitializeForm(rsTankName) after the newForm.Show event allows the datagridview to set the CheckBoxColumn correctly. 因此newForm.Show事件之后调用newForm.InitializeForm(rsTankName)可以使datagridview正确设置CheckBoxColumn。

Likely because the actual CheckBox itself is only actually generated upon the Show command, despite the fact that it is 'available' as a cell with DataGridViewCheckBoxColumn properties in the datagrid, directly after the New frmAddEvent has created the new form instance. 可能是因为实际的CheckBox本身仅在Show命令时才真正生成,尽管事实上它是在New frmAddEvent创建新表单实例之后立即作为数据网格中具有DataGridViewCheckBoxColumn属性的单元格“可用”的事实。 The actual CheckBox and its corresponding CheckedState is not created before the newForm.Show event is called. 在调用newForm.Show事件之前,不会创建实际的CheckBox及其对应的CheckedState。 It would appear that the when the CheckBox is created for display (during the newForm.Show event) there is no comparison made to its actual value. 看起来当创建CheckBox用于显示时(在newForm.Show事件期间),没有对其实际值进行比较。

So, in order to set the Checkbox column on initiating a new form you have to call the Show event prior to setting the DataGridViewCheckBoxColumn values otherwise the CheckBox will not show it as 'Checked'. 因此,为了在启动新表单时设置Checkbox列,必须在设置DataGridViewCheckBoxColumn值之前调用Show事件,否则CheckBox不会将其显示为“已检查”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM