• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1966
  • Last Modified:

datagrid checkbox colum lock

Hi all ..

This is my problem, i've got a datagrid with a checkbox column, but i only want to display check or unchecked from the database, i dont want the user to be able to change the status of the checkbox, it can only change when i let him ..

i've tryed using the readonly property but it does not suit me ..

Is there anything like on vb6

datagrid.column(index).locked = true or false ???
0
MICROCAOS
Asked:
MICROCAOS
  • 4
  • 3
  • 2
  • +1
1 Solution
 
TimCotteeCommented:
Hi MICROCAOS,

One solution is to add an onclick event handler with return false:

In your itemdatabound event, you can add attributes to each checkbox that is generated. Use:

MyCheckbox.Attributes.Add("onclick","return false;")

This will allow the checkbox to be displayed as normal, however any click will be discarded so the state will not be changed.

Tim Cottee
0
 
TimCotteeCommented:
MICROCAOS,

Dopey me, that is an asp.net solution, not a vb.net solution. Hang on

Tim
0
 
newyuppieCommented:
workaround could be if you handle the checkbox checkedchange event, you could add a code like:

checkbox1.checked = not chechbox1.checked

so that it will return the checked state to the one that was there before the click no matter if it was true or false.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
MICROCAOSAuthor Commented:
no i cannot mess with the state of the check box .. it has a important value .. it has to display the corrected cheked state, the only thing i need is to disable the user from clicking on the checkbox in the datagrid so he cannot change te value unless i permit that in another form ...

TimCottee
eheheh i thought as mutch eheheh
0
 
newyuppieCommented:
the code i posted will not mess with the checkbox state if you make these modifications:

1) declare a private variable for the class
private loading as boolean

2) in the form Load add this line at the very beginning of the load:
loading = true

3) and this to the very end of the sub
loading = false

3) handle the checkbox changed event with this code

If loading then Exit Sub
checkbox1.checked = not chechbox1.checked


this will
0
 
newyuppieCommented:
this will allow you to load and set the datagrid source without handling the checkbox state. after load is done, if a user clicks on the checkbox, the handler will reclick it back to what it was originally.

also if you want to allow it programmatically from another form you could

1) add another variable to the class
public allowcheck as boolean

2) change the checkbox event handler to
If loading then Exit Sub
if not allowcheck then
  checkbox1.checked = not chechbox1.checked
end if

3) based on what you want to allow or not, switch the allowchecks to true of false from another form.


0
 
MICROCAOSAuthor Commented:
but .. how can i get to the event handler of the checkbox ???

BTW it's not a checkbox .. it's a DataGridBoolColumn on a datagrid and this is the code for it :

Dim dgEstiloColuna As New DataGridTableStyle
Dim colCheck As New DataGridBoolColumn

dgEstiloColuna.MappingName = dtsCOMPRAS.Tables("LINHAS").ToString

      colCheck.MappingName = "APP_GERCMP"
      colCheck.HeaderText = "Gerada"
      colCheck.AllowNull = False
      colCheck.TrueValue = True
      colCheck.FalseValue = False
      colCheck.ReadOnly = True
      colCheck.NullValue = False
      colCheck.Width = 50
      dgEstiloColuna.GridColumnStyles.Add(colCheck)

      grdAPP_ITMCMP.TableStyles.Add(dgEstiloColuna)

datagrid : grdAPP_ITMCMP
0
 
MICROCAOSAuthor Commented:
Ok i've found a solution :

Public Shared bolGRIDCHECKBOX As Boolean = True


colCheck.MappingName = "APP_GERCMP"
      colCheck.HeaderText = "Gerada"
      colCheck.AllowNull = False
      colCheck.TrueValue = True
      colCheck.FalseValue = False
      If bolGRIDCHECKBOX = True Then
         colCheck.ReadOnly = True
      Else
         colCheck.ReadOnly = False
      End If
      colCheck.NullValue = False
      colCheck.Width = 50
      dgEstiloColuna.GridColumnStyles.Add(colCheck)


Private Sub grdAPP_ITMCMP_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles grdAPP_ITMCMP.MouseUp
      Dim shtCHKCOL As Short = 10     'coluna checkbox
      Dim hti As DataGrid.HitTestInfo = Me.grdAPP_ITMCMP.HitTest(e.X, e.Y)

      Try
         If hti.Type = DataGrid.HitTestType.Cell AndAlso hti.Column = shtCHKCOL Then
            If bolGRIDCHECKBOX = False Then
               Me.grdAPP_ITMCMP(hti.Row, hti.Column) = Not CBool(Me.grdAPP_ITMCMP(hti.Row, hti.Column))
            End If
         End If
      Catch ex As Exception
         MessageBox.Show(ex.ToString())
      End Try

   End Sub


A it was so simple that i thought i couldn't work .. but .. it did eheh but thanks anyway ..
0
 
newyuppieCommented:
great
NY
0
 
DarthModCommented:
PAQd, 125 points refunded.

DarthMod
CS Moderator
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now