PC Review


Reply
Thread Tools Rate Thread

DataGridView: Preventing deselection of a selected checkbox

 
 
Mike
Guest
Posts: n/a
 
      26th Jan 2006
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


 
Reply With Quote
 
 
 
 
Toff McGowen
Guest
Posts: n/a
 
      26th Jan 2006
a code sample would be nice.

tm

"Mike" <none> wrote in message
news:%(E-Mail Removed)...
> 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
>
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      26th Jan 2006
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


 
Reply With Quote
 
Ken Tucker [MVP]
Guest
Posts: n/a
 
      26th Jan 2006
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
-------------------
"Mike" <none> wrote in message
news:%(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      27th Jan 2006
"Ken Tucker [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      27th Jan 2006
"Cor Ligthert [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      27th Jan 2006
"Toff McGowen" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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


 
Reply With Quote
 
Toff McGowen
Guest
Posts: n/a
 
      27th Jan 2006
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

"Mike" <none> wrote in message news:(E-Mail Removed)...
> "Toff McGowen" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >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
>
>



 
Reply With Quote
 
Cor Ligthert [MVP]
Guest
Posts: n/a
 
      27th Jan 2006
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?...d-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?...8-1def3152436f

I hope this helps,

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGridView checkbox. Mr. X. Microsoft VB .NET 4 25th Jun 2010 08:01 AM
Datagridview checkbox Cdude Microsoft C# .NET 3 30th Apr 2010 07:16 PM
DataGridView, preventing CancelNew Jim Microsoft Dot NET Framework Forms 1 28th Dec 2007 07:44 AM
Preventing focus from leaving DataGridView Larry Microsoft Dot NET Framework Forms 4 27th Dec 2007 06:26 PM
datagridview checkbox mrstrong Microsoft Dot NET Framework Forms 6 13th Sep 2007 12:35 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:48 AM.