Solved

Select all rows in datagridview

Posted on 2016-10-26
15
32 Views
Last Modified: 2016-10-26
Hello,

Need to set boolean value to true all the rows in datagridview.
Tried SelectAll method but,it does not put check or true value in the checkbox column.

The selection clolumn is added in datagridview as follows:

 If dg.Name <> "DgHelpUsers" Then
                .Columns.Add("Sel", GetType(Object))
                .Columns("Sel").SetOrdinal(0)
            End If
0
Comment
Question by:RIAS
15 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41859942
Select will select things - it is not the same as setting a value in a check box.  They are two different things altogether.

I think you will have to loop through all the rows in the datagridview and explicitly set the value of the cheeckbox to whatever you want.
0
 

Author Comment

by:RIAS
ID: 41859943
Is there any other way apart from looping thr the rows in datagrdiview
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41859946
If the source is a database you could run an update query and then repopulate the grid.

Built in functionality will do things with the gridview.  You want to change the contents - which is your data and a bit away from the base functionality the gridview needs.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

by:Elizabeth Anderson
ID: 41860006
0
 

Author Comment

by:RIAS
ID: 41860007
Tried SelectAll method but,it does not put check or true value in the checkbox column.
0
 

Author Comment

by:RIAS
ID: 41860010
Basically want to avoid looping through all the rows as its taking a lot of time.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41860016
>>Basically want to avoid looping through all the rows as its taking a lot of time.
Bear in mind:  Sometimes what one wants one can't have.

What sort of time is it taking?  How many rows?  Can you modify the underlying data then refresh the GUI?
0
 

Author Comment

by:RIAS
ID: 41860018
Andy,

I have rows like 50K.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 41860024
And it works with reasonable performance?

Do you know about virtual mode ?
0
 

Author Comment

by:RIAS
ID: 41860039
It works but,just need a better way.
No not tried virtual mode.
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 250 total points
ID: 41860128
I use DatatGridViews extensively, and often have a need to do what you're asking. I've not yet found a better way other than as suggested - loop through all rows and set the value of your column as needed, or add a column to the data source that can be set via SQL before loading the DGV.

With that said: In your code above, are you adding a column to your DGV, or to a DataTable? Your code does not translate over to a DataGridView, but rather to a DataTable. The line below:

.Columns.Add("Sel", GetType(Object))

gives an error when I try to add that to a DataGridView. The second argument of that for a DGV column is the HeaderText value, not the DataType.

If you're adding a column to a Datatable, and you want that column to be "selected" (i.e. a checkbox) then you could do this:

Dim col As New DataColumn
col.Name = "SomeName"
col.DataType = GetType(Boolean)
col.DefaultValue = True
YourDataTable.Columns.Add col

This would add a Boolean column, and by default that column would be checked.
0
 

Author Comment

by:RIAS
ID: 41860164
I add column like this :
  Public Sub BindDatagridview(ByVal dt As DataTable, ByVal dg As DataGridView)
        Dim dv As DataView
        With dt
            'Outstanding  check List_MouseDown
            Dim SQL As String = String.Empty
            'Initialize the image column.
            If dg.Name <> "DgHelpUsers" Then
                .Columns.Add("Sel", GetType(Object))
                .Columns("Sel").SetOrdinal(0)
            End If
            If dt.Rows.Count <> 0 Then
                dg.DataSource = dt
                dg.Rows(0).Selected = False
                dg.ClearSelection()
                For inti = 0 To dg.DataSource.columns.count - 1
                    dg.Columns(inti).SortMode = DataGridViewColumnSortMode.Programmatic
                Next
                If dg.Name <> "DgHelpUsers" Then
                    With dg.Columns("Sel")
                        .ToolTipText = "Selection"
                        .HeaderText = ""
                        .DefaultCellStyle.ForeColor = Color.Transparent
                        .Width = 15
                    End With

                End If
            Else
                'The datatable is empty so load only the columns
                dv = New DataView(dt)
                'add code for sorting as original defaultview
                dg.DataSource = dv
            End If
        End With
    End Sub

Open in new window

0
 

Author Comment

by:RIAS
ID: 41860167
Scott,
The requirement is to keep it unselected on load then there is a button for selectAll.
Selectall will select all rows on the grid
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 250 total points
ID: 41860195
>>No not tried virtual mode.

Learn about it and try it.  You can display millions of records in a fraction of a second.  (Because you don't actually display millions, only those that are visible).
0
 

Author Closing Comment

by:RIAS
ID: 41860263
Cheers!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question