DataGridView: Preventing deselection of a selected checkbox

M

Mike

Hi all,

I have a DataGridViewCheckBoxColumn as one of my columns in a DataGridView.
I want this checkbox to only be checked, but not unchecked - it's used by
the user to audit that they have "seen" and checked an order.

In VB6 I would handle the Mouse Button Down event, test the target cell and
set the mouse event arguements to nothing. This doesn't actually work in
..NET, allowing the user to deselect the checkbox.

If I put the logic in the CellContentClick event to test whether the user is
selecting an already checked cell and keeping the cell checked - this also
doesn't work.

I can, however test for the value of the checkbox and set/unset other
checkboxes on the same row; but no matter what I set the original target
checkbox to, I can't get .NET to prevent the user from deselecting a
selected checkbox.

Any ideas what I'm doing wrong?

Thanks!

Mike
 
C

Cor Ligthert [MVP]

Mike,

There is no DataGrid in VB6, so you cannot handle that in VBNet. Maybe is
there something as a FlexGrid, however that is completely different.

Be aware that Data controls in VBNet use forever the underlying DataSource.
That means that the best way to handle those Data controls is to access them
by the DataSource and not direct in the control.

I hope this helps,

Cor
 
K

Ken Tucker [MVP]

Hi,

You can always make the column read only.

Dim strConn As String
Dim da As SqlDataAdapter
Dim conn As SqlConnection
Dim ds As New DataSet

strConn = "Server = .;"
strConn &= "Database = NorthWind; Integrated Security = SSPI;"
conn = New SqlConnection(strConn)
da = New SqlDataAdapter("Select * From [Products]", conn)

da.Fill(ds, "Products")
DataGridView1.DataSource = ds.Tables("Products")
DataGridView1.Columns("Discontinued").ReadOnly = True

Ken
 
M

Mike

Ken Tucker said:
Hi,

You can always make the column read only.

Thanks Ken,

The problem with that is, I want to be able to make it read/write so that it
can be selected the first time. Another bug that your code sample would
introduce is that I would set >all< cells in that column to read only.

I'm sure there must be an easier way of preventing a user click on an
already selected checkbox in a datagridview.

M
 
M

Mike

Cor Ligthert said:
Mike,

There is no DataGrid in VB6, so you cannot handle that in VBNet. Maybe is
there something as a FlexGrid, however that is completely different.

There are grids and controls in VB6 that you can catch the mouse events on
and set the mouse parameters to nothing such that the mouse click event is
effectively cancelled.
Be aware that Data controls in VBNet use forever the underlying
DataSource. That means that the best way to handle those Data controls is
to access them by the DataSource and not direct in the control.

Fair enough, but that still won't prevent deselection of a selected
checkbox.

M
 
M

Mike

Toff McGowen said:
a code sample would be nice.

No code, at the moment it's just a datagridview bound to a datatable. One of
the columns is of a checkbox type.

I want to prevent deselection after selection - EVEN if the selection was
inadvertant.

M
 
T

Toff McGowen

Oh. Then checking the check box will alter the rowstate of the underlying
record.
If the rowstate is modified then prevent/reset check state...assuming no
other field of the record is editable and thus able to alter rowstate?

tm
 
C

Cor Ligthert [MVP]

Mike,

Why not however using the validating event from the checkbox will probably
more easy.

This is about the textbox however the checkbox will probably go as well. (I
did not try that)

http://www.vb-tips.com/default.aspx?ID=bece831d-6742-4364-bd0d-203ca99d2825

Otherwise you can try it (for the underlying datasource) in the row change
event of that datasource.

And if you real want it up to date, than you van evaluate the datasource
like this. (a little bit ugly method)

http://www.vb-tips.com/default.aspx?ID=30d9d2fd-9f10-4928-b7c8-1def3152436f

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top