Hi experts,

I am a student working on a project for my company. I work using Microsoft Visual Studio 2005. Language is vb.net. I also work using windows application.

I have a form(EmailContacts) which contains a datagridview and a button to add the data to a textbox of another form. Everything is working fine and i can display data from the database on the datagridview properly. The problem is how do i add a 'Select All' feature such that when the user clicked once on the 'Select All' checkbox, all the checkboxes of the columns are selected?

Pls advise.

These are my codes for the form:-

Public Class EmailContacts
    Dim xs As String
    Dim conn As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter
    Private Sub EmailContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\Documents and Settings\1B09158\Desktop\aLL.iCONS\database\User.mdb"

        'Dim connectionString As String = "Data Source=ITCU-22\SQLEXPRESS;Initial Catalog=User;" & "Integrated Security=SSPI;"
        'conn = New SqlConnection(connectionString)

        xs = "SELECT CustName,Email,Selection FROM customer"
        da = New OleDb.OleDbDataAdapter(xs, conn)
        conn.Open()

        ds = New DataSet()
        'Dim commandBuilder As SqlCommandBuilder = New SqlCommandBuilder(da)
        da.Fill(ds, "customer")
        DataGridView1.DataSource = ds.Tables("customer")
        Email.Show()
    End Sub


    Private Sub AddContacts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer

        Dim ds As DataTable = CType(DataGridView1.DataSource, DataTable)
        Email.rtbBcc.Text = ""

        For i = 0 To (ds.Rows.Count - 1)
            If DataGridView1.Rows(i).Cells("Selection").Value IsNot System.DBNull.Value Then
                If DataGridView1.Rows(i).Cells("Selection").Value = True Then
                    Email.rtbBcc.Text += DataGridView1.Rows(i).Cells("Email").Value.ToString()
                    Email.rtbBcc.Text += ","
                    'Email.TextBox5.Text += DataGridView1.Rows(i).Cells("CustName").Value.ToString() + "," & vbCrLf


                    'If DataGridView1.Rows(i).Cells("Selection").Value = True Then
                    '    SendSMS.TextBox1.Text += DataGridView1.Rows(i).Cells("CustName").Value.ToString() + "," & vbCrLf
                    'End If
                End If
            End If
        Next
        Email.rtbBcc.Text = Email.rtbBcc.Text.Remove(Email.rtbBcc.Text.Length - 1, 1)
        Me.Hide()

    End Sub

End Class

Have to try to update value of selection column?

For i = 0 To ds.Rows.Count - 1
      ds.Rows(i)("Selection")=true
      ..
 Next

Have to try to update value of selection column?

For i = 0 To ds.Rows.Count - 1
      ds.Rows(i)("Selection")=true
      ..
 Next

Thank you for the quick reply. Where exactly should i insert those codes? I'm sorry but i'm new to this language.

Hi.
you can simply use SelectAll property of Datagridview.

DataGridView1.SelectAll()

Happy Coding :-)

Ok. So where exactly should i insert that method?

Hi.
you can simply use SelectAll property of Datagridview.

DataGridView1.SelectAll()

Happy Coding :-)

alright. I've already tried it by inserting that method in a button. So when i click the button, it will the select all. The problem is when i click that button, it select all the rows by highlighting and that is not i wanted. What i planned to do is to insert a checkbox on top of the Selection column or insert a button that can tick all the checkboxes.

Plz advise.

alright. I've already tried it by inserting that method in a button. So when i click the button, it will the select all. The problem is when i click that button, it select all the rows by highlighting and that is not i wanted. What i planned to do is to insert a checkbox on top of the Selection column or insert a button that can tick all the checkboxes.

Plz advise.

Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim i As Integer
        Dim SelectedWays
        SelectedWays = 0
        For i = 0 To WPGrid.RowCount - 1
            If CheckBox1.Checked = True Then
                WPGrid.Item(0, i).Value = True
                SelectedWays = SelectedWays + 1
            Else
                WPGrid.Item(0, i).Value = False
            End If
        Next
        TextBox3.Text = SelectedWays
    End Sub

This code will check/uncheck all check the boxes in the first column and put the total count in a textbox. That part works great. The problem I am having is with some code that counts them if a single box is checked.

Private Sub WPGrid_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles WPGrid.CellMouseUp
        SelectedWays = 0
        Dim i
        For i = 0 To WPGrid.Rows.Count - 1
            If WPGrid.Item(0, i).Value = True Then
                SelectedWays = SelectedWays + 1
            End If
        Next
        TextBox3.Text = SelectedWays
        ShowWaypoint()
    End Sub

The problem is this code wont count the box that was just checked until you pick another cell in the grid. I have tried doing it in a CellValueChanged() routine but either one does the same thing. You can check/uncheck the same box over and over and it will never count it until you select another cell. For some reason it won't count the currently selected cell.

Edited 6 Years Ago by mmosoul: n/a

Problem solved. I just have it select the next column after the checkbox is checked then have it count the checkboxes in a CurrentCellChanged() subroutine.

Private Sub WPGrid_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles WPGrid.CellMouseUp
        Dim crow, ccol As Integer
        crow = WPGrid.CurrentCell.RowIndex
        ccol = WPGrid.CurrentCell.ColumnIndex
        If ccol = 0 Then
            WPGrid.CurrentCell = WPGrid.Rows(crow).Cells(1)
        End If
    End Sub

    Private Sub WPGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WPGrid.CurrentCellChanged
        SelectedWays = 0
        Dim i
        For i = 0 To WPGrid.Rows.Count - 1
            If WPGrid.Item(0, i).Value = True Then
                SelectedWays = SelectedWays + 1
            End If
        Next
        TextBox3.Text = SelectedWays
    End Sub

Problem solved. I just have it select the next column after the checkbox is checked then have it count the checkboxes in a CurrentCellChanged() subroutine.

Private Sub WPGrid_CellMouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles WPGrid.CellMouseUp
        Dim crow, ccol As Integer
        crow = WPGrid.CurrentCell.RowIndex
        ccol = WPGrid.CurrentCell.ColumnIndex
        If ccol = 0 Then
            WPGrid.CurrentCell = WPGrid.Rows(crow).Cells(1)
        End If
    End Sub

    Private Sub WPGrid_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles WPGrid.CurrentCellChanged
        SelectedWays = 0
        Dim i
        For i = 0 To WPGrid.Rows.Count - 1
            If WPGrid.Item(0, i).Value = True Then
                SelectedWays = SelectedWays + 1
            End If
        Next
        TextBox3.Text = SelectedWays
    End Sub

Yes! I have tried it and yes it works perfectly. Thank you so much for your time and effort. A little effort goes a long way and therefore i hope anyone who has the same prob as i do are able to solve theirs through this solution that you have given. Once again, thank you mmosoul.

This article has been dead for over six months. Start a new discussion instead.