0

I have a datagridview with a column with checkbox in it. What I am trying to do is when user click on the check box, I need to check for some other conditions, and only allow the check box to be selected if conditions meet other wise stop the event and dont allow checkbox selection.

Can some one guide pls?

user1063108
  • 662
  • 1
  • 10
  • 24
  • The events mentioned in [this question](http://stackoverflow.com/questions/932040/triggering-a-checkbox-value-changed-event-in-datagridview-c-net) will probably help you, just do your validation in there. – Bridge Jun 09 '12 at 22:34

2 Answers2

0

Well it's really simple.

you can attach event haandler to your checkbox and when event fires just check everything you need to check and if conditions doesn't met then call uncheck on the checkbox.

Mayank
  • 8,777
  • 4
  • 35
  • 60
  • I guess my question was vague. When user clicks on check box, before checkbox is actually checked, I want validate my conditions, if they meet I allow the event to complete and checkbox is showed checked, but otherwise, checkbox is never checked – user1063108 Jun 09 '12 at 22:41
  • well just call `checkBox.Checked = false;` if the conditions don't meet. – Mayank Jun 09 '12 at 22:44
  • hunm..I should have tried this, but here is my understanding..when user clicks, I can validate from db, this will go and come back and will take time, now during this time user will see that checkbox is checked, once I got the data back from db, then I may set checkbox.checked=false; However during this time checkbox will be displayed as checked and thats what I dont want to do, I actually want to it to be clean(hopefully) and wont allow the actual checking event to complete. is there a way to trap the evnen in that manner – user1063108 Jun 09 '12 at 22:47
  • I guess I have to settle, I will settle with the event dataGridView1_CellContentClick – user1063108 Jun 09 '12 at 23:26
  • Sounds to me like you need to re-think the design if possible. If you don't want to he user to check it because of something in the database, you should be able to send that indicator to the form at the time it is shown initially and then even disable the checkbox to begin with. Why do you need to wait until it is clicked before you go and check the database? – Nope Jun 09 '12 at 23:28
  • I totally agree with you, but my issue is that this needs to work as CVS, or any other version control. I have to check and determine at run time....I hope some one can guide me to right events – user1063108 Jun 10 '12 at 00:13
  • Any input is greatly appreciated,? unfortunately dataGridView1_CellContentClick doesnt work as intended. I can set the value to false but it doesnt take effect. I certainly need right sequence where I need to trap the event, MS docs say that CellClick event is which I may need as it is called before value is actually changed, but when I set the value to false, it doesnt work – user1063108 Jun 10 '12 at 03:02
0

OK, so here is the recap of my problem I have a windows form application, it loads some information from a datasource(file, db etc). When its loaded, every row's first column has DataGridViewCheckBoxCell in it. Requirement was that when a user clicks on the checkbox, it must be checked against the source that this particular row was not checked by any other user from any other workstation. My Solution Logic as I envisioned it: I wanted to make sure when user clicks on the cell, before chackbox is checked, I check from the data source and don't allow checkbox to be selected or allow it based upon if this row was used by some one else or not.

My Solution: What I came up with is to use CellClick event, because when user clicks on the cell, the checkbox is yet to be selected, then I check the datasource if any other user is using it or not, if it is being used, I set the checkbox.ReadOnly=true

This way this event essentially is dead and nothing happens...:-) I wish I had a better approach but this is working for my needs.

Thanks for every one who tried to help.

user1063108
  • 662
  • 1
  • 10
  • 24