7 Replies - 1574 Views - Last Post: 05 December 2012 - 10:40 PM Rate Topic: -----

#1 the5thelement  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 30-July 12

Check Which Checkboxes In DataGridView Are Checked

Posted 04 December 2012 - 06:35 PM

I have a 63x63 datagridview filled with checkboxes for a total of 3969
checkboxes. Each checkbox represents a title in a game world. I am trying
to figure out how I can make the application check to see what checkboxes
are checked and create a file based on that. For example in column 1 rows
1 and 2 have a checkbox that is checked, and column 2 row 1 has a checkbox
checked. I need to find out which checkboxes are checked and create a file
based on that. Like this Tile1_1, Tile1_2, and Tile2_1. Im sorry if this
doesn't make sense but I don't know were to start and all I have found on
google is ways to count how many checkboxes are checked not which checkboxes
are checked.

Is This A Good Question/Topic? 0
  • +

Replies To: Check Which Checkboxes In DataGridView Are Checked

#2 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 04 December 2012 - 11:09 PM

Not sure what form you want it in, but here's some example code. I made a DataGridView with 3 columns and 9 rows.
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        DataGridView1.Rows.Add(8)
        For x = 0 To 2
            For y = 0 To 8
                DataGridView1.Item(x, y).Value = False
            Next
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim checks(3, 9) As Boolean

        For col As Integer = 0 To 2
            For ro As Integer = 0 To 8
                checks(col, ro) = CBool(DataGridView1.Item(col, ro).Value)
                Debug.Print(col & ", " & ro & ", " & DataGridView1.Item(col, ro).Value.ToString)
            Next
        Next
    End Sub


The Form1_Load routine is needed because otherwise the CheckBox values are Nothing instead of True or False.

Edit: Fixed a small error in code

This post has been edited by lar3ry: 05 December 2012 - 06:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 the5thelement  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 30-July 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 04:49 PM

View Postlar3ry, on 04 December 2012 - 11:09 PM, said:

Not sure what form you want it in, but here's some example code. I made a DataGridView with 3 columns and 9 rows.
    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        DataGridView1.Rows.Add(8)
        For x = 0 To 2
            For y = 0 To 8
                DataGridView1.Item(x, y).Value = False
            Next
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim checks(3, 9) As Boolean

        For col As Integer = 0 To 2
            For ro As Integer = 0 To 8
                checks(col, ro) = CBool(DataGridView1.Item(col, ro).Value)
                Debug.Print(col & ", " & ro & ", " & DataGridView1.Item(col, ro).Value.ToString)
            Next
        Next
    End Sub


The Form1_Load routine is needed because otherwise the CheckBox values are Nothing instead of True or False.

Edit: Fixed a small error in code


Thank you very much. This code has helped a lot. I am only getting one error.

Quote

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I get this error when I set X to "0 to 62" and Y to "0 to 62".
I don't get this error when I set Y to "0 to 60" which makes
no sense because the grid is 63x63.
Was This Post Helpful? 0
  • +
  • -

#4 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 06:38 PM

View Postthe5thelement, on 05 December 2012 - 05:49 PM, said:

Thank you very much. This code has helped a lot. I am only getting one error.

Quote

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I get this error when I set X to "0 to 62" and Y to "0 to 62".
I don't get this error when I set Y to "0 to 60" which makes
no sense because the grid is 63x63.

Do you get that one when you do something like the code I supplied in Form_Load? Or is it just when checking the CheckBoxes?

I gather you made a set of columns, 63 columns wide, then added 62 rows to it. Please correct me if I'm wrong.

Can you show me the Dim statement for the array (The one like I used... checks())?
Was This Post Helpful? 0
  • +
  • -

#5 the5thelement  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 30-July 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 06:50 PM

Here is my Form1_Load
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Select()
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        DataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
        DataGridView1.Rows.Add(62)
        For x = 0 To 62
            For y = 0 To 62
                DataGridView1.Item(x, y).Value = False
            Next
        Next
    End Sub


And here is my Button_Click
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim checks(63, 63) As Boolean

        For col As Integer = 0 To 62
            For ro As Integer = 0 To 62
                checks(col, ro) = CBool(DataGridView1.Item(col, ro).Value)
                Debug.Print(col & ", " & ro & ", " & DataGridView1.Item(col, ro).Value.ToString)
            Next
        Next

    End Sub

Was This Post Helpful? 0
  • +
  • -

#6 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 08:40 PM

Sure looks like it should run.
Is the error in the Click routine?
When it stops, click on the Break button, and check the values in col and ro. What are they? And, what line are they on?

It's a big job to build your Form, so if there's a place where you can upload the Project (DropBox or similar), I could take a look at it in it's entirety.
Was This Post Helpful? 0
  • +
  • -

#7 the5thelement  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 59
  • Joined: 30-July 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 10:02 PM

No the error is in the form load routine.
Here is a download link for the project.
Map Tile Adder
Was This Post Helpful? 0
  • +
  • -

#8 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Check Which Checkboxes In DataGridView Are Checked

Posted 05 December 2012 - 10:40 PM

View Postthe5thelement, on 05 December 2012 - 11:02 PM, said:

No the error is in the form load routine.

In your first description, you said there were 63 * 63 cells, and in the last code you posted in a message, there were also 63 * 63 cells. But in the code you put on DropBox, there were 64 * 64 cells. I changed it to 63 * 63, and have it working. The DataGridView on the Form, I also changed to make it 63 cells across.
    Dim Columns As Integer = 0
    Dim Rows As Integer = 0
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Select()
        DataGridView1.AllowUserToAddRows = False
        DataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells)
        DataGridView1.AutoResizeRows(DataGridViewAutoSizeRowsMode.AllCells)
        DataGridView1.Rows.Add(63)
        For x As Integer = 0 To 62
            For y As Integer = 0 To 62
                DataGridView1.Item(x, y).Value = True
            Next
        Next
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim checks(63, 63) As Boolean

        For col As Integer = 0 To 62
            For ro As Integer = 0 To 62
                checks(col, ro) = CBool(DataGridView1.Item(col, ro).Value)
                Debug.Print(col & ", " & ro & ", " & DataGridView1.Item(col, ro).Value.ToString)
            Next
        Next

    End Sub


If this is what you wanted, great! But if not, I am at a total loss as to why you can't have a 64 * 64 matrix of CheckBoxes in a DataGridView. Perhaps it's a limitation of one or the other type of control.

Let me know how it goes, will you? It's got me baffled.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1