3 Replies - 579 Views - Last Post: 23 August 2015 - 12:51 PM Rate Topic: -----

#1 t3cho   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 231
  • Joined: 27-May 14

Only one collumn-row-Cell enable

Posted 23 August 2015 - 11:17 AM

Hey guys im trying here to make only one cell in column(0) to be able to check.

Imagine that only one row in SQL Table can be primary key so all others can't

Posted Image

I need to check if the data type is int or bigint and enable to check the PK collumn.

When i check the PK for one row the previous checked needs to be unchecked.

i tried with this code

For i = 0 To dgv.Rows.Count - 1
            If dgv.Rows(i).Cells("PK").Value Then
                For j = 0 To i - 1
                    dgv.Rows(j).Cells("PK").Value = False
                Next

                For x = i To dgv.Rows.Count - 1
                    dgv.Rows(x).Cells("PK").Value = False
                Next
            End If
        Next


It should work fine but the problem is when i click on other row without checking the PK cell it reset the previous one.

For loop to uncheck the rows to selected one
For loop to uncheck the rows from selected one to max.

Is This A Good Question/Topic? 0
  • +

Replies To: Only one collumn-row-Cell enable

#2 andrewsw   User is online

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: Only one collumn-row-Cell enable

Posted 23 August 2015 - 12:29 PM

Your question isn't entirely clear, but the following code works for me to uncheck all other checkboxes in the same column when one of them is checked or unchecked.

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) _
            Handles DataGridView1.CellContentClick

        If e.ColumnIndex <> 0 Then
            Exit Sub    'it's not the checkbox
        End If
        'check the data type, exit if not int or bigint

        For Each row As DataGridViewRow In DataGridView1.Rows
            If row.Index <> e.RowIndex Then
                row.Cells(0).Value = False
            End If
        Next
    End Sub

It is better to modify it to not use the magic number 0, but to refer to the column by its name.

..like this:

If e.ColumnIndex <> DataGridView1.Columns("PK").Index Then

This post has been edited by andrewsw: 23 August 2015 - 12:27 PM

Was This Post Helpful? 1
  • +
  • -

#3 t3cho   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 231
  • Joined: 27-May 14

Re: Only one collumn-row-Cell enable

Posted 23 August 2015 - 12:41 PM

Thank you for your answer it seems like you understood my problem. But this is close solution except one thing.

I need to uncheck the other checkboxes only but only if the checkbox is clicked.

By this code no matter in which cell i click it reset the checkboxes.

When i click here it will uncheck the checkbox

Posted Image


EDIT : My bad ignore this post.

It was my previous code on other dgv event.

Anyway thanks for solution

This post has been edited by t3cho: 23 August 2015 - 12:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is online

  • awks lol ffs
  • member icon

Reputation: 6697
  • View blog
  • Posts: 27,509
  • Joined: 12-December 12

Re: Only one collumn-row-Cell enable

Posted 23 August 2015 - 12:51 PM

I cannot replicate your issue. For the test I have it will only uncheck the other checkboxes if I click directly on a checkbox, in its square.

View Postt3cho, on 23 August 2015 - 07:41 PM, said:

EDIT : My bad ignore this post.

It was my previous code on other dgv event.

Anyway thanks for solution

Ok ;)

I will add a note for anyone reading, and that is, to get the NEW checked state of the clicked checkbox, use:

Dim state = CType(DataGridView1.Rows(e.RowIndex).Cells(0).EditedFormattedValue, Boolean)

I haven't actually tested this, but I believe that reading the .Value won't tell us the state that the checkbox will be in after clicking.

This post has been edited by andrewsw: 23 August 2015 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1