Solved

Select all rows in datagridview

Posted on 2016-10-26
15
17 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
Comment Utility
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
Comment Utility
Is there any other way apart from looping thr the rows in datagrdiview
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
 
LVL 4

Expert Comment

by:Elizabeth Anderson
Comment Utility
0
 

Author Comment

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

Author Comment

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

Expert Comment

by:AndyAinscow
Comment Utility
>>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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:RIAS
Comment Utility
Andy,

I have rows like 50K.
0
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
And it works with reasonable performance?

Do you know about virtual mode ?
0
 

Author Comment

by:RIAS
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comment Utility
Cheers!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now